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

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

Introduction

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

Prototype

JsonNodeFactory instance

To view the source code for com.fasterxml.jackson.databind.node JsonNodeFactory instance.

Click Source Link

Usage

From source file:io.syndesis.maven.ExtractConnectorDescriptorsMojo.java

private void addConnectorMeta(ObjectNode root, ClassLoader classLoader) {
    ObjectNode node = new ObjectNode(JsonNodeFactory.instance);
    addOptionalNode(classLoader, node, "meta", "camel-connector.json");
    addOptionalSchemaAsString(classLoader, node, "schema", "camel-connector-schema.json");
    if (node.size() > 0) {
        root.set("connector", node);
    }/*w  w  w. j  a va 2 s  . c om*/
}

From source file:org.jetbrains.webdemo.ResponseUtils.java

public static ObjectNode getErrorAsJsonNode(String error) {
    ObjectNode result = new ObjectNode(JsonNodeFactory.instance);
    result.put("type", "err");
    result.put("exception", error);
    return result;
}

From source file:managers.nodes.SlotManager.java

protected Promise<JsonNode> toJSON(final JsonNode properties) {
    Slot slot = new Slot(properties.get("uuid").asText());
    Promise<List<JsonNode>> partsAndRefs = Has.relationships.endNodes(slot);
    Promise<JsonNode> json = partsAndRefs.map(new Function<List<JsonNode>, JsonNode>() {
        public JsonNode apply(List<JsonNode> partsAndRefs) {
            List<JsonNode> partNodes = new ArrayList<JsonNode>();
            List<JsonNode> refNodes = new ArrayList<JsonNode>();
            for (JsonNode partOrRef : partsAndRefs) {
                if (partOrRef.has("name")) {
                    String name = partOrRef.get("name").asText();
                    JsonNode ref = new TextNode(name);
                    refNodes.add(ref);//w  w  w.java  2  s  .  c o  m
                } else {
                    partNodes.add(partOrRef);
                }
            }
            ArrayNode parts = JsonNodeFactory.instance.arrayNode();
            parts.addAll(partNodes);
            ((ObjectNode) properties).put("parts", parts);
            ArrayNode refs = JsonNodeFactory.instance.arrayNode();
            refs.addAll(refNodes);
            ((ObjectNode) properties).put("refs", refs);
            return properties;
        }
    });
    return json;
}

From source file:org.codehaus.modello.generator.jackson.JacksonVerifier.java

public void verifyWriter() throws Exception {
    String expectedJson = FileUtils.fileRead(getTestFile("src/test/verifiers/jackson/expected.json"));

    // ----------------------------------------------------------------------
    // Build the model thats going to be written.
    // ----------------------------------------------------------------------

    Model expected = new Model();

    expected.setExtend("/foo/bar");

    expected.setName("Maven");

    expected.setModelVersion("4.0.0");

    MailingList mailingList = new MailingList();

    mailingList.setName("Mailing list");

    mailingList.setSubscribe("Super Subscribe");

    mailingList.setUnsubscribe("Duper Unsubscribe");

    mailingList.setArchive("?ber Archive");

    expected.addMailingList(mailingList);

    Scm scm = new Scm();

    String connection = "connection";

    String developerConnection = "developerConnection";

    String url = "url";

    scm.setConnection(connection);/*from   www  . j  a va 2 s .  co m*/

    scm.setDeveloperConnection(developerConnection);

    scm.setUrl(url);

    expected.setScm(scm);

    Build build = new Build();

    build.setSourceDirectory("src/main/java");

    build.setUnitTestSourceDirectory("src/test/java");

    SourceModification sourceModification = new SourceModification();

    sourceModification.setClassName("excludeEclipsePlugin");

    sourceModification.setDirectory("foo");

    sourceModification.addExclude("de/abstrakt/tools/codegeneration/eclipse/*.java");

    build.addSourceModification(sourceModification);

    expected.setBuild(build);

    Component component = new Component();

    component.setName("component1");

    expected.addComponent(component);

    component = new Component();

    component.setName("component2");

    component.setComment("comment2");

    expected.addComponent(component);

    Component c2 = new Component();

    c2.setName("sub");

    c2.setComment("subcomment");

    component.getComponents().add(c2);

    component = new Component();

    component.setName("component3");

    // DOM

    ObjectNode custom = JsonNodeFactory.instance.objectNode();
    custom.put("foo", "bar");

    ObjectNode child = JsonNodeFactory.instance.objectNode();
    child.put("att1", "value");
    child.put("content", "baz");
    custom.put("bar", child);

    ObjectNode el1 = JsonNodeFactory.instance.objectNode();
    el1.put("el2", "te&xt");

    custom.put("el1", el1);

    custom.putArray("excludes").add("*.vlt").add("*.xml");

    component.setCustom(custom);

    expected.addComponent(component);

    // end DOM

    component = new Component();
    component.setName("component4");
    expected.addComponent(component);

    Properties properties = new Properties();
    properties.setProperty("name", "value");
    component.setFlatProperties(properties);

    properties = new Properties();
    properties.setProperty("key", "theValue");
    component.setProperties(properties);

    Repository repository = new Repository();
    repository.setId("foo");
    expected.addRepository(repository);

    repository = new Repository();
    repository.setId("bar");
    expected.addRepository(repository);

    ContentTest content = new ContentTest();
    content.setContent("content value");
    content.setAttr("attribute");
    expected.setContent(content);

    // ----------------------------------------------------------------------
    // Write out the model
    // ----------------------------------------------------------------------

    MavenJacksonWriter writer = new MavenJacksonWriter();

    StringWriter buffer = new StringWriter();

    writer.write(buffer, expected);

    String actualJson = buffer.toString();

    System.out.println(expectedJson);

    System.out.println("+-----------------------------------+");

    System.out.println(actualJson);

    compareJsonText(expectedJson, actualJson);

    // Test the reader

    MavenJacksonReader reader = new MavenJacksonReader();

    Model actual = reader.read(new StringReader(expectedJson));

    Assert.assertNotNull("Actual", actual);

    assertModel(expected, actual);

    buffer = new StringWriter();

    writer.write(buffer, actual);

    // test the re-writer result

    compareJsonText(expectedJson, buffer.toString());
}

From source file:com.github.jonpeterson.jackson.module.versioning.VersionedModelSerializer.java

private void doSerialize(T value, JsonGenerator generator, SerializerProvider provider,
        TypeSerializer typeSerializer) throws IOException {
    // serialize the value into a byte array buffer then parse it back out into a JsonNode tree
    // TODO: find a better way to convert the value into a tree
    JsonFactory factory = generator.getCodec().getFactory();
    ByteArrayOutputStream buffer = new ByteArrayOutputStream(4096);
    JsonGenerator bufferGenerator = factory.createGenerator(buffer);
    try {/* w w w.j av a 2 s  . co m*/
        if (typeSerializer != null)
            delegate.serializeWithType(value, bufferGenerator, provider, typeSerializer);
        else
            delegate.serialize(value, bufferGenerator, provider);
    } finally {
        bufferGenerator.close();
    }

    ObjectNode modelData = factory.createParser(buffer.toByteArray()).readValueAsTree();

    // set target version to @SerializeToVersion's value, @JsonVersionModel's defaultSerializeToVersion, or
    //   @JsonVersionModel's currentVersion in that order
    String targetVersion = null;
    if (serializeToVersionProperty != null) {
        targetVersion = (String) serializeToVersionProperty.getAccessor().getValue(value);
        modelData.remove(serializeToVersionProperty.getName());
    }
    if (targetVersion == null)
        targetVersion = jsonVersionedModel.defaultSerializeToVersion();
    if (targetVersion.isEmpty())
        targetVersion = jsonVersionedModel.currentVersion();

    // convert model data if there is a converter and targetVersion is different than the currentVersion or if
    //   alwaysConvert is true
    if (converter != null && (jsonVersionedModel.alwaysConvert()
            || !targetVersion.equals(jsonVersionedModel.currentVersion())))
        modelData = converter.convert(modelData, jsonVersionedModel.currentVersion(), targetVersion,
                JsonNodeFactory.instance);

    // add target version to model data if it wasn't the version to suppress
    if (!targetVersion.equals(jsonVersionedModel.versionToSuppressPropertySerialization()))
        modelData.put(jsonVersionedModel.propertyName(), targetVersion);

    // write node
    generator.writeTree(modelData);
}

From source file:org.openlmis.web.service.VendorEventFeedService.java

private JsonNode convertToTemplate(Map<String, String> map, String value) throws IOException {
    ObjectMapper objectMapper = new ObjectMapper();
    JsonNode rootNode = objectMapper.readTree(value);
    ObjectNode returnedNode = new ObjectNode(JsonNodeFactory.instance);
    Iterator<Map.Entry<String, JsonNode>> iterator = rootNode.fields();
    while (iterator.hasNext()) {
        Map.Entry<String, JsonNode> mapEntry = iterator.next();
        String fieldName = mapEntry.getKey();
        String mappedName = map.get(fieldName);
        if (mappedName != null) {
            returnedNode.put(mappedName, mapEntry.getValue());
        } else {/*from  w ww .  j ava  2s . c  o  m*/
            returnedNode.put(fieldName, mapEntry.getValue());
        }
    }
    return returnedNode;
}

From source file:com.turn.shapeshifter.NamedSchemaSerializer.java

/**
 * Serializes a repeated field./*  ww w.  j a  va  2 s  .c  om*/
 *
 * @param message the message being serialized
 * @param registry a registry of schemas, for enclosed object types
 * @param field the descriptor of the repeated field to serialize
 * @param count the count of repeated items in the field
 * @return the JSON representation of the serialized
 * @throws SerializationException
 */
private ArrayNode serializeRepeatedField(Message message, ReadableSchemaRegistry registry,
        FieldDescriptor field, int count) throws SerializationException {
    ArrayNode array = new ArrayNode(JsonNodeFactory.instance);
    for (int i = 0; i < count; i++) {
        Object value = message.getRepeatedField(field, i);
        JsonNode valueNode = serializeValue(value, field, registry);
        if (!valueNode.isNull()) {
            array.add(valueNode);
        }
    }
    return array;
}

From source file:io.gs2.matchmaking.Gs2MatchmakingClient.java

/**
 * Anybody????<br>/*from w  w  w  .j  ava2  s. com*/
 * <br>
 * ????????????????<br>
 * ????ID?????????ID???API?????<br>
 * <br>
 * ?????????????<br>
 * ??????????<br>
 * ?????????API?????????<br>
 * <br>
 * - : 10<br>
 * <br>
 *
 * @param request 
        
 * @return ?
        
 */

public AnybodyDoMatchmakingResult anybodyDoMatchmaking(AnybodyDoMatchmakingRequest request) {

    ObjectNode body = JsonNodeFactory.instance.objectNode();

    HttpPost post = createHttpPost(
            Gs2Constant.ENDPOINT_HOST + "/matchmaking/"
                    + (request.getMatchmakingName() == null || request.getMatchmakingName().equals("") ? "null"
                            : request.getMatchmakingName())
                    + "/anybody",
            credential, ENDPOINT, AnybodyDoMatchmakingRequest.Constant.MODULE,
            AnybodyDoMatchmakingRequest.Constant.FUNCTION, body.toString());
    if (request.getRequestId() != null) {
        post.setHeader("X-GS2-REQUEST-ID", request.getRequestId());
    }

    post.setHeader("X-GS2-ACCESS-TOKEN", request.getAccessToken());

    return doRequest(post, AnybodyDoMatchmakingResult.class);

}

From source file:es.bsc.amon.controller.EventsDBMapper.java

public ObjectNode markAsFinished(String id) {
    BasicDBObjectBuilder q = BasicDBObjectBuilder.start();
    q.add(_ID, new ObjectId(id));
    long timestamp = Calendar.getInstance().getTimeInMillis();

    BasicDBObjectBuilder m = BasicDBObjectBuilder.start();
    m.add("$set", BasicDBObjectBuilder.start(ENDTIME, timestamp).get());

    colEvents.update(q.get(), m.get(), false, false);

    ObjectNode on = new ObjectNode(JsonNodeFactory.instance);
    on.put(_ID, id);/*from  w w  w.  j a  v a 2  s .c om*/
    on.put(ENDTIME, timestamp);

    return on;
}

From source file:controllers.CommentController.java

private ArrayNode getCommentArray(Long elementId, Long versionId, Long parentId) {
    List<Comment> comments = commentRepository.findAllByClimateServiceIdAndVersionIdAndParentId(elementId,
            versionId, parentId);/*  www.j a  v  a 2s.c  om*/
    ArrayNode commentArray = JsonNodeFactory.instance.arrayNode();

    for (Comment comment : comments) {
        ObjectNode oneComment = JsonNodeFactory.instance.objectNode();
        oneComment.put("comment_id", comment.getCommentId());
        oneComment.put("parent_id", parentId);
        oneComment.put("in_reply_to", comment.getInReplyTo());
        oneComment.put("element_id", elementId);
        oneComment.put("created_by", comment.getCreatedBy());
        oneComment.put("fullname", comment.getFullname());
        oneComment.put("picture", comment.getPicture());
        oneComment.put("posted_date", timeFormat.format(comment.getPostedDate()));
        oneComment.put("text", comment.getText());
        oneComment.put("attachments", JsonNodeFactory.instance.arrayNode());
        oneComment.put("childrens", getCommentArray(elementId, versionId, comment.getCommentId()));
        commentArray.add(oneComment);
    }

    return commentArray;
}