Home  >  Article  >  Java  >  How do you serialize polymorphic objects with Gson and preserve inherited members in the JSON output?

How do you serialize polymorphic objects with Gson and preserve inherited members in the JSON output?

Linda Hamilton
Linda HamiltonOriginal
2024-10-31 21:09:02477browse

How do you serialize polymorphic objects with Gson and preserve inherited members in the JSON output?

Serializing Polymorphic Objects with Gson

Gson is a popular Java library for converting Java objects to and from JSON. One challenge in serializing objects with Gson is handling polymorphism, where multiple subclasses inherit from a common superclass.

Problem

Consider a scenario where you have a class hierarchy with a base class ObixBaseObj and multiple inherited classes like ObixOp. The goal is to serialize objects of these classes to JSON using Gson while preserving the inherited members (e.g., in and out for ObixOp).

GsonSerialization Example

<code class="java">ObixBaseObj lobbyObj = new ObixBaseObj();
lobbyObj.setIs("obix:Lobby");

ObixOp batchOp = new ObixOp();
batchOp.setName("batch");
batchOp.setIn("obix:BatchIn");
batchOp.setOut("obix:BatchOut");

lobbyObj.addChild(batchOp);

Gson gson = new Gson();
System.out.println(gson.toJson(lobbyObj));</code>

Result

<code class="json">{"obix":"obj","is":"obix:Lobby","children":[{"obix":"op","name":"batch"}]}</code>

Notice that the serialized JSON does not include the inherited members of ObixOp (in and out).

RuntimeTypeAdapterFactory Solution

To handle polymorphism gracefully, Gson provides a powerful feature called RuntimeTypeAdapterFactory. This class automatically registers subclasses to their corresponding superclass for serialization and deserialization.

Implementation

<code class="java">RuntimeTypeAdapterFactory<ObixBaseObj> adapter = 
                    RuntimeTypeAdapterFactory
                   .of(ObixBaseObj.class)
                   .registerSubtype(ObixBaseObj.class)
                   .registerSubtype(ObixOp.class);


Gson gson2=new GsonBuilder().setPrettyPrinting().registerTypeAdapterFactory(adapter).create();
Gson gson = new Gson();
System.out.println(gson.toJson(lobbyObj));
System.out.println("---------------------");
System.out.println(gson2.toJson(lobbyObj));</code>

Result

<code class="json">{"obix":"obj","is":"obix:Lobby","children":[{"obix":"op","name":"batch","children":[]}]}
---------------------
{
  "type": "ObixBaseObj",
  "obix": "obj",
  "is": "obix:Lobby",
  "children": [
    {
      "type": "ObixOp",
      "in": "obix:BatchIn",
      "out": "obix:BatchOut",
      "obix": "op",
      "name": "batch",
      "children": []
    }
  ]
}</code>

This solution correctly serializes all members, including those inherited by subclasses.

Handling Multiple Subclasses

In cases where there are multiple subclasses, Gson's RuntimeTypeAdapter provides a convenient mechanism to register them.

  1. Create a centralized class for registering types (e.g., GsonUtils):
<code class="java">public class GsonUtils {

    private static final GsonBuilder gsonBuilder = new GsonBuilder()
            .setPrettyPrinting();

    public static void registerType(
            RuntimeTypeAdapterFactory<?> adapter) {
        gsonBuilder.registerTypeAdapterFactory(adapter);
    }

    public static Gson getGson() {
        return gsonBuilder.create();
    }
}</code>
  1. Register the adapter within the subclasses:
<code class="java">public class ObixBaseObj {

    private static final RuntimeTypeAdapterFactory<ObixBaseObj> adapter = 
            RuntimeTypeAdapterFactory.of(ObixBaseObj.class);

    static {
        GsonUtils.registerType(adapter);
    }

    private synchronized void registerClass() {
        if (!registeredClasses.contains(this.getClass())) {
            registeredClasses.add(this.getClass());
            adapter.registerSubtype(this.getClass());
        }
    }

    public ObixBaseObj() {
        registerClass();
        obix = "obj";
    }
}</code>

This approach ensures that all subclasses are automatically registered with the GsonBuilder, simplifying the serialization process.

The above is the detailed content of How do you serialize polymorphic objects with Gson and preserve inherited members in the JSON output?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn