java - Is it possible to allow Gson to just skip fields with wrong types? -
i have class sdk don't have access change, serialize json-valid string into.
however, external api puts in wrong type date field.
long story short: can ignore errors in gson, or tell gson ignore errors on fields, , partial object?
for example, field should double, date(number) instead. i'm not using anyway, don't care, , don't need whole process fail. want parcable fields out, faulty fields left null.
note: writing deserializer creates object want have created gson defeats purpose propose.
this line of code fails, because single field wrong:
customer customer = gson.fromjson(settings.getcustomerobjectjsonstring(), customer.class);
i skip field cannot parse, because don't have access customer class, generated sdk/library.
i'm aware of 2 options.
you can use a json deserializer implementation parse json elements on own. following example affect double
, double
fields whatever dtos passed single gson
instance, , such behavior can deseriable. unfortunately, don't know if it's possible use jsondeserializer
in "context" way: e.g. let work double
, double
fields if fields of parent class.
private static final class dto { private double primitive; private double nullable; private string string; } private static final class failsafedoublejsondeserializer implements jsondeserializer<double> { @override public double deserialize(final jsonelement element, final type type, final jsondeserializationcontext context) throws jsonparseexception { if ( !element.isjsonprimitive() ) { return null; } try { final jsonprimitive primitive = (jsonprimitive) element; final number number = primitive.getasnumber(); return number.doublevalue(); } catch ( final numberformatexception ignored ) { return null; } } } private static final jsondeserializer<double> failsafedoublejsondeserializer = new failsafedoublejsondeserializer(); private static final gson gson = new gsonbuilder() .registertypeadapter(double.class, failsafedoublejsondeserializer) .registertypeadapter(double.class, failsafedoublejsondeserializer) .create(); public static void main(final string... args) { dump(gson.fromjson("{\"primitive\": 23, \"nullable\": 42, \"string\": \"foo bar\"}", dto.class)); dump(gson.fromjson("{\"primitive\": \"whatever\", \"nullable\": \"whatever\", \"string\": \"foo bar\"}", dto.class)); } private static void dump(final dto dto) { out.println(dto.primitive + " " + dto.nullable + " " + dto.string); }
another more low level option can a type adapter implementation. 1 advantage of 1 on previous example can annotate failing fields @jsonadapter
annotation in dto classes known potentially broken.
private static final class dto { @jsonadapter(failsafedoubletypeadapter.class) private double primitive; @jsonadapter(failsafedoubletypeadapter.class) private double nullable; private string string; } private static final class failsafedoubletypeadapter extends typeadapter<double> { @override public void write(final jsonwriter writer, final double value) { throw new unsupportedoperationexception(); } @override public double read(final jsonreader reader) throws ioexception { final jsontoken peek = reader.peek(); if ( peek != number ) { reader.skipvalue(); return null; } return reader.nextdouble(); } } private static final gson gson = new gson(); public static void main(final string... args) { dump(gson.fromjson("{\"primitive\": 23, \"nullable\": 42, \"string\": \"foo bar\"}", dto.class)); dump(gson.fromjson("{\"primitive\": \"whatever\", \"nullable\": {\"subvalue\": \"whatever\"}, \"string\": \"foo bar\"}", dto.class)); } private static void dump(final dto dto) { out.println(dto.primitive + " " + dto.nullable + " " + dto.string); }
thus, both examples generate following output:
23.0 42.0 foo bar
0.0 null foo bar
for
{"primitive": 23, "nullable": 42, "string": "foo bar"}
- and
{"primitive": "whatever", "nullable": {"subvalue": "whatever"}, "string": "foo bar"}
payloads respectively.
Comments
Post a Comment