java - How can I handle false as null for any primitive or boxed numerical bean property in Jackson? -
i've been struggling issue. endpoint receives lot of broken api calls php application there false
booleans set numerical fields.
how can attach custom deserializer objectmapper
convert false
values null
or 0
depending upon type of corresponding numerical property in bean?
here's json:
{ "name": "my name", "image_url": null, "price": false "list_price": false; }
here's example of bean:
public class item { public string name; public string imageurl; public double price; public double listprice; }
i've custom deserializer along lines of means explicitly need attach every field:
public class falseasnulldeserializer extends jsondeserializer<object> implements contextualdeserializer { private final class targettype; private falseasnulldeserializer() { this.targettype = null; // required jackson } public falseasnulldeserializer(class<?> targettype) { this.targettype = targettype; } @override public object deserialize(jsonparser parser, deserializationcontext ctxt) throws ioexception { jsonnode node = parser.readvalueastree(); if (!node.isobject() && !node.asboolean()) { return null; } else { return jsonutils.getmapper().treetovalue(node, targettype); } } @override public jsondeserializer<?> createcontextual(deserializationcontext context, beanproperty property) throws jsonmappingexception { return new falseasnulldeserializer(property.gettype().getrawclass()); } }
Comments
Post a Comment