A utility class containing a number of static methods to read and write JSON text.
- No external dependencies, the implementation only uses standard Java.
- Parsing to generic Java Map/List instances.
- Optional support to get/put values in the nested map data structure by using path expressions. This is only a convenience, you can use the nested Maps directly as well.
- Thoroughly profiled and tested.
Steps:
- Create a nested map structure using the utility methods and path expressions.
- Render the nested map to JSON text.
// Create a map.
//
Map<String, String> testMap = new LinkedHashMap<String, String>();
JsonUtil.putObjectInMap("lastName", map, "Ranschaert");
JsonUtil.putObjectInMap("firstName", map, "Bruno");
// Render to JSON.
//
String json = JsonUtil.convertToJson(testMap);
This will produce something like:
{"lastName":"Ranschaert", "firstName":"Bruno"}
Steps:
- Parse the JSON text to a nested map structure.
- Interrogate the nested map structure using the path expressions.
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}
// Parse the JSON text into a map.
//
Map<String, Object> map = (Map<String,Object>) JsonUtil.parseJson(example);
// Interrogate the map (using path expressions).
//
JsonUtil.getStringFromMap("glossary.title", map);
JsonUtil.getObjectFromMap("glossary.GlossDiv.GlossList.GlossEntry.GlossDef.GlossSeeAlso", map);
JsonUtil.getStringFromMap("glossary.GlossDiv.GlossList.GlossEntry.GlossDef.GlossSeeAlso[1]", map);
- Pretty printing flag.
- The JSON text can contain single quotes or double quotes to delimit the strings. Single quotes can be useful when the JSON text is embedded in Java to reduce backslash clutter.
- Parsing is very fast.
The project is Maven based, it contains a single utility class and its corresponding JUnit test.
mvn clean install