I have a JSON stream being generated by a server side C++ program that is currently in development. I’ve been given a sample of the resulting JSON and I am concerned that I will have to parse the json by hand, that I won’t be able to use normal class mapping provided by tools such as GSON or Jackson.
Please take a look at the following (somewhat) contrived example they have provided. The sections I’m concerned with are the meta data “serie” array having different parameters. The key – “key” for example is present in only one of the array elements. Will this not cause issues trying to map this array to a collection of a specific class?
Lastly, I am concerned that the “point” object is not similar. I have very limited understanding of JSON (being an old fashioned java swing developer) but the fact that the “point” key value pairs can be different – is a problem.
The whole idea for this json stream is to describe a table, with ways of showing progress and to provide a mechanism for asking for “more” from the underlying hardware. Also if you are wondering why, I am sharing this datastream with a thin client (html browser).
So am I correct that this will not easily convert to java objects?
{
"abort": "abort;session=sessionname",
"data": {
"metadata": [
{
"protocol": "HTTP",
"serie": [
{
"name": "k1",
"description": "xDR ID",
"tooltip": "ASDR Unique Identifier - UiD",
"type": "int64",
"key": "1"
},
{
"name": "c1",
"description": "Answered",
"tooltip": "Request with Response",
"type": "bool"
},
{
"name": "c2",
"description": "Active",
"tooltip": "Session status: active or closed/down",
"type": "bool"
}
]
},
{
"protocol": "DNS",
"serie": [
{
"name": "k1",
"description": "xDR ID",
"tooltip": "ASDR Unique Identifier - UiD",
"type": "int64",
"key": "1"
},
{
"name": "k2",
"description": "Transaction ID",
"type": "int64",
"key": "1",
"display": "number"
},
{
"name": "k3",
"description": "Client",
"tooltip": "Source IP Address",
"type": "string",
"key": "1",
"display": "ip"
}
]
}
],
"summary": [
{
"timestamp": "1331192727",
"protocol": "HTTP",
"activity": "www.google.com",
"results": "OK",
"point": {
"k1": "1",
"c1": "true",
"c2": "true"
}
},
{
"timestamp": "1331192727",
"protocol": "DNS",
"activity": "www.google.com",
"results": "OK",
"point": {
"k1": "1",
"k2": "1.1.4.229"
}
}
]
},
"progress": {
"perc": "100"
},
"more": "13,39,1331192727,1331192760,27236,1.1.4.229,limit=1000,session=sessionname"
}
Thank you for any advice you can provide.
-D Klotz
With GSON, assuming that the class you are deserializing into has fields for all the names that appear in the JSON, the fields not found in the JSON will just be left null:
https://sites.google.com/site/gson/gson-user-guide#TOC-Finer-Points-with-Objects
“While deserialization, a missing entry in JSON results in setting the corresponding field in the object to null”
Things get a little more complicated if arbitrary field names are allowed in the JSON – for example, if Point allows c1, c2, … cn. But you can handle this with a custom deserializer.
https://sites.google.com/site/gson/gson-user-guide#TOC-Writing-a-Deserializer
Edit:
Here’s how you might write a custom deserializer for Point: