Example usage for com.fasterxml.jackson.databind.node JsonNodeFactory JsonNodeFactory

List of usage examples for com.fasterxml.jackson.databind.node JsonNodeFactory JsonNodeFactory

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind.node JsonNodeFactory JsonNodeFactory.

Prototype

protected JsonNodeFactory(boolean paramBoolean) 

Source Link

Usage

From source file:org.raegdan.troca.Main.java

private static void printRatesAsJSON(HashMap<String, Double> rates, boolean fancy) {
    JsonNodeFactory factory = new JsonNodeFactory(true);
    ObjectNode rootNode = new ObjectNode(factory);

    for (Entry<String, Double> rate : rates.entrySet())
        rootNode.put(rate.getKey(), rate.getValue());

    try {//from  w  w w .  j a  va 2s.c om
        out((fancy) ? new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(rootNode)
                : rootNode.toString(), true);
    } catch (JsonProcessingException e) {
        printException(e);
    }
}

From source file:squash.deployment.lambdas.utils.CloudFormationResponder.java

/**
 *  Initialises the responder.//from w  ww  .  j  av  a  2  s  .  c o m
 *  
 *  <p>This must be called before adding properties to the response or sending the response
 */
public void initialise() throws IOException {
    // Create the node factory that gives us nodes.
    this.factory = new JsonNodeFactory(false);
    // create a json factory to write the treenode as json.
    this.jsonFactory = new JsonFactory();
    this.cloudFormationJsonResponse = new ByteArrayOutputStream();
    this.printStream = new PrintStream(cloudFormationJsonResponse);
    this.generator = jsonFactory.createGenerator(printStream);
    this.mapper = new ObjectMapper();
    this.rootNode = factory.objectNode();
    this.dataOutputsNode = factory.objectNode();
    this.rootNode.set("Data", this.dataOutputsNode);
    this.initialised = true;
}

From source file:com.redhat.lightblue.metadata.types.BinaryTypeTest.java

License:asdf

@Test
public void testFromJsonString() throws IOException {
    String jsonString = "{\"binaryData\": \"asdf\"}";

    JsonNode node = JsonUtils.json(jsonString);

    assertTrue(node != null);/*from   w  w  w. ja va 2 s  . c  o  m*/

    JsonNode binaryDataNode = node.get("binaryData");

    assertTrue(binaryDataNode != null);
    byte[] bytes = binaryDataNode.binaryValue();
    assertTrue(bytes != null);
    assertTrue(bytes.length > 0);

    // try to convert back to json, verify we get the exact same thing
    JsonNodeFactory jsonNodeFactory = new JsonNodeFactory(true);
    JsonNode binaryDataNodeOut = binaryType.toJson(jsonNodeFactory, bytes);

    assertTrue(binaryDataNodeOut != null);
    assertEquals("asdf", binaryDataNodeOut.asText());
    assertEquals("\"asdf\"", binaryDataNodeOut.toString());
}

From source file:com.redhat.lightblue.metadata.rdbms.impl.RDBMSDataStoreParserTest.java

@Test
public void testConvert() throws IOException {
    String databaseName = "fakeDatabase";
    String datasourceName = "fakeDatasource";

    MetadataParser<JsonNode> p = new JSONMetadataParser(new Extensions<JsonNode>(), new DefaultTypes(),
            new JsonNodeFactory(true));

    JsonNode emptyNode = JsonUtils.json("{}");

    RDBMSDataStore ds = new RDBMSDataStore(databaseName, datasourceName);

    new RDBMSDataStoreParser<JsonNode>().convert(p, emptyNode, ds);

    assertEquals(databaseName, p.getStringProperty(emptyNode, "database"));
    assertEquals(datasourceName, p.getStringProperty(emptyNode, "datasource"));
}

From source file:com.redhat.lightblue.metadata.rdbms.impl.RDBMSDataStoreParserTest.java

@Test
public void testConvert_WithNullValues() throws IOException {
    MetadataParser<JsonNode> p = new JSONMetadataParser(new Extensions<JsonNode>(), new DefaultTypes(),
            new JsonNodeFactory(true));

    JsonNode emptyNode = JsonUtils.json("{}");

    RDBMSDataStore ds = new RDBMSDataStore(null, null);

    new RDBMSDataStoreParser<JsonNode>().convert(p, emptyNode, ds);

    assertNull(p.getStringProperty(emptyNode, "database"));
    assertNull(p.getStringProperty(emptyNode, "datasource"));
}

From source file:com.redhat.lightblue.metadata.rdbms.impl.RDBMSPropertyParserImplTest.java

@Test
public void testParse_NoDialect() throws IOException {
    expectedEx.expect(com.redhat.lightblue.util.Error.class);
    expectedEx.expectMessage("{\"objectType\":\"error\",\"errorCode\":\"" + RDBMSConstants.ERR_FIELD_REQUIRED
            + "\",\"msg\":\"No field informed\"}");

    MetadataParser<JsonNode> p = new JSONMetadataParser(new Extensions<JsonNode>(), new DefaultTypes(),
            new JsonNodeFactory(true));

    JsonNode emptyNode = JsonUtils.json("{}");

    new RDBMSPropertyParserImpl<JsonNode>().parse(RDBMSPropertyParserImpl.NAME, p, emptyNode);
}

From source file:com.redhat.lightblue.metadata.types.BinaryTypeTest.java

License:asdf

@Test
public void testEmbeddedPDF() throws FileNotFoundException, IOException, JSONException {
    String pdfFilename = "./BinaryTypeTest-sample.pdf";

    InputStream is = this.getClass().getClassLoader().getResourceAsStream(pdfFilename);

    ByteArrayOutputStream buffer = new ByteArrayOutputStream();

    int read;/*from  w  w w. j  a v  a 2  s . co m*/
    byte[] bytes = new byte[1000];

    while ((read = is.read(bytes, 0, bytes.length)) != -1) {
        buffer.write(bytes, 0, read);
    }

    String encoded = DatatypeConverter.printBase64Binary(buffer.toByteArray());

    String jsonString = "{\"binaryData\": \"" + encoded + "\"}";

    JsonNode nodeIn = JsonUtils.json(jsonString);
    JsonNode binaryDataNodeIn = nodeIn.get("binaryData");

    byte[] bytesOut = binaryDataNodeIn.binaryValue();

    JsonNodeFactory jsonNodeFactory = new JsonNodeFactory(true);
    JsonNode binaryDataNodeOut = binaryType.toJson(jsonNodeFactory, bytesOut);

    ObjectNode nodeOut = jsonNodeFactory.objectNode();
    nodeOut.set("binaryData", binaryDataNodeOut);

    JSONAssert.assertEquals(jsonString, nodeOut.toString(), JSONCompareMode.LENIENT);
}

From source file:com.redhat.lightblue.metadata.rdbms.impl.RDBMSPropertyParserImplTest.java

@Test
public void testParse_EmptyDialect() throws IOException {
    expectedEx.expect(com.redhat.lightblue.util.Error.class);
    expectedEx.expectMessage("{\"objectType\":\"error\",\"errorCode\":\"" + RDBMSConstants.ERR_FIELD_REQUIRED
            + "\",\"msg\":\"No field informed\"}");

    MetadataParser<JsonNode> p = new JSONMetadataParser(new Extensions<JsonNode>(), new DefaultTypes(),
            new JsonNodeFactory(true));

    JsonNode emptyNode = JsonUtils.json("{\"dialect\":\"\"}");

    new RDBMSPropertyParserImpl<JsonNode>().parse(RDBMSPropertyParserImpl.NAME, p, emptyNode);
}

From source file:uk.ac.ucl.excites.sapelli.collector.SapColCmdLn.java

static public void printProjectInfoJSON(File sapFile, Project project) throws IOException {
    // Create the node factory that gives us nodes.
    JsonNodeFactory factory = new JsonNodeFactory(false);

    // create a json factory to write the treenode as json. for the example
    // we just write to console
    JsonFactory jsonFactory = new JsonFactory();
    JsonGenerator generator = jsonFactory.createGenerator(System.out);
    ObjectMapper mapper = new ObjectMapper();

    // the root node
    ObjectNode projectJSON = factory.objectNode();

    // describe project:
    projectJSON.put("source", sapFile.getAbsolutePath());
    projectJSON.put("id", project.getID());
    projectJSON.put("fingerprint", project.getFingerPrint());
    projectJSON.put("name", project.getName());
    projectJSON.put("variant", project.getVariant());
    projectJSON.put("version", project.getVersion());
    projectJSON.put("display-name", project.toString(false));
    projectJSON.put("model-id", project.getModel().id);
    projectJSON.put("install-path", fsp.getProjectInstallationFolder(project, false).getAbsolutePath());
    ArrayNode formsJSON = factory.arrayNode();
    for (Form frm : project.getForms()) {
        ObjectNode formJSON = factory.objectNode();
        formJSON.put("id", frm.id);
        formJSON.put("produces-data", frm.isProducesRecords());
        formJSON.put("model-schema-number",
                (frm.isProducesRecords() ? frm.getSchema().getModelSchemaNumber() : null));
        formsJSON.add(formJSON);//from w  w  w  .  jav a  2 s .c  om
    }
    projectJSON.set("forms", formsJSON);

    // Serialise:
    mapper.writeTree(generator, projectJSON);
}

From source file:com.redhat.lightblue.metadata.rdbms.impl.RDBMSPropertyParserImplTest.java

@Test
public void testParse_NoOperation() throws IOException {
    expectedEx.expect(com.redhat.lightblue.util.Error.class);
    expectedEx.expectMessage("{\"objectType\":\"error\",\"errorCode\":\"" + RDBMSConstants.ERR_FIELD_REQUIRED
            + "\",\"msg\":\"No Operation informed\"}");

    MetadataParser<JsonNode> p = new JSONMetadataParser(new Extensions<JsonNode>(), new DefaultTypes(),
            new JsonNodeFactory(true));

    JsonNode emptyNode = JsonUtils.json("{\"dialect\":\"" + DialectOperators.ORACLE + "\"}");

    new RDBMSPropertyParserImpl<JsonNode>().parse(RDBMSPropertyParserImpl.NAME, p, emptyNode);
}