Package org.kopitubruk.util.json

Provides utilities to convert objects into JSON and parse JSON into Java objects.

See: Description

Package org.kopitubruk.util.json Description

Provides utilities to convert objects into JSON and parse JSON into Java objects. The JSONUtil class is an alternative to the org.json package.

Instead of creating its own maps for objects or lists for arrays, this package allows you to use any Map you like, allowing for iterations on your Map to be predictable by using a TreeMap or a LinkedHashMap which can be useful for debugging. You can also use any Iterable object or Enumeration to create a Javascript array or even use an actual array of objects or primitives. In many cases it may be possible to use existing data structures without modification.

This package optionally supports reflection so that you don't have to use a Map at all. You can choose which types of objects to use reflection on with JSONConfig.addReflectClass(Object) or JSONConfig.addReflectClasses(Collection) or you can have it use reflection on all unrecognized objects by using JSONConfig.setReflectUnknownObjects(boolean). The privacy level of fields to show with reflection can be controlled by using JSONConfig.setPrivacyLevel(int). By default only fields which are public or which have public getters will be included, but protected, package private and private can be included by using that setting.

There is an interface provided called JSONAble which enables marking classes that can convert themselves to JSON and when those are encountered as the values in a Map, Iterable, Enumeration, array or reflected class, then their JSONAble.toJSON(JSONConfig,Writer) method will be called to add them to the output.

Maps, Iterables, Enumerations, arrays and reflected classes are traversed, allowing the creation of complex graphs of JSON data with one call. JSONAbles may also be traversed if the JSONAble object implements complex data structures within itself and uses this package to generate its own JSON.

There is loop detection which attempts to avoid infinite recursion, but there are some ways to get past the detection, particularly with toString() methods in non-JSONAble objects, or with JSONAbles which do not properly pass their JSONConfig object along so care should still be exercised in avoiding loops in data structures.

There are a number of configuration options to allow you to disable validation and loop detection, change some of the character escape behavior and even generate certain types of non-standard JSON which may work if you're using a Javascript eval() on it rather than a strict JSON parser.

ECMAScript 6 support is available by enabling it in JSONConfig. This causes ECMAScript 6 code point escapes to be recognized as well as generated when it saves characters over code unit escapes. It also allows a larger set of characters in identifiers as per the ECMAScript 6 standard.

There is some support for arbitrary precision numbers. All Javascript numbers are internally 64-bit double precision floating point and all numbers eventually get converted to that format by default by whatever parses the JSON. If you set JSONConfig.setPreciseNumbers(boolean) to true then numbers which cannot be accurately represented by 64-bit double precision floating point will be quoted in the output. This allows those string representations of those numbers to be fed to an arbitrary precision package constructor to maintain the original precision. There are several arbitrary precision math packages available for Javascript. JSONParser creates BigInteger and BigDecimal whenever it encounters numbers that lose precision in Long or Double respectively.

This package uses Apache Commons Logging facade in a few places so it should work with whatever logging framework you're using, but most of the messages are debug level so you shouldn't see them unless you enable debug for the package/classes. They are all related to JNDI lookups or MBean access so if you're having trouble with those, you may want to enable debug for this package.

The JSONParser class parses JSON data. It converts Javascript objects into LinkedHashMaps and Javascript arrays into ArrayLists. It's a "loose" parser in that it accepts valid Javascript syntax for objects and arrays, numbers and strings so it is less strict than the JSON standard.

Copyright © 2016. All rights reserved.