java - Registering multiple adapters with GSON isn't working -
i'm having issue registering multiple typeadapters gsonbuilder. seems 1 fire off , never takes second 1 considering. if each 1 seems work fine. need them work both , seems doing wrong. using gson v2.2.4.
zip object simple form:
public class zip { private string zipcode; private string city; private string state; public zip(){} } zipserializer:
public class zipserializer implements jsonserializer<zip>{ @override public jsonelement serialize(zip obj, type type, jsonserializationcontext jsc) { gson gson = new gson(); jsonobject jobj = (jsonobject)gson.tojsontree(obj); jobj.remove("state"); return jobj; } } jsonresponse object simple form:
public class jsonresponse { private string jsonrpc = "2.0"; private object result = null; private string id = null; public jsonresponse(){} } jsonresponseserializer:
public class jsonresponseserializer implements jsonserializer<jsonresponse> { @override public jsonelement serialize(jsonresponse obj, type type, jsonserializationcontext jsc) { gson gson = new gson(); jsonobject jobj = (jsonobject)gson.tojsontree(obj); jobj.remove("id"); return jobj; } } test example:
public class test { public static void main(string[] args) { zip zip = new zip(); zip.setcity("testcity"); zip.setstate("oh"); zip.setzipcode("12345"); jsonresponse resp = new jsonresponse(); resp.setid("1"); resp.setresult(zip); resp.setjsonrpc("2.0"); gson gson1 = new gsonbuilder() .registertypeadapter(jsonresponse.class, new jsonresponseserializer()) .registertypeadapter(zip.class, new zipserializer()) .setprettyprinting() .create(); string json = gson1.tojson(resp); system.out.println(json); } } output:
{ "jsonrpc": "2.0", "result": { "zipcode": "12345", "city": "testcity", "state": "oh" } } expected output: (notice zipserializer didn't fire off)
{ "jsonrpc": "2.0", "result": { "zipcode": "12345", "city": "testcity" } } i simpled down test case this. know can use exclusionstrategy in example achieve results real issue more complex , best way me portray , replicate issue.
thanks
<---------------------------- solution ------------------------------->
i managed read gson custom seralizer 1 variable (of many) in object using typeadapter , helped me out greatly.
i created base customtypeadapterfactory , extended each class needed special serialization.
customizedtypeadapterfactory
public abstract class customizedtypeadapterfactory<c> implements typeadapterfactory { private final class<c> customizedclass; public customizedtypeadapterfactory(class<c> customizedclass) { this.customizedclass = customizedclass; } @suppresswarnings("unchecked") // use runtime check guarantee 'c' , 't' equal public final <t> typeadapter<t> create(gson gson, typetoken<t> type) { return type.getrawtype() == customizedclass ? (typeadapter<t>) customizemyclassadapter(gson, (typetoken<c>) type) : null; } private typeadapter<c> customizemyclassadapter(gson gson, typetoken<c> type) { final typeadapter<c> delegate = gson.getdelegateadapter(this, type); final typeadapter<jsonelement> elementadapter = gson.getadapter(jsonelement.class); return new typeadapter<c>() { @override public void write(jsonwriter out, c value) throws ioexception { jsonelement tree = delegate.tojsontree(value); beforewrite(value, tree); elementadapter.write(out, tree); } @override public c read(jsonreader in) throws ioexception { jsonelement tree = elementadapter.read(in); afterread(tree); return delegate.fromjsontree(tree); } }; } /** * override muck {@code toserialize} before written * outgoing json stream. */ protected void beforewrite(c source, jsonelement toserialize) { } /** * override muck {@code deserialized} before parsed * application type. */ protected void afterread(jsonelement deserialized) { } } ziptypeadapterfactory (did jsonresponsetypeadapterfactory)
public class ziptypeadapterfactory extends customizedtypeadapterfactory<zip> { ziptypeadapterfactory() { super(zip.class); } @override protected void beforewrite(zip source, jsonelement toserialize) { jsonobject obj = (jsonobject) toserialize; obj.remove("state"); } } test code:
gson gson1 = new gsonbuilder() .registertypeadapterfactory(new jsonresponsetypeadapterfactory()) .registertypeadapterfactory(new ziptypeadapterfactory()) .setprettyprinting() .create(); string json = gson1.tojson(resp); system.out.println(json); thank help.
problem in jsonresponseserializer. create new gson instance, on new instance zipserializeris not registered. that's why second serializer never called.
if want achieve delegation , complex serialization, @ typeadapterfactory.
as said, if want filter fields serialization define exclusionstrategy.
Comments
Post a Comment