Search code examples
javajsonrobospice

Robospice dynamically mapping


Can somebody tell me how it's possible to do a dynamically mapping with RoboSpice in my "RequestResult.java"?

My JSON looks either like this:

"result": [
    {
      "attributeX1" "...",
      "type": "x",
      "attributeXi" "..."
    },
    {
      "attributeX1": "...",
      "type": "x",
      "attributeXi": "..."
     }
]

or

"result": [
    {
      "attributeY1" "...",
      "type": "y",
      "attributeYi" "..."
    },
    {
      "attributeY1" "...",
      "type": "y",
      "attributeYi" "..."
    }
]

On a request the type is either x or y. So if "type": "x" it's necessary for me to use model "x.java" and if "type" : "y" need to use model "y.java"


Solution

  • It all depends on the JSON library you are using. Jackson 2 offers a polymorphic type that could be interesting for you : http://programmerbruce.blogspot.com.es/2011/05/deserialize-json-with-jackson-into.html

    The official doc is here : http://wiki.fasterxml.com/JacksonPolymorphicDeserialization