Understanding DeserializationExclusionStrategy

26 views
Skip to first unread message

ron.difrango

unread,
May 13, 2013, 12:24:21 PM5/13/13
to googl...@googlegroups.com
I'm trying to understand how the exclusion strategies work and I created the following strategy:

       static class GsonApproach implements ExclusionStrategy {
        public boolean shouldSkipClass(Class<?> clazz) {
            return clazz.getAnnotation(Custom.class) != null;
        }

        @Override
        public boolean shouldSkipField(FieldAttributes field) {
            return field.getAnnotation(Customclass) != null;
        }
    }

And when I create a class like so:

    @Custom
    static class AnnotatedClassIn {
        private Long statusCode;

        public Long getStatusCode() {
            return statusCode;
        }

        public void setStatusCode(Long statusCode) {
            this.statusCode = statusCode;
        }
    }

It works fine to create JSON:

        ExclusionStrategy strategy = new GsonApproach();
        GsonBuilder builder = new GsonBuilder().setPrettyPrinting().addDeserializationExclusionStrategy(strategy);
       
        Gson gson = builder.create();
        AnnotatedClassIn in = new AnnotatedClassIn();
        in.statusCode = Long.valueOf(1);
       
        String inString = gson.toJson(in);

Produces:

In: {
  "statusCode": 1
}

But when I try and reverse it:

AnnotatedClassIn in2 = gson.fromJson(inString, AnnotatedClassIn.class);

I get a NULL object back.

Thoughts?
Reply all
Reply to author
Forward
0 new messages