java - Gson custom deserializer not called for null values -


i'm using gson serialization in javafx context. avoid having property boilerplate in serialized json, created handful of custom serializers/deserializers write property's value instead of actual property object, in particular 1 generic property<t>:

private static class propertyserializer implements jsonserializer<property<?>> {     @override     public jsonelement serialize(property<?> property, type type, jsonserializationcontext context) {         object value = property.getvalue();         return context.serialize(value);     } }  private static class propertydeserializer implements jsondeserializer<property<?>> {     @override     public property<?> deserialize(jsonelement json, type typeoft, jsondeserializationcontext context) throws jsonparseexception {         if (json.isjsonnull()) {             system.out.println("i never printed");         }         type typeparam = ((parameterizedtype) typeoft).getactualtypearguments()[0];         object obj = context.deserialize(json, typeparam);         return new simpleobjectproperty<>(obj);     } } 

it worked pretty until stumbled upon null values. design, when property's value null (not property itself), serializer write null because writes value, not property (i have used gson.serializenulls() make write it).

my problem that, @ deserialization time, my custom serializer not called null values because of a gson bug caused these lines in com.google.gson.treetypeadapter:

@override public t read(jsonreader in) throws ioexception {     if (deserializer == null) {         return delegate().read(in);     }     jsonelement value = streams.parse(in);     if (value.isjsonnull()) {         return null; // bypasses custom deserializer! why dat me?     }     return deserializer.deserialize(value, typetoken.gettype(), gson.deserializationcontext); } 

this results in property being null after deserialization instead of non-null property containing null value.


they marked bug fixed because apparently there workaround using typeadapterfactory, can't seem figure out how it. looks have manually deserialize object, don't know type can't. typeadapter methods read , write don't provide me jsondeserializationcontext of current gson, can't rely on standard deserialization value (which did custom deserializer).

private static class propertytypeadapter extends typeadapter<property<?>> {      @override     public void write(jsonwriter out, property<?> value) throws ioexception {         object valuetoserialize = value.getvalue();         // how use standard serialization of object?     }      @override     public property<?> read(jsonreader in) throws ioexception {         switch (in.peek()) {             case null:                 return new simpleobjectproperty<>(null);             default:                 type typeoft = ???                 type typeparam = ((parameterizedtype) typeoft).getactualtypearguments()[0];                 return ???; // how invoke standard deserialization of object?         }     } } 

does know how solve this?

for future readers: didn't solution implemented @ first in other answer, changed use typeadapter instead of custom deserializer.

this takes more code write, ended writing lightweight library tailored use gson in context of javafx, no 1 has write again :)

here is: fxgson.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -