JsonParser. When no target type is provided, the parser will convert JSON string to Java
List. Like this:
Mapthat contains 2 keys. One of the keys (
one) holds another
Mapinstance. As you can see from the example, simple JSON data types are converted into their counter-part Java types. Boolean value is converted to
Boolean, number is converted to a
JsonParserfinds the best possible type; for example, if number can be stored into an
Integeris returned, but if it is a decimal value then
Doubleis returned and so on.
nameis a simple property. But
JsonParser. It will parse inner JSON maps to
Bartypes, since the generic type of the property specifies the map's content type. In the same way
JsonParsercan handle the
JsonParsernow converts JSON directly to a target type. Let's remember:
JsonParsercan parse complex JSON strings into Java object graph, as long it can resolve the type information.
Interis an interface:
JsonParserthat can't figure out by looking at the
intersproperty types and its generic information. We need to explicitly specify the target type for maps values. As you could guess, we can use a path to specify the mapping. But in this case, the path needs to address the values of the map! No problem - by using a special path name
valuewe can address all the values of a map:
value, we could specify the key's type, using the path reference
keys. Look at the following example:
Pairof two different types.
keysto reference ALL values of a map or ALL keys of a map (or of an array). But you can not change the type of particular map value, since these special paths address ALL items.
.useAltPaths()we are telling
JsonParserto match paths to current map values! By default, this option is disabled, for performance reasons (there is some penalty because more paths are matched).
__class. If you have such JSON or if you have used this option with
JsonSerializer, you can enable this feature with
classthat holds the full class name of the target. But be careful:
Mapso we can fetch the class name and then converted to a target class. Because of this double conversion expect performance penalties if using class metadata name.
allowClass(). If the class name does not match one of set wildcard patterns,
JsonParserwill throw an exception. To reset the whitelist, use
TypeConverterto convert between strings to real types.
Datemay be specified as a string in
yyyy/MM/ddformat and not as a number of milliseconds. So we need to explicitly convert the string into
Date. For that, we can use
JsonParsermay run in so-called loose mode, when it can process more:
JsonParserin loose mode may parse the following input:
Listi.e. in the lazy mode, there is no sense to parse to concrete types. The returned object is lazy since the parsing happens only on key retrieval.