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

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

Introduction

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

Prototype

public ContentParser(final ObjectMapper jsonObjectMapper) 

Source Link

Document

Constructor with a custom configured Jackson ObjectMapper

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);// ww w  . j  ava  2 s.  c o  m
    jsonArray.add(jsonObject);
    Assert.assertEquals(jsonArray, contentParser.parseJson(jsonArrayString));
}