Tasos Stamadianos
11/26/2018, 6:17 PM{
"sessionId": "...",
"commandId": "...",
"message": { ... }
}
So, at a lower layer of my code, I parse the sessionId
and the commandId
using JsonTreeParser
, and using the same parser I extract message
as a JsonObject. Later on, whoever needs to use the message
JsonObject can deserialize to whatever they want using mapper.readTree
.
Now, say I need to serialize back to another format. Specifically:
{
"success": true,
"message": "...",
"commandId": "...",
"payload": { ... }
}
That payload
field is any custom class which needs to be serializable and passed somehow, but I can't find a way to make it a JsonObject, or write any JsonObject for that matter. I also tried making a generic class with an field of the type of payload
, but that didn't seem like the right direction. I guess what I'm asking is, how can I create a JsonObject from an instance of a class?sandwwraith
11/26/2018, 8:52 PMJsonTreeMapper
can convert serializable classes to json tree and backTasos Stamadianos
11/26/2018, 8:54 PMwriteTree
function what i'm looking for?sandwwraith
11/26/2018, 8:55 PMTasos Stamadianos
11/26/2018, 9:00 PMsandwwraith
11/26/2018, 9:11 PMPairSerializer(StringSerializer, IntSerializer)
for Pair<String, Int>
Tasos Stamadianos
11/26/2018, 9:14 PMfun <T: Any> writePayload(payloadSerializer: SerializationStrategy<T>, obj: T) {
jsonTree.writeTree(Pair("payload", obj), StringSerializer to payloadSerializer)
}
sandwwraith
11/26/2018, 9:22 PMPairSerializer
is a class in internal package, not a pair of serializers 🙂Tasos Stamadianos
11/26/2018, 9:22 PMsandwwraith
11/27/2018, 6:47 AMJsonElement -> String
? Just call .toString and you would get a correct JSON.