Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
367 views
in Technique[技术] by (71.8m points)

java - Rules for Jersey to parse JSON/ Jackson Subtype deserialisation

I receive JSONs the way:

@POST
@Path("log")
public Map<String, List<OperationResult>> log(Stats stats) {
  ..
}

The examples of JSONs:

{
  "eventType": 1
  "params": {
    "field1" : 10
  }
}

{
  "eventType": 2
  "params": {
    "field2" : "ten"
  }
}

I have a class structure (they are generated by jsonschema2pojo, suppose it does not matter):

interface Params;
class Params1 implements Params{ public int field1; }
class Params2 implements Params{ public String field2; }

class Stats {
  public int eventType;
  public Params params;
}

How can I make Jersey to parse JSONs so that if eventType = 1 then stats.params becomes an instance of Params1 and else of Params2?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I spent some time to work this out this morning. An interesting usecase. I figured out how to do that, but I had to change your json slightly. This is not strictly necessary, however type-conversion wasn't partof your question, so we can do a follow up if needed :)

Your json:

artur@pandaadb:~/tmp/test$ cat 1.json 
{
  "eventType": "1",
  "params": {
    "field1" : 10
  }
}
artur@pandaadb:~/tmp/test$ cat 2.json 
{
  "eventType": "2",
  "params": {
    "field2" : "10"
  }
}

I am using those 2 files to do the request. Note that I changed the eventType t be a String rather than a number. I will point that out later.

Your model objects:

public class Stats {

    @JsonProperty
    int eventType;


    public Params params;

    @JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.EXTERNAL_PROPERTY, property="eventType")
    @JsonSubTypes({ @Type(value = Param1.class, name = "1"), @Type(value = Param2.class, name = "2") })
    public void setParams(Params params) {
        this.params = params;
    }
}

I am using JsonTypeInfo, here's what this does:

JsonTypeInfo.Id.NAME 

logical type name, in your case it is the property "eventType"

JsonTypeInfo.As.EXTERNAL_PROPERTY 

Means that an external property is used for deserialisation context. You can only use that on the property, not as a class annotation on Params itself. That is why I annotate the setter method instead of the interface class.

property="eventType" 

Simply tells jackson what property name to use

Then in the JsonSubTypes I annotate the options that are possible, in your case 2:

@Type(value = Param1.class, name = "1") 

this tells jackson to use Param1.class in case the eventType property is "1"

Accordingly the same for PAram2.class and the property value being "2"

NOTE This is why I changed the json slightly. The subtype annotations can not take an integer as property. Now there are different options you could be using, e.g. TypeConverters that convert your integer property into a string at runtime, and that way you can keep your json the same. I skipped that step, a quick google will give you instructions on how to do that though.

Now your parameter model looks like that:

public interface Params {

    public static class Param1 implements Params {
        @JsonProperty
        int field1;
    }

    public static class Param2 implements Params {

        @JsonProperty
        String field2;
    }

}

I am annotating the properties so Jackson knows to deserialise those.

NOTE I had a bit of an issue because there are two properties that to my lazy tired eyes look the same:

JsonTypeInfo.As.EXTERNAL_PROPERTY
JsonTypeInfo.As.EXISTING_PROPERTY

You can not use EXISTING :D This literally took ten minutes to figure out. Fun fact, I had both lines above and kept commenting one out, not getting why on earth one of them is throwing an exception while the other works.

Anyway.

And finally the test:

artur@pandaadb:~/tmp/test$ curl -XPOST  "localhost:8085/api/v2/test" -d @1.json -H "Accept: application/json" -H "Content-Type: application/json"
io.gomedia.resource.Params$Param1
artur@pandaadb:~/tmp/test$ 
artur@pandaadb:~/tmp/test$ curl -XPOST  "localhost:8085/api/v2/test" -d @2.json -H "Accept: application/json" -H "Content-Type: application/json"
io.gomedia.resource.Params$Param2

Note that the resource is printing the name of the instantiated class. As you can see both json have been deserialised into the correct instance class.

I hope that helps :)

Artur

(Fun fact #2: In my answer I also used EXISTING and not EXTERNAL and just didn't see it. I might need to ask jackson to change their names for my sanity's sake)

EDIT

I just tried it, and Jackson is smart enough to convert your json for you. So, you can leave the json as is, and simply have the property in your model as a String (as demonstrated). Everything works fine.

For completeness though, in case you want a converter (because you might need that to convert your string model back into an integer for serailisation), this would be a integer-to-string converter:

public class EventTypeConverter implements Converter<Integer, String>{

    @Override
    public String convert(Integer value) {
        return String.valueOf(value);
    }

    @Override
    public JavaType getInputType(TypeFactory typeFactory) {
        return SimpleType.construct(Integer.class);
    }

    @Override
    public JavaType getOutputType(TypeFactory typeFactory) {
        return SimpleType.construct(String.class);
    }

}

You can use it by doing:

@JsonProperty
@JsonDeserialize(converter=EventTypeConverter.class)
String eventType;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...