quick-json is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. quick-json can work with any arbitrary Java objects.
This parser supports validating/non-validating versions.
Non-Validating parser verifies the standards of json while parsing and will not validate keys/values and will not apply any custom validations at all.
Validating parser version is a strict parser and it does parsing based upon pre-configured validation rules.
This Parser is a simple and flexible parser for parsing input JSON string and return the corresponding java collection representation.
1. quick-json Parser
quick-json is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert
a JSON string to an equivalent Java object. quick-json can work with any arbitrary Java objects.
This parser supports validating/non-validating versions.
Non-Validating parser verifies the standards of json while parsing and will not validate keys/values and will not apply any custom
validations at all.
Validating parser version is a strict parser and it does parsing based upon pre-configured validation rules.
This Parser is a simple and flexible parser for parsing input JSON string and return the corresponding java collection
representation.
e.g.
{
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
Now Map generated out of the above mentioned JSON String will look like,
A map with one key as 'samplejson' and its value as another map of key/value pairs. i.e. it will be very easy for the developers to
look up for a specific key within json hierarchy.
Value can be looked up by just parsing the above JSON string using parsing API as follows,
JsonParserFactory factory=JsonParserFactory.getInstance();
JsonParser parser=factory.newJsonParser();
Map jsonData=parser.parse(inputJsonString);
String value=(String)jsonData.get("key2");
Simple examples of json,
E.g.1
[{
"key":"value",
"key1":[234234.32,adfasdf],
"key2":[arrayvalue,arrayvalue1,arrayvalue2]
},
{
"key":{
subjsonkey:subjsonvalue
}
},
"Rajesh Putta",
234234.23]
3. "vOffset": 100,
"alignment": "center",
"onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
}
}}
quick-json features
# Compliant with JSON specification (RFC4627)
# High-Performance JSON parser
# Supports Flexible/Configurable parsing approach
# Configurable validation of key/value pairs of any JSON Heirarchy
# Easy to use # Very Less foot print
# Raises developer friendly and easy to trace exceptions
# Pluggable Custom Validation support - Keys/Values can be validated by configuring custom validators as and when
encountered
# Validating and Non-Validating parser support
# Support for two types of configuration (JSON/XML) for using quick-json validating parser
# Require JDK 1.5 (1.5.0_22_1) # No dependency on external libraries
# Support for Json Generation through object serialization
# Support for collection type selection during parsing process
quick-json Non-Validating parser
Usage
quick-json Non-Validating parser supports parsing of different formats of json strings which are compliant with JSON spec. Parser
throws developer friendly exceptions in case of there is any syntax/semantic issues.
E.g.
samplejson = {
"key1": value1,
"key_2": "value2",
key3: value3,
key4:234234234,
key5:234234.233
}
4. Value can be looked up by just parsing the above JSON string using parsing API as follows,
JsonParserFactory factory=JsonParserFactory.getInstance();
JsonParser parser=factory.newJsonParser();
Map jsonData=parser.parseJson(inputJsonString);
//lookup the key 'key_2' under Json Block 'samplejson'
String value=jsonData.get("samplejson").get("key_2");
{
"employees": [
{ "firstName":"Rajesh" , "lastName":"Putta" },
{ "firstName":"Rajesh" , "lastName":"P" },
{ "firstName":"first name" , "lastName":"last name" }
]
}
Value can be looked up by just parsing the above JSON string using parsing API as follows,
JsonParserFactory factory=JsonParserFactory.getInstance();
JsonParser parser=factory.newJsonParser();
Map jsonData=parser.parseJson(inputJsonString);
Map rootJson=jsonData.get("root");
List al=rootJson.get("employees");
String fName=((Map)al.get(0)).get("firstName");
String lName=((Map)al.get(0)).get("lastName");
quick-json Validating parser Usage
quick json validating parser can be used whenever json has to be validated at every level to see if the format is expected or not.
Keys/Values, Sub-Json, Array and its elements can be validated.
What/When/Where needs to be validated ? All this information is expected to be passed to the parser during initialization.
Below is the sample way of initializing and working with validating parser.
JsonParserFactory factory=JsonParserFactory.getInstance();
JsonParser parser=factory.newJsonParser();
//initialize parser with the json validation configuration file stream
parser.initialize(xml_stream));
//configure parser for validating
parser.setValidating(true);
//parse input json string with validating parser instance
Map jsonData=parser.parseJson(inputJsonString);
E.g.1
Input JSON String
sample={
"key":"value",
"key1":"value1",
"key2":234234234
}
5. Validation configuration for the above JSON string would be,
<json-config>
<json-heirarchy path="root">
<KeyValues>
<KeyValue name="ROOT" valueType="JSON"/>
</KeyValues>
</json-heirarchy>
<json-heirarchy path="sample">
<KeyValues>
<KeyValue name="default" valueType="STRING_LITERAL"/>
<KeyValue name="key2" valueType="INTEGER"/>
</KeyValues>
</json-heirarchy>
</json-config>
root configuration is for validating the root level, this is mandatory to validate the root level is JSON/JSON_ARRAY
sample configuration is for the key/value pairs under sample json string. As per the above configuration, default configuration is
applicable for all the key/value pairs whenever there is no specific validation configured for keys. In this case key2 requirement is
to have separate validation configuration. so overriding the default one.
Validation data types
JSON validates for JSON presence
JSON_ARRAY validates for JSON_ARRAY presence
STRING_LITERAL validates for double quoted string presence
STRING Validates for simple string with alphanumeric data
INTEGER Validates for number data, doesnt allow double type of values
DOUBLE Validates for number data, allows double type of values
BOOLEAN Validates for boolean value (true or false)
NULL Validates for null
E.g.2
Input JSON String
sample={
"key":"value",
"key1":["value1",234234.32,adfasdf],
"key2":234234234
}
Validation Configuration for the above json string would be,
<json-config>
<json-heirarchy path="root">
<KeyValues>
<KeyValue name="ROOT" valueType="JSON"/>
</KeyValues>
</json-heirarchy>
<json-heirarchy path="sample">
<KeyValues>
<KeyValue name="default" valueType="STRING_LITERAL"/>
<KeyValue name="key1" valueType="JSON_ARRAY"/>
6. <KeyValue name="key2" valueType="INTEGER"/>
</KeyValues>
</json-heirarchy>
<json-heirarchy path="sample/key1">
<KeyValues>
<KeyValue name="default" valueType="STRING_LITERAL"/>
<KeyValue index="1" valueType="DOUBLE"/>
<KeyValue index="2" valueType="STRING"/>
</KeyValues>
</json-heirarchy>
</json-config>
quick-json Validating parser Usage -
with JSON based validation
configuration
quick json validating parser can be configure with JSON based validation configuration whenever other json has to be validated at
every level to see if the format is expected or not. Keys/Values, Sub-Json, Array and its elements can be validated.
What/When/Where needs to be validated ? All this information is expected to be passed to the parser during initialization.
Below is the sample way of initializing and working with validating parser with JSON based validation configuration.
JsonParserFactory factory=JsonParserFactory.getInstance();
JSONParser parser=factory.newJsonParser(ValidationConfigType.JSON);
parser.initializeWithJson(json_based_validation_config_stream);
parser.setValidating(true);
//parse input json string with validating parser instance
Map jsonData=parser.parseJson(inputJsonString);
E.g.1
[{
"key":"value",
"key1":[234234.32,true ],
"key2":[arrayvalue,arrayvalue1,arrayvalue2]
},
{
"key":{
subjsonkey:subjsonvalue
}
},
"Rajesh Putta",
234234.23]
Below would be the JSON based validation configuration,
{
"root":{
"ROOT":{valueType:"JSON_ARRAY"},
"0":{valueType:"JSON"},
"1":{valueType:"JSON"},
8. set.add(234);
set.add(2343);
set.add("asdfasfd");
set.add(tp);
data.put("set-pojo", set);
data.put("objectarray", new Object[]{submap,set,submap,tp});
Here is the implementation for serializing the above data set
JsonGeneratorFactory factory=JsonGeneratorFactory.getInstance();
JSONGenerator generator=factory.newJsonGenerator();
String json=generator.generateJson(data);
Here is the serialized data object,
[{"age":28,"doublearray":[234.0,555.0,777.0,888.0],"floatarray":[1,2,34,35],"set-pojo":["asdfasfd",
{"name":"Rajesh","age":28,"ss":3223.33,"data":{"2":20,"1":10},},2343,234],"subdata":
{"3":"3","2":"2","1":"1"},"objectarray":[{"2":20,"1":10},{"2":20,"1":10},
{"name":"Rajesh","age":28,"ss":3223.33,"data":{"2":20,"1":10},}],"name":"Rajesh Putta","chararray":
[a,b,c],"city":"hyderabad","array":[234,555,777,888]}]