I have a Java code that handles json inheritance the code is like this :
public class BaseMessage {
private String messageId;
private Integer type;
...
}
public class TextMessage extends BaseMessage {
private String recipient;
private String sender;
...
}
public class SystemTextMessage extends BaseMessage {
private String field1;
private String field2;
...
}
And some other classes
And I'm using Gson library like this:
RuntimeTypeAdapterFactory<BaseMessage> runtimeTypeAdapterFactory = RuntimeTypeAdapterFactory
.of(BaseMessage.class, "type")
.registerSubtype(TextMessage.class,
String.valueOf(MessageType.TEXT_MESSAGE))
.registerSubtype(SystemTextMessage.class,
String.valueOf(MessageType.SYSTEM_MESSAGE))
;
Gson gson = new GsonBuilder().registerTypeAdapterFactory(runtimeTypeAdapterFactory).create();
Type listType = new TypeToken<List<BaseMessage>>(){}.getType();
List<BaseMessage> list = gson.fromJson(json, listType);
And then I just iterate through the List and comparing by "instanceof".
And what about golang? Is there any way to do same thing? I failed to find something similar. Any 1 can help? Thank you.