1

I am using the @JsonAdapter annotation of Gson library to serialize/deserialize my JSON objects to POJO.

The deserialization is working fine, but the serialization is giving me this

 {
    "name": "ana",
    "email": "test@test.co.uk",
    "address": {
        "address_line1": "123 Some Address",
        "address_line2": "",
        "address_city": "My city",
        "address_postalcode": "CV31 9GP"
    }
}

instead of

{
    "name": "ana",
    "email": "test@test.co.uk",
    "address_line1": "123 Some Address",
    "address_line2": "",
    "address_city": "My city",
    "address_postalcode": "CV31 9GP"
}

This is because I'm returning a JsonElement, but I can't see another way to serialize my Address object.

Is there any way to return the outer key-value pairs?

User.java:

public class User() {
    private String name;
    private String email;

    @JsonAdapter(AddressSerializer.class)
    private Address address;
    ...
}

AddressSerializer.java

public class AddressSerializer implements JsonSerializer<Address> {

    @Override
    public JsonElement serialize(Address src, Type typeOfSrc, JsonSerializationContext context) {
        JsonObject jsonAddress = new JsonObject();

        jsonAddress.addProperty("address_line1", src.getAddressLine1());
        jsonAddress.addProperty("address_line2", src.getAddressLine2());
        jsonAddress.addProperty("address_city", src.getCity());
        jsonAddress.addProperty("address_postalcode", src.getPostcode());

        return jsonAddress;
    }
}
Ana Varani
  • 105
  • 1
  • 8
  • You have Address pojo class inside User thats why you are getting like that. it should be like as name – srinu Apr 13 '18 at 10:15

2 Answers2

0

Have you tried @JsonUnwrapped?

edit: But yeah as @srinu pointed out the JSON you are getting now is the standard representation which is showing that Address is a different object. Otherwise it looks like all properties belong to the user.

  • Yes, make sense! I've created this Address object to reuse in my other classes that receive the same 4 keys for the address. I'm trying to find a way to bring the address keys to its parent, at the same level of "name" key. – Ana Varani Apr 13 '18 at 10:51
  • @JsonUnwrapped private Address address; – str8tonowhere Apr 16 '18 at 13:13
  • Isn't @JsonUnwrapped a Jackson annotation? – Ana Varani Apr 16 '18 at 13:50
  • Yes, @JsonUnwrapped is a Jackson annotation. I believe there isn't a equivalent for this in Gson at this moment. [here](https://stackoverflow.com/questions/43294694/how-to-implement-a-gson-equivalent-of-jsonunwrap) they seems to talk about an alternative. – Robson Farias Apr 25 '18 at 20:14
0

Seems related to this question.

You could do something like that for your User serializer:

public class UserSerializer implements JsonSerializer<User> {

    @Override
    public JsonElement serialize(User src, Type typeOfSrc, JsonSerializationContext context) {
        JsonObject jsonUser = new JsonObject();

        jsonUser.addProperty("name", src.getName());
        jsonUser.addProperty("email", src.getEmail());
        jsonUser.addProperty("address_line1", src.getAddress().getAddressLine1());
        jsonUser.addProperty("address_line2", src.getAddress().getAddressLine2());
        jsonUser.addProperty("address_city", src.getAddress().getCity());
        jsonUser.addProperty("address_postalcode", src.getAddress().getPostcode());

        return jsonUser;
    }
}
Robson Farias
  • 151
  • 1
  • 3
  • 14