RESTful and flexjson trivial question

Help
2013-01-24
2013-03-19
  • Agustin Treceno

    Agustin Treceno - 2013-01-24

    Hi all, I am adding RESTful support to my web application so I can do CRUD operations like this:

    Read info about a participant (JSONSerialize):
    <code>
    $ curl -i -H "Accept: application/json" http://localhost:8080/dancingwithstars/participant/1
    {
        "birthDate": "2013-01-23",
        "code": "JS0001",
        "firstName": "Julia",
        "mainFunction": {
            "code": "AA"
            "name": "Principal Dancer"
        },
        "gender": "F",
        "height": 160.00,
        "id": 1,
        "lastName": "Smith",
        "version": 1,
        "weight": 55.00
    }
    </code>
    Or create a new participant (JSONDeserialize)
    <code>
    $ curl -i -X POST -H "Content-Type: application/json" -H "Accept: application/json" -d '{"birthDate":"2013-01-15","code":"AT0001","firstName":"Angela","mainFunction":{"code": "AB", "name":"Choreografer"},"gender":"F","height":189.00,"lastName":"Wright","version":0,"weight":76.00}' http://localhost:8080/dancingwithstars/participants
    </code>
    But the thing is, I don't need to know all the fields of the mainFunction object to create a participant I just need to know the code which is unique. For the serialization I can use:
    <code>
    return new JSONSerializer().include("mainFunction.code").exclude("mainFunction.*").exclude("*.class").transform(new DateTransformer("yyyy-MM-dd"), Date.class).prettyPrint(true).serialize(this);
    </code>
    And I get the following json:
    <code>
    {
        "birthDate": "2013-01-23",
        "code": "JS0001",
        "firstName": "Julia",
        "mainFunction": {
            "code": "AA"
        },
        "gender": "F",
        "height": 160.00,
        "id": 1,
        "lastName": "Smith",
        "version": 1,
        "weight": 55.00
    }
    </code>
    But I would like:
    <code>
    {
        "birthDate": "2013-01-23",
        "code": "JS0001",
        "firstName": "Julia",
        "mainFunction": "AA",
        "gender": "F",
        "height": 160.00,
        "id": 1,
        "lastName": "Smith",
        "version": 1,
        "weight": 55.00
    }
    </code>
    For the deserialization I don't know. The requirement seems pretty common so I wanted to ask in the forum. Any ideas?

    Thanks a lot!
    Agustin

     
  • Charlie Hubbard

    Charlie Hubbard - 2013-01-29

    You'll have to write a specialized ObjectFactory to collapse the "AA" field onto a code.  It will have to take care of deserializing that field and binding it into the object.  You can attach that ObjectFactory on the path using the use() method.

        new JSONDeserializer<Participant>().use( "mainFunction", new SpecialObjectFactory() ).deserialize( json, Participant.class );

     

Log in to post a comment.