Results 1 to 7 of 7

Thread: How to setup the Json mixin properly for Foursquare data

  1. #1
    Join Date
    Jul 2010
    Location
    Brooklyn, NY
    Posts
    32

    Default How to setup the Json mixin properly for Foursquare data

    Hi guys,

    I'm trying to figure out the cleanest, simplest way to parse Foursquare data using the mixin technique from the other SaaS modules. Foursquare's envelope looks like this when querying for a user:

    Code:
    {
       "meta":{
          "code":200
       },
       "response":{
          "user":{
             "id":"700390",
             "firstName":"Matt",
             "lastName":"Wright",
             .....
          }
       }
    }
    Bear in mind that for other API calls, the property name under "response" is different depending on the call... for instance, the Todos call looks like so...

    Code:
    {
       "meta":{
          "code":200
       },
       "response":{
          "todos":{
             "count":2,
             "items":[
                {
                   "id":"4dfa596a8877b30c3987985f",
                   ...
                }
             ]
          }
       }
    }
    Any tips?
    Last edited by mattupstate; Jun 16th, 2011 at 04:52 PM.

  2. #2
    Join Date
    Aug 2004
    Posts
    1,067

    Default

    That particular JSON structure makes things tricky, that's for sure. It seems that the thing you really want is the content of the "response" field. Being a nested field, this complicates deserialization a bit.

    I don't believe that the Jackson annotations offer a way to dig deep into a JSON structure to start deserialization, which means you're going to have to do something a bit different. What I'm thinking is to create a container object that holds the object you really want. Using the user profile case as an example, you'd have...

    - A FoursquareUserContainer which has a FoursquareUser as a property.
    - FoursquareUserContainer would be annotated with @JsonDeserialize which would reference a deserializer to dig down to the "response" field in the JSON...at which point the deserializer would hand that node off to Jackson for deserialization.
    - Meanwhile, FoursquareUser isn't annotated at all. Instead there's a FoursquareUserMixin that is annotated with the appropriate @JsonCreator and @JsonProperty annotations.
    - When consuming that resource via RestTemplate, you'd getForObject() with the object being a FoursquareUserContainer. From that you'd retrieve the FoursquareUser and return it.

    I acknowledge it's not as simple as just tossing a mixin in, but the deep-nesting makes it tricky. There are probably ways to generalize the function of FoursquareUserContainer so that parts of it can be reused to deserialize all objects coming back in that JSON structure. I'd be interested to know if you find a simpler approach.
    Craig Walls
    Spring Social Project Lead

  3. #3
    Join Date
    Jul 2010
    Location
    Brooklyn, NY
    Posts
    32

    Default

    Thanks for the tip, Craig. I think that will put me in the right direction to solve this. Cheers!

  4. #4
    Join Date
    Aug 2004
    Location
    Melbourne, FL
    Posts
    2,794

    Default

    Matt,
    You might also want to try asking this question on the Jackson users list and see what you learn there: http://xircles.codehaus.org/projects/jackson/lists
    Keith Donald
    Core Spring Development Team

  5. #5
    Join Date
    Jul 2010
    Location
    Brooklyn, NY
    Posts
    32

    Default

    I referred to the Jackson mailing list and its a bit slow going over there. However, per Craigs advice I've come up with the following thus far...

    FoursquareUser
    https://github.com/mattupstate/sprin...quareUser.java

    FoursquareUserMixin
    https://github.com/mattupstate/sprin...UserMixin.java

    FoursquareUserContainer
    https://github.com/mattupstate/sprin...Container.java

    FoursquareUserContainerDeserializer
    https://github.com/mattupstate/sprin...erializer.java

    Notice the FoursquareContainerDeserializer extends AbstractFoursquareDeserializer
    https://github.com/mattupstate/sprin...erializer.java

    I'd love to hear some criticisms from the seasoned Java devs.

  6. #6
    Join Date
    Aug 2004
    Posts
    1,067

    Default

    Matt: I saw Tatu's response on the mailing list and he offered an approach that I hadn't thought of...it just might work (Tatu knows Jackson better than anyone, so it probably will). I'd recommend trying both approaches and see how they stack up against each other.

    From what I see in your code, you've done pretty much what I had in mind. And I see that you have tests around it, so I assume it's working for you. Let me know if I can help. FWIW, I get a lot of questions about whether or not there's a Foursquare module for Spring Social, so it'll be good to finally say that there is.
    Craig Walls
    Spring Social Project Lead

  7. #7
    Join Date
    Jul 2010
    Location
    Brooklyn, NY
    Posts
    32

    Default

    Nice to hear there's some desire for Foursquare. At any rate, I wasn't able to look too far into sub-types yet but I will when I get home.

    The single test I setup for this is passing so all is well will this so far.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •