Stable/Terministic comparison of JSON objects in Jackson and GSON - java

As it turns out Jackson does not do stable JSON object comparison contrary to this question. So I was wondering if GSON has stable comparison of JSON objects. (without having to override equals/implement one's own comparator)

Your gist shows org.json code, not Jackson.
Jackson has a perfectly able .equals() implementation for all JsonNodes. And that includes all "non container types" as well as "container types":
final JsonNodeFactory factory = JsonNodeFactory.instance;
final JsonNode node1 = factory.objectNode().put("hello", "world");
final JsonNode node2 = factory.objectNode().put("hello", "world");
node1.equals(node2); // true
Of course, it does respect JSON's "order does not matter" with object members: { "a": 1, "b": 2 } is equal to { "b": 2, "a": 1 } -- as it should.
That may only be my opinion as well, but really, when it comes to JSON, anything is better than org.json.

Related

Using GSON to parse Json into a JAVA Object where the Json Elements may vary

I am trying to parse a JSON .txt file into a JAVA object using GSON. The JSON file has the following structure:
{
"event0" : {
"a" : "abc",
"b" : "def"
},
"event1" : {
"a" : "ghi",
"b" : "jkl",
"c" : "mno"
}
}
I have read the text file into a String called dataStr. I want to use the fromJson method to capture the events into the following JAVA class:
public class Event {
private String a;
private String b;
private String c;
public Event() {}
}
The problem is that the JSON might have one extra field "c" in some of the elements. I want to parse all the events into Event class objects, and for the cases where there is no "c" field, I want to make it null or zero in my object. It is not known beforehand which of the elements will have the "c" field.
Specifically, I was not able to figure out how to handle one extra field in some of the JSON elements. I want to do something along the lines of:
Gson gson = new Gson();
ArrayList<Event> events = gson.fromJson(dataStr, Event.class);
But I am stuck with first, how to iterate over the events in the Json file, and secondly, how to handle some occasional missing fields into the same Event object. I would really appreciate a kick in the right direction. Thank you all.
I am fairly new to JSON parsing, and might have missed something in the following answers:
Using Gson to convert Json into Java Object
Mapping JSON into POJO using Gson
Using gson to parse json to java object
Parse JSON into a java object
How to parse a json file into a java POJO class using GSON
I'm not sure if I understood your question right. As per my understanding, you are trying to convert a json object with an extra field which is not available in the java class. Frankly, I don't understand why you want that or if it's possible to start with. You can have a workaround by converting the json to Map.
Map map = gson.fromJson(jsonString, Map.class);
Gson automatically do that for you.
So, if you have a class "Alpha" with 3 fields ("a", "b" and "c") and you try to work on a json object that has 2 fields with names that match with Alpha's "a" and "b", Gson will fill "a" and "b" with json file's value and "c" will automatically set as null.
So, in your case, if you write this:
ArrayList<Event> events = gson.fromJson(dataStr, Event.class);
And in your json there are events with only 2 fields (that match with any Event's class fields) and events with all fields set, you will get a list of Events with no errors. Maybe you'll get some fields null, but the code will work.
I hope to be helpful! Ask for further informations, if you want to!
EDIT
Note that your json file has not to be .txt but .json instead!
First I believe your JSON should look like this:
{
"events": [
{
"name": "event0",
"a": "abc",
"b": "def"
},
{
"name": "event1",
"a": "abc",
"b": "def",
"c": "mno"
}
]
}
This will need two classes for your model:
public List<Event> events = null;
public class Event {
public String name;
public String a;
public String b;
public String c;
}
And then then with GSON
Events events = gson.fromJson(jsonData, Events.class);
Also I recommend to always use an online validator for JSON so you are sure your JSON structure is correct before coding against it.
https://jsonlint.com/
Or for formate the JSON:
http://jsonprettyprint.com/
Also this website can create the Java classes for you from either a JSON Schema or by using an example file.
http://www.jsonschema2pojo.org/
Try the below code snippet:
Gson gson = new Gson();
ArrayList<Event> events = gson.fromJson(dataStr, new TypeToken<ArrayList<Event>>(){}.getType());
In the source code of Gson has a very clear explain

Filter out fields from a JsonNode

I have a Jackson JsonNode of sub-type ObjectNode:
ObjectNode node = parent.path('somepath');
node has a number of sub-fields, such as you'd see in this json object:
{
"somepath": {
"a": 1,
"b": 2,
"c": 3,
"d": 4
}
}
So the above object node will have four sub-objects (all JsonNode/ObjectNodes in their own right): a, b, c and d.
Given object node, I'd like to filter out some of the subfields. For instance, let's say I'd like to filter out everything but some list of field names, say ["b", "c"]. When I re-serialize the node object it would look like this:
{
"somepath": {
"b": 2,
"c": 3
}
}
I can think of a lot of ways where I can loop through both the field name list and the keep list, and rebuild the object, but that all seems like a lot of work and very unclear. What I'd love to use is a Stream.filter() type of function:
List<String> keepList = Lists.newArrayList("b", "c");
node.stream().filter( field -> keepList.contains(field.name()));
Obviously the above code doesn't work because I can't 'stream' an ObjectNode. Is there a way I can get this to work in a similar fashion, or am I stuck going the long way around?
There is a method in ObjectNode that does exactly what you want: retain. You might use it this way:
ObjectNode node = parent.path('somepath');
node = node.retain(keepList);

Jackson Unmarshall custom object instead of LinkedHashMap

I have a Java object Results:
public class MetaData {
private List<AttributeValue<String,Object>> properties
private String name
...
... getters/setters ...
}
The AttributeValue class is a generic key-value class. It's possible different AttributeValue's are nested. The (value) Object will then be another AttributeValue and so forth.
Due to legacy reasons the structure of this object cannot be altered.
I have my JSON, which I try to map to this object.
All goes well for the regular properties. Also the first level of the list is filled with AttributeValues.
The problem is the Object. Jackson doesn't know how to interpret this nested behavior and just makes it a LinkedHashMap.
I'm looking for a way to implement custom behavior to tell Jackson this has to be a AttributeValue-object instead of the LinkedHashMap.
This is how I'm currently converting the JSON:
ObjectMapper om = new ObjectMapper();
MetaData metaData = om.readValue(jsonString, new TypeReference<MetaData>(){});
And this is example JSON. (this is obtained by serializing an existing MetaData object to JSON, I have complete control over this syntax).
{
"properties":[
{
"attribute":"creators",
"value":[
{
"attribute":"creator",
"value":"user1"
},{
"attribute":"creator",
"value":"user2"
}
]
},{
"attribute":"type",
"value": "question"
}
],
"name":"example"
}
(btw: I've tried the same using GSON, but then the object is a StringMap and the problem is the same. Solutions using GSON are also welcome).
edit In Using Jackson ObjectMapper with Generics to POJO instead of LinkedHashMap there is a comment from StaxMan:
"LinkedHashMap is only returned when type information is missing (or if Object.class is defined as type)."
The latter seems to be the issue here. Is there a way I can override this?
If you have control over the serialization, try calling enableDefaultTyping() on your mapper.
Consider this example:
Pair<Integer, Pair<Integer, Integer>> pair = new Pair<>(1, new Pair<>(1, 1));
ObjectMapper mapper = new ObjectMapper();
String str = mapper.writeValueAsString(pair);
Pair result = mapper.readValue(str, Pair.class);
Without enableDefaultTyping(), I would have str = {"k":1,"v":{"k":1,"v":1}} which would deserialize to a Pair with LinkedHashMap.
But if I enableDefaultTyping() on mapper, then str = {"k":1,"v":["Pair",{"k":1,"v":1}]} which then perfectly deserializes to Pair<Integer, Pair<...>>.

Getting a SubArray of Fasterxml Jackson ArrayNode

I need to get a sub-array of an ArrayNode object in fasterxml jackson in Java.
To be more clear,
I have a fasterxml jackson ArrayNode object which contains for example let's say 100 objects.
I have a limit parameter for example let's say 5.
Can do it in a very primitive way as indicated below,
ArrayNode arrayNodeRecProducts = (ArrayNode) recProducts;
int arrayNodeSize = arrayNodeRecProducts.size();
if (limit >= 0 && limit < arrayNodeSize) {
while (arrayNodeRecProducts.has(limit)) {
arrayNodeRecProducts.remove(limit);
}
}
The "recProducts" object casted to ArrayNode type is a fasterxml jackson JsonNode and contains an array.
Above works but quite inefficient as the inner while loop runs for "arrayNodeSize - limit" number of times in removing the ArrayNode objects one by one.
Is there a sub-array operation which we can perform on the ArrayNode or the casted JsonNode itself?
Thanks and Regards..
Thanks "henrik" for your answer and you were correct in that Jackson doesn't support such a functionality for ArrayNodes. So what I did was I downloaded the Jackson databind codebase and looked inside the hood why they are not providing such a SubArray functionality for ArrayNodes (Please be informed that I am referring to databind 2.3.2).
Internally, Jackson is maintaining the ArrayNode in a JsonNode List as below,
private final List<JsonNode> _children = new ArrayList<JsonNode>();
To my surprise, for some reason I cannot understand, they have not provided a SubArray operation which could be easily accomplished by using the subList method of this contained list. For example as below,
public List<JsonNode> subArray(int fromIndex, int toIndex) {
return _children.subList(fromIndex, toIndex);
}
Above method would have saved me from the trouble I was facing but it is simply not included in the library.
So what I did in my codebase is to simply hack into this private list in runtime using reflection and invoke the subList operation at runtime as below.
ArrayNode arrayNodeRecProducts = (ArrayNode) recProducts;
if (limit >= 0 && limit < arrayNodeRecProducts.size()) {
Field innerArrayNode = ArrayNode.class.getDeclaredField("_children");
innerArrayNode.setAccessible(true);
List<JsonNode> innerArrayNodeChildNodes = (List<JsonNode>) innerArrayNode.get(arrayNodeRecProducts);
List<JsonNode> limitedChildNodes = innerArrayNodeChildNodes.subList(0, limit);
innerArrayNode.set(arrayNodeRecProducts, limitedChildNodes);
}
I know that the above code will not work in all situations but for my situation it is working fine.
At the same time, I know this is a violation of our well guarded OO principle Encapsulation, but I can live with that for reasons explained in below post.
Dosen't Reflection API break the very purpose of Data encapsulation?
There is no such support build in Jackson.
I would create a new ArrayNode and fill it until the limit, that would be more efficient in most cases:
ArrayNode limited = objectMapper.createArrayNode();
for(JsonNode e : src) {
limited.add(e);
if (limited.size() == limit) {
break;
}
}

Jackson how to transform JsonNode to ArrayNode without casting?

I am changing my JSON library from org.json to Jackson and I want to migrate the following code:
JSONObject datasets = readJSON(new URL(DATASETS));
JSONArray datasetArray = datasets.getJSONArray("datasets");
Now in Jackson I have the following:
ObjectMapper m = new ObjectMapper();
JsonNode datasets = m.readTree(new URL(DATASETS));
ArrayNode datasetArray = (ArrayNode)datasets.get("datasets");
However I don't like the cast there, is there the possibility for a ClassCastException?
Is there a method equivalent to getJSONArray in org.json so that I have proper error handling in case it isn't an array?
Yes, the Jackson manual parser design is quite different from other libraries. In particular, you will notice that JsonNode has most of the functions that you would typically associate with array nodes from other API's. As such, you do not need to cast to an ArrayNode to use. Here's an example:
JSON:
{
"objects" : ["One", "Two", "Three"]
}
Code:
final String json = "{\"objects\" : [\"One\", \"Two\", \"Three\"]}";
final JsonNode arrNode = new ObjectMapper().readTree(json).get("objects");
if (arrNode.isArray()) {
for (final JsonNode objNode : arrNode) {
System.out.println(objNode);
}
}
Output:
"One"
"Two"
"Three"
Note the use of isArray to verify that the node is actually an array before iterating. The check is not necessary if you are absolutely confident in your datas structure, but its available should you need it (and this is no different from most other JSON libraries).
In Java 8 you can do it like this:
import java.util.*;
import java.util.stream.*;
List<JsonNode> datasets = StreamSupport
.stream(obj.get("datasets").spliterator(), false)
.collect(Collectors.toList())
I would assume at the end of the day you want to consume the data in the ArrayNode by iterating it. For that:
Iterator<JsonNode> iterator = datasets.withArray("datasets").elements();
while (iterator.hasNext())
System.out.print(iterator.next().toString() + " ");
or if you're into streams and lambda functions:
import com.google.common.collect.Streams;
Streams.stream(datasets.withArray("datasets").elements())
.forEach( item -> System.out.print(item.toString()) )
Is there a method equivalent to getJSONArray in org.json so that I have proper error handling in case it isn't an array?
It depends on your input; i.e. the stuff you fetch from the URL. If the value of the "datasets" attribute is an associative array rather than a plain array, you will get a ClassCastException.
But then again, the correctness of your old version also depends on the input. In the situation where your new version throws a ClassCastException, the old version will throw JSONException. Reference: http://www.json.org/javadoc/org/json/JSONObject.html#getJSONArray(java.lang.String)
Obtain an iterator by calling the JsonNode's iterator() method, and go on...
JsonNode array = datasets.get("datasets");
if (array.isArray()) {
Iterator<JsonNode> itr = array.iterator();
/* Set up a loop that makes a call to hasNext().
Have the loop iterate as long as hasNext() returns true.*/
while (itr.hasNext()) {
JsonNode item=itr.next();
// do something with array elements
}
}

Categories