Example usage for org.apache.commons.scxml2.io ContentParser parseJson

List of usage examples for org.apache.commons.scxml2.io ContentParser parseJson

Introduction

In this page you can find the example usage for org.apache.commons.scxml2.io ContentParser parseJson.

Prototype

public Object parseJson(final String jsonString) throws IOException 

Source Link

Document

Parse and map JSON string to 'raw' Java Objects: object -> LinkedHashMap, array -> ArrayList

Usage

From source file:org.apache.commons.scxml2.io.ContentParserTest.java

@Test
public void testParseJson() throws Exception {
    ObjectMapper jsonObjectMapper = new ObjectMapper();
    jsonObjectMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
    jsonObjectMapper.configure(JsonParser.Feature.ALLOW_YAML_COMMENTS, true);
    // not by default configured, but much easier for unit-testing Java embedded JSON Strings
    jsonObjectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);

    ContentParser contentParser = new ContentParser(jsonObjectMapper);

    String jsonObjectString = "{ /*comment*/ 'string' : 'foobar', 'int' : 1, 'boolean' : false, 'null' : null }";
    LinkedHashMap<String, Object> jsonObject = new LinkedHashMap<>();
    jsonObject.put("string", "foobar");
    jsonObject.put("int", new Integer(1));
    jsonObject.put("boolean", Boolean.FALSE);
    jsonObject.put("null", null);
    Assert.assertEquals(jsonObject, contentParser.parseJson(jsonObjectString));

    String jsonArrayString = "[" + jsonObjectString + "," + "# yaml comment\n" + jsonObjectString + "]";
    ArrayList<Object> jsonArray = new ArrayList<>(2);
    jsonArray.add(jsonObject);//  w  w w .ja v  a 2  s . c o  m
    jsonArray.add(jsonObject);
    Assert.assertEquals(jsonArray, contentParser.parseJson(jsonArrayString));
}