Java Code Examples for com.fasterxml.jackson.databind.node.JsonNodeFactory#arrayNode()

The following examples show how to use com.fasterxml.jackson.databind.node.JsonNodeFactory#arrayNode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SyndEntrySerializer.java    From streams with Apache License 2.0 6 votes vote down vote up
private void serializeContents(ObjectNode root, JsonNodeFactory factory, List contents) {
  if (contents == null || contents.size() == 0) {
    return;
  }
  ArrayNode contentsArray = factory.arrayNode();
  for (Object obj : contents) {
    ObjectNode content = factory.objectNode();
    if (obj instanceof com.rometools.rome.feed.rss.Content) {
      com.rometools.rome.feed.rss.Content rssContent = (com.rometools.rome.feed.rss.Content) obj;
      content.put("type", rssContent.getType());
      content.put("value", rssContent.getValue());
    }
    if (obj instanceof com.rometools.rome.feed.atom.Content) {
      com.rometools.rome.feed.atom.Content atomContent = (com.rometools.rome.feed.atom.Content) obj;
      content.put("type", atomContent.getType());
      content.put("value", atomContent.getValue());
      content.put("mode", atomContent.getMode());
      content.put("src", atomContent.getSrc());
    }
    contentsArray.add(content);
  }
  root.put("contents", contentsArray);
}
 
Example 2
Source File: SyndEntrySerializer.java    From streams with Apache License 2.0 6 votes vote down vote up
private void serializeModules(ObjectNode root, JsonNodeFactory factory, List modules) {
  if (modules == null || modules.size() == 0) {
    return;
  }
  ArrayNode modulesArray = factory.arrayNode();
  for (Object obj : modules) {
    if (obj instanceof Module) {
      Module mod = (Module) obj;
      if (mod.getUri() != null) {
        modulesArray.add(mod.getUri());
      }
    } else {
      LOGGER.debug("SyndEntry.getModules() items are not of type Module. Need to handle the case of class : {}",
          obj.getClass().toString());
    }
  }
  root.put("modules", modulesArray);
}
 
Example 3
Source File: JsonPatchPatchConverter.java    From spring-sync with Apache License 2.0 6 votes vote down vote up
/**
 * Renders a {@link Patch} as a {@link JsonNode}.
 * @param patch the patch
 * @return a {@link JsonNode} containing JSON Patch.
 */
public JsonNode convert(Patch patch) {
	
	List<PatchOperation> operations = patch.getOperations();
	JsonNodeFactory nodeFactory = JsonNodeFactory.instance;
	ArrayNode patchNode = nodeFactory.arrayNode();
	for (PatchOperation operation : operations) {
		ObjectNode opNode = nodeFactory.objectNode();
		opNode.set("op", nodeFactory.textNode(operation.getOp()));
		opNode.set("path", nodeFactory.textNode(operation.getPath()));
		if (operation instanceof FromOperation) {
			FromOperation fromOp = (FromOperation) operation;
			opNode.set("from", nodeFactory.textNode(fromOp.getFrom()));
		}
		Object value = operation.getValue();
		if (value != null) {
			opNode.set("value", MAPPER.valueToTree(value));
		}
		patchNode.add(opNode);
	}
	
	return patchNode;
}
 
Example 4
Source File: FileSampler.java    From log-synth with Apache License 2.0 6 votes vote down vote up
private void readDelimitedData(String lookup, List<String> lines) {
    Splitter splitter;
    if (lookup.matches(".*\\.csv")) {
        splitter = Splitter.on(",");
    } else if (lookup.matches(".*\\.tsv")) {
        splitter = Splitter.on("\t");
    } else {
        throw new IllegalArgumentException("Must have file with .csv, .tsv or .json suffix");
    }

    List<String> names = Lists.newArrayList(splitter.split(lines.get(0)));
    JsonNodeFactory nf = JsonNodeFactory.withExactBigDecimals(false);
    ArrayNode localData = nf.arrayNode();
    for (String line : lines.subList(1, lines.size())) {
        ObjectNode r = nf.objectNode();
        List<String> fields = Lists.newArrayList(splitter.split(line));
        Preconditions.checkState(names.size() == fields.size(), "Wrong number of fields, expected ", names.size(), fields.size());
        Iterator<String> ix = names.iterator();
        for (String field : fields) {
            r.put(ix.next(), field);
        }
        localData.add(r);
    }
    data = localData;
}
 
Example 5
Source File: ConversionUtil.java    From native-navigation with MIT License 5 votes vote down vote up
/** Converts a {@link ReadableArray} into an Json {@link ArrayNode} */
static ArrayNode toJsonArray(ReadableArray readableArray) {
  JsonNodeFactory nodeFactory = JsonNodeFactory.instance;
  ArrayNode result = nodeFactory.arrayNode();
  for (int i = 0; i < readableArray.size(); i++) {
    ReadableType indexType = readableArray.getType(i);
    switch (indexType) {
      case Null:
        result.addNull();
        break;
      case Boolean:
        result.add(readableArray.getBoolean(i));
        break;
      case Number:
        result.add(readableArray.getDouble(i));
        break;
      case String:
        result.add(readableArray.getString(i));
        break;
      case Map:
        result.add(toJsonObject(readableArray.getMap(i)));
        break;
      case Array:
        result.add(toJsonArray(readableArray.getArray(i)));
        break;
      default:
        Log.e(TAG, "Could not convert object at index " + i + ".");
    }
  }
  return result;
}
 
Example 6
Source File: ContentManifest.java    From cordova-hot-code-push with MIT License 5 votes vote down vote up
private String generateJson() {
    final JsonNodeFactory nodeFactory = JsonNodeFactory.instance;
    ArrayNode filesListNode = nodeFactory.arrayNode();
    for (ManifestFile fileEntry : files) {
        ObjectNode fileNode = nodeFactory.objectNode();
        fileNode.set(JsonKeys.FILE_PATH, nodeFactory.textNode(fileEntry.name));
        fileNode.set(JsonKeys.FILE_HASH, nodeFactory.textNode(fileEntry.hash));
        filesListNode.add(fileNode);
    }

    return filesListNode.toString();
}
 
Example 7
Source File: JsonDiff.java    From zjsonpatch with Apache License 2.0 5 votes vote down vote up
private ArrayNode getJsonNodes() {
    JsonNodeFactory FACTORY = JsonNodeFactory.instance;
    final ArrayNode patch = FACTORY.arrayNode();
    for (Diff diff : diffs) {
        ObjectNode jsonNode = getJsonNode(FACTORY, diff, flags);
        patch.add(jsonNode);
    }
    return patch;
}
 
Example 8
Source File: SyndEntrySerializer.java    From streams with Apache License 2.0 5 votes vote down vote up
private void serializeListOfStrings(List toSerialize, String key, ObjectNode node, JsonNodeFactory factory) {
  if (toSerialize == null || toSerialize.size() == 0) {
    return;
  }
  ArrayNode keyNode = factory.arrayNode();
  for (Object obj : toSerialize) {
    if (obj instanceof String) {
      keyNode.add((String) obj);
    } else {
      LOGGER.debug("Array at Key:{} was expecting item types of String. Received class : {}", key, obj.getClass().toString());
    }
  }
  node.put(key, keyNode);
}
 
Example 9
Source File: MultiPointToSinglePointIntentCodecTest.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Tests the multi point to single point intent decoding with JSON codec.
 *
 * @throws IOException if JSON processing fails
 */
@Test
public void decodeMultiPointToSinglePointIntent() throws IOException {

    final JsonNodeFactory nodeFactory = JsonNodeFactory.instance;
    ObjectNode json = nodeFactory.objectNode();
    json.put("type", "MultiPointToSinglePointIntent");
    json.put("id", "0x0");
    json.put("appId", "foo");
    json.put("priority", 100);
    ArrayNode ingress = nodeFactory.arrayNode();
    ObjectNode ingressPoint = nodeFactory.objectNode();
    ingressPoint.put("port", "3");
    ingressPoint.put("device", "333");
    ingress.add(ingressPoint);
    ObjectNode ingressPoint2 = nodeFactory.objectNode();
    ingressPoint2.put("port", "1");
    ingressPoint2.put("device", "111");
    ingress.add(ingressPoint2);
    json.set("ingressPoint", ingress);
    ObjectNode egressPoint = nodeFactory.objectNode();
    egressPoint.put("port", "2");
    egressPoint.put("device", "222");
    json.set("egressPoint", egressPoint);
    assertThat(json, notNullValue());

    JsonCodec<MultiPointToSinglePointIntent> intentCodec = context.codec(MultiPointToSinglePointIntent.class);
    assertThat(intentCodec, notNullValue());

    final MultiPointToSinglePointIntent intent = intentCodec.decode(json, context);

    assertThat(intent.toString(), notNullValue());
    assertThat(intent, instanceOf(MultiPointToSinglePointIntent.class));
    assertThat(intent.priority(), is(100));
    assertThat(intent.ingressPoints().toString(), is("[333/3, 111/1]"));
    assertThat(intent.egressPoint().toString(), is("222/2"));

}