user1772250
user1772250

Reputation: 379

How do I use a custom Json Serializer inside another serializer

I have some Objects

public class MyObject {
    private String name;
    private String city;
    public MyObject(String n, String c) {
       name=n; city=c;}
    public String getName() {return name;}
    public String getCity() {return city;}
    public void setName(String n) {name=n;}
    public void setCity(String c) {city =c;}
}

And i have a custom serializer:

public class MySerializer implements JsonSerializer<MyObject> {
public JsonElement serialize(final MyObject myobj, final Type type,      JsonSerializationContext context) {
    JsonObject result = new JsonObject();
    result.add("id", new JsonPrimitive(myobj.getName()));
    return result;
}
}

Basically i just want to serilize only 1 of the 2 fields. this works great when do something like:

       GsonBuilder builder = new GsonBuilder();
       builder.registerTypeAdapter(MyObject.class, new MySerialize());
       Gson gson = builder.create();
       System.out.println(gson.toJson(new MyObject("test","something"));

however it gets a bit more complicated (and here is my question) when i have another object which is made up of "MyObject"s. How can i get the correct serializer to only serialize the one field of MyObject.

so i have another class:

public class SomeObject {
   private String id;
   private MyObject foo;
   private MyObject bar;
   ...
}

and i have a custom serializer:

  public JsonElement serialize(final SomeObject something, final Type type,            JsonSerializationContext context) {
    JsonObject result = new JsonObject();
    Gson gson = new Gson();
            result.add("id", new JsonPrimitive(something.getId()));

            //here i need help
            result.add("myobject1", new JsonPrimitive(gson.toJson(something.getFoo())));

           return result;
        }

I'm not sure if its best practice to create the GsonBuilder for "MyObject" inside the custom serializer for SomeObject is it?

ive tried something like:

GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(MyObject.class, new MySerialize());
builder.registerTypeAdapter(SomeObject.class, new SomeObjectSerializer());
Gson gson = builder.create();
System.out.println(gson.toJson(new SomeObject("id",new MyObject("test","something"),new                   MyObject("test2,"barrrrr"));

and i would exepct "{"id":"id","foo"{"id":"test"},"bar":{"id:"test2"}}

but that is not the case. bascially i want just the first field in a custom object whcih i have a seralizer for, but do i need to build that serializer inside another objects custom serializer? seems wrong, dunno why.

Upvotes: 2

Views: 2525

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280132

Note how you have access to the JsonSerializationContext in your custom JsonSerializer classes. You can call JsonSerializationContext#serialize(Object) and Gson will use a registered or default TypeAdapter to serialize that object and return a JsonElement which you can add to the outer JsonElement.

Upvotes: 2

Related Questions