DEV Community

Discussion on: Handling unknown JSON structures

Collapse
 
alainvanhout profile image
Alain Van Hout

A similar approach, using Gson, is to use GSON.fromJson(yourstring, HashMap.class), which will converts it to a structure of maps, lists and primitives.

Collapse
 
sorincostea profile image
Sorin Costea

Not if in the string there's an array, or has nested structures, basically if you don't know what you received. My point was, with the above you can safely poke around unknown JSON structures.

Collapse
 
alainvanhout profile image
Alain Van Hout

That's also possible with map-based parsing: it parses a JSON array to a List, mostly at whichever nested depth. You of course need to do type checking, but that's equally true for the the other approach, since calling getAsInt() on a string also inherently has its costs.

Thread Thread
 
sorincostea profile image
Sorin Costea

For my lazy use case returning null (or providing a default) instead of try/catching covered 100%. Anyway vert.x solution is just a thin layer over Jackson so it's not like they reinvented the wheel... just simple things made even simpler.