julman99 / gson-fire

A java library that adds some very useful features to Gson, like Date serializing to unix timestamp or RFC3339, method (getters) serialization, pre/post processors and many others. Check out the documentation to learn how to use it!
http://gsonfire.io
Other
226 stars 36 forks source link

Add more powerful wrapping/unwrapping #32

Open kaqqao opened 7 years ago

kaqqao commented 7 years ago

Is it possible to implement a feature similar to Jackson's @JsonUnwrap that would automatically wrap/unwrap a Json field (potentially unwrapping its field(s) further)?

Example of the desired behavior:

class Address {
    String street;
    String number;
}

class Person {
    String fullName;
    @GsonUnwrap Address address;
}

class Business {
    String businessName;
    @GsonUnwrap Person owner;
}

This would then allow a flat Json structure such as:

{ 
    "businessName" : "BN", 
    "fullName" : "Some Dude", 
    "street" : "Big street", 
    "number" : "301A"
}

to be (de)serialized to/from a Business object.

Ideally, the strategy for deciding what to unwrap would be configurable so that annotations (as used above) are only one option, so that editing the beans isn't necessary. Going even further, supporting custom prefixing/suffixing to avoid name collision (see how I had to call fields businessName and fullName instead of just name) like @JsonUnwrap does would be absolute perfection.

Do you think this is feasible?

kaqqao commented 7 years ago

After looking deeper into Gson, this seems somewhat impractical to do. Still, I made a crude deserializer that implements this behavior: http://stackoverflow.com/questions/43294694/how-to-implement-a-gson-equivalent-of-jsonunwrap/43300684#43300684 Feel free to close this issue if you are not interested into going down this rabbit hole.