Map to JSON converter and JSON to Map parser with support JSON5 https://spec.json5.org/
By simple:
What is a json-object? This is a string representation of javascript object.
What is a javascript-object? This is a associative array where the string as key and any object as value.
The most similar structure in the java is a Map<String, Object>. This is natural json representation, and this is very useful for debug, research, and in support of production system.
- Java 1.6
- Not have any other dependency
Suppose we have the json5-string:
{
// comments
unquoted: 'and you can quote me on that',
singleQuotes: 'I can use "double quotes" here',
lineBreaks: "Look, Mom! \
No \\n's!",
hexadecimal: 0xdecaf,
leadingDecimalPoint: .8675309, andTrailing: 8675309.,
positiveSign: +1,
trailingComma: 'in objects', andIn: ['arrays',],
"backwardsCompatible": "with JSON",
}
In a code:
import a2u.tn.utils.json.JsonParser;
...
Map<String, Object> result = JsonParser.parse(json);And we get this result :
LinkedHashMap: result {
unquoted -> Stirng: "and you can quote me on that"
singleQuotes -> Stirng: "I can use \"double quotes\" here"
lineBreaks -> Stirng: "Look, Mom! \nNo \\n's!"
hexadecimal -> Integer: 912559
leadingDecimalPoint -> Double: 0.8675309
andTrailing -> Double: 8675309.0
positiveSign -> Integer: 1
trailingComma -> Stirng: "in objects"
andIn -> ArrayList:[ Stirng: "arrays" ] size = 1
backwardsCompatible -> Stirng: "with JSON"
}
By default in this parsing for collections using LinkedHashMap and ArrayList. This is very useful for debug.
If you unlike LinkedHashMap or ArrayList, you can use method
public static Map<String, Object> parse(String data, IGetCollection listener)
For example:
String json;
Map<String, Object> result;
json = "{obj1:{num1:123, obj2:{list:[456, 789]}}}";
result = JsonParser.parse(json,
new JsonParser.IGetCollection() {
@Override
public Map<String, Object> forObject(String path) {
if (path.equals("root.obj1.obj2")) {
return new HashMap<String, Object>();
}
return null;
}
@Override
public Collection forList(String path) {
if (path.equals("root.obj1.obj2.list")) {
return new HashSet();
}
return null;
}
});Suppose we must generate next json:
{
"num": 123,
"str": "str",
"innermap": {
"innernum": 345
},
"list1": [789, 987],
"list2": [{
"innernum": 345
},
{
"innernum": 345
}]
}import a2u.tn.utils.json.JsonSerializer; Map<String, Object> map = new LinkedHashMap<String, Object>();
map.put("num", 123);
map.put("str", "str");
Map<String, Object> innermap = new LinkedHashMap<String, Object>();
innermap.put("innernum", 345);
map.put("innermap", innermap);
List<Object> list1 = new ArrayList<Object>();
list1.add(789);
list1.add(987);
map.put("list1", list1);
List<Object> list2 = new ArrayList<Object>();
list2.add(innermap);
list2.add(innermap);
map.put("list2", list2); String json = JsonSerializer.toJson(map);It's all.