java - How to serialize/deserialize partial JSON object using GSON? -
i have complex json many nested fields coming backend, example,
{ "a": ... "b": ... "not-required-1":{ "not-required-2":[ ... ] .. }, "not-required-3":{ "not-required-4":[ ... ] ... } } i interested in "a" , "b" , can map corresponding types values. object has many fields don't need should serialize when make changes in "a" , "b".
if create type "a" , "b", when serialize object, "not-required" fields gone. option map every field in not-required corresponding java class, fall in number of 100s.
clarification,
backend sends json, processed in code , need send json backend.. though don't need process fields, doesn't mean backend doesn't need them.
i want convert incoming json pojo , use through out app, , serialize json using gson.
c#'s json.net library has support hybird object serialization using json.linq.
c# example.
class dataobject{ [jsonproperty("a")] dataa {get;set;} [jsonproperty("b")] datab b {get;set;} [jsonproperty("not-required-a")] json.linq.jsonelement notrequireda {get;set;} [jsonproperty("not-required-b")] json.linq.jsonelement notrequiredb; {get;set;} } in c# way don't need map fields of notrequireda , notrequiredb, serialize correctly when needed.
you can use jsonelement. example:
class dataobject{ @expose @serialize("a") dataa a; @expose @serialize("b") datab b; @expose @serialize("not-required-a") jsonelement notrequired; ... }
Comments
Post a Comment