Java – custom Jackson unmarshalling behavior

I use Jackson fasterxml to ungroup JSON There are two kinds of properties in my object: input properties and calculated properties In input JSON, I only get the input value

The calculated value actually depends on the input value I have to fill in these values before referencing the object So I just check if Jackson has any hooks so that I can calculate there For example, JAXB provides the afterunmarshal method to define the ungrouping behavior:

void afterUnmarshal(Unmarshaller u,Object parent)

But I can't find similar information about custom Jackson Does Jackson provide any such framework hooks to customize the unmarshalling behavior?

Solution

I'd rather recommend using constructor creators to keep model objects immutable That is, all JSON values are passed to the constructor, which initializes other computed properties

In any case, if you want to customize an object after deserialization (without writing a deserializer for each type), you can modify the deserializer by finally calling the special method of the newly constructed instance This is an example for all classes that implement a special interface (consider using annotations to mark the post constructor)

public class Jacksonpostconstruct {

    public static interface postconstructor {
        void postconstruct();
    }

    public static class Bean implements postconstructor {
        private final String field;

        @JsonCreator
        public Bean(@JsonProperty("field") String field) {
            this.field = field;
        }

        public void postconstruct() {
            System.out.println("Post construct: " + toString());
        }

        @Override
        public String toString() {
            return "Bean{" +
                    "field='" + field + '\'' +
                    '}';
        }
    }

    private static class postconstructDeserializer extends DelegatingDeserializer {
        private final JsonDeserializer<?> deserializer;

        public postconstructDeserializer(JsonDeserializer<?> deserializer) {
            super(deserializer);
            this.deserializer = deserializer;
        }

        @Override
        protected JsonDeserializer<?> newDelegatingInstance(JsonDeserializer<?> newDelegatee) {
            return deserializer;
        }

        @Override
        public Object deserialize(JsonParser jp,DeserializationContext ctxt) throws IOException {
            Object result = _delegatee.deserialize(jp,ctxt);
            if (result instanceof postconstructor) {
                ((postconstructor) result).postconstruct();
            }
            return result;
        }
    }

    public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        SimpleModule module = new SimpleModule();
        module.setDeserializerModifier(new BeanDeserializerModifier() {
            @Override
            public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config,BeanDescription beanDesc,final JsonDeserializer<?> deserializer) {
               return new postconstructDeserializer(deserializer);
            }
        });
        mapper.registerModule(module);
        String json = "{\"field\":\"value\"}";
        System.out.println(mapper.readValue(json,Bean.class));
    }

}

Output:

Post construct: Bean{field='value'}
Bean{field='value'}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>