I am using the Gson library to parse JSON messages into Java POJOs before persisting to a PostgreSQL database. The PostgreSQL database schema cannot be changed.
An example POJO Message.java is below:
@TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)
public class Message {
    @Column(nullable = false, columnDefinition = "jsonb")
    @Type(type = "jsonb")
    private String messageDetail;
    @Column(nullable = false)
    private String messageType;
    @Column(nullable = false, unique = true)
    private String messageId;
}
An example JSON message is below:
{
    "messageId": "ae97d31b-c63e-42fd-b619-f3cdcb169f58",
    "messageDetail": {
        "count": 5
    },
    "messageType":"AdminUserSearch"
}
I want to the messageDetail which is a JSON object to be parsed (kept) as a String. Gson is failing as it seems to recognise that it is a JSON object and hence not allowing me to keep it as a string. For database integration reasons I would prefer this field on my POJO to be a string rather than anything else e.g. Object, JSONNode, Map<String,Object>.
The following code fails:
String messageString = gson.toJson(kafkaMessage);
JsonObject messageObject = gson.fromJson(messageString, JsonObject.class);
Message messagePojo = gson.fromJson(messageObject, Message.class);
 
    