Announcement Announcement Module
Collapse
No announcement yet.
How to setup the Json mixin properly for Foursquare data Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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, 04:52 PM.

  • #2
    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.

    Comment


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

      Comment


      • #4
        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

        Comment


        • #5
          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.

          Comment


          • #6
            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.

            Comment


            • #7
              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.

              Comment

              Working...
              X