2

I'm trying to dynamically parse a lot of data in Java. In other words I have tons of different types of data so I am putting them in a String or a JSON String (via GSON) instead of doing:

switch(data){
case instanceof Class1:
     Class1 data = gson.fromJson(jsonString);
     //Do Stuff for class 1 layout
case instanceof Class2:
     Class2 data = gson.fromJson(jsonString);
etc.

So instead of doing this for 80 or so classes (which more may be added/removed/changed at any given time) I am planning to put the data in either a String or Json String and parse out the values (depth first).

I am on a low end PC (single core Atom processor) and am trying to reduce the amount of taxing I put on the CPU, so determining which would be faster... regex string manipulation with splits or using a recursive JSON parser.

Tacitus86
  • 1,314
  • 2
  • 14
  • 36

1 Answers1

1

Let us discuss both the cases you have mentioned here:

CASE 1: Creating instances for each and every json input using gson (mapped to a Class)

and

CASE 2: Create a Document (or similar type object) itself and try accessing data from there.

For Case 2, you don't need to write a parser yourself: there are parsers already available. I'll just write down a jackson example here (There must be similar stuff available with gson as well):

String myJson = "{ \"type\" : \"foo\", \"class\" : 3 }";
ObjectMapper objectMapper = new ObjectMapper();
JsonNode node = objectMapper.readValue(myJson, JsonNode.class);
JsonNode type = node.get("type");
System.out.println(type.asText());

As per my experience, the performance difference hasn't been much in both these cases as the libraries handle the parsing quite efficiently but if you have a lot of different types of JSON, then it doesn't make any sense to create POJOs out of each and every JSON (So much mapping !!).

I have personally not worked with gson because of performance reasons like this but you can create what's called an ObjectMapper using jackson and use it quite efficiently. I assume there should be similar thing in gson as well.

The only downside would be that every field will now be accessible through a string input which might make your code a bit unreadable.

EDIT:

If you really wish to iterate through all the fields of the json, you'll have to do DFS ultimately, but parsing can still be avoided by using the fields method of the JsonNode.

root.fields() will return with an iterator with the entries in it which can be then gracefully used as described in answer here.

Remember, similar stuff with different names will be available in gson too. :)

Parijat Purohit
  • 921
  • 6
  • 16
  • You are still going to have to create an instance of each class type. And you have to know the keys of the JSON. I am getting a JSON string that I know nothing about. I want to be able to handle that. – Tacitus86 Oct 27 '17 at 19:09
  • While handling, you will have to know the keys right? Or else, if you go through the ObjectMapper (No fan of jackson but I dont know how gson works), you won't have to declare the classes is what I am saying, since your JSON structure might change... You can't go and create classes for every new JSON is what I thought is causing you the problem. Also, You can actually iterate through the nodes, their values, arrays, etc. and basically access the object without having a rigidity on the JSON structure (No need to map it to a class anymore). – Parijat Purohit Oct 27 '17 at 19:12
  • fields() iterator can be used as well, btw.. if you wish to go through all the nodes. Handling JSON with java is a it ugly. – Parijat Purohit Oct 27 '17 at 19:15
  • Well I mean you don't really know or care about the keys. You know you just have to iterate over all the items in the JSON be it object, array, primitive whatever. I just want to get the String value of each item, I don't care what the key is in this scenario. – Tacitus86 Oct 27 '17 at 19:18
  • 1
    Perhaps I just don't know what a JsonNode is. I thought that equated to Class1 in my example. But if I could use node to iterate over the JSON without knowing the keys... – Tacitus86 Oct 27 '17 at 19:20
  • 1
    Yes, you can use the `fields()` method to do exactly that. Also, got https://stackoverflow.com/questions/11428329/using-gson-to-deserialize-specific-json-field-of-an-object for gson as well :D – Parijat Purohit Oct 27 '17 at 19:28
  • Thanks Parijat. I think this covers how to do it in GSON. https://stackoverflow.com/questions/27964498/how-to-iterate-a-jsonobject-gson – Tacitus86 Oct 27 '17 at 19:38
  • @Tacitus86 glad I could help :) – Parijat Purohit Oct 27 '17 at 19:38