/** * Factory method for constructing a node that represents JSON * String value */ @Override public TextNode textNode(String text) { return TextNode.valueOf(text); }
/** * If an explicit null is set we preserve that in a {@link NullNode} so that we * can serialize an explicit JSON null instead of ommitting that field from the JSON object. * * @return NullNode if path is null, otherwise a TextNode containing the value. */ private JsonNode resolvePath(String pathString) { if (pathString == null) { return NullNode.getInstance(); } return TextNode.valueOf(pathString); }
private JsonNode merge(JsonNode source, JsonNode replacement) throws IOException { JsonNode replacementNodeEncoded = replacement.get(fieldName); if (replacementNodeEncoded != null) { ObjectNode decodedSource = tryDecode(source.get(fieldName), mapper.createObjectNode(), fieldName); decodedSource.setAll(tryDecode(replacement.get(fieldName), mapper.createObjectNode(), fieldName)); return TextNode.valueOf(mapper.writeValueAsString(decodedSource)); } else { return source.get(fieldName); } }
/** * Set name-value pair properties for this type or field. */ public final S prop(String name, String val) { return prop(name, TextNode.valueOf(val)); }
private JsonNode merge(JsonNode source, JsonNode replacement) throws IOException { JsonNode replacementNodeEncoded = replacement.get(fieldName); if (replacementNodeEncoded != null) { HashMap<String, JsonNode> result = new HashMap<String, JsonNode>(); result.putAll(groupArrayElementsByKey(tryDecode(source.get(fieldName), mapper.createArrayNode(), fieldName), key)); result.putAll(groupArrayElementsByKey(tryDecode(replacement.get(fieldName), mapper.createArrayNode(), fieldName), key)); return TextNode.valueOf(mapper.writeValueAsString(result.values())); } else { return source.get(fieldName); } }
/** * Adds a property with the given name <tt>name</tt> and * value <tt>value</tt>. Neither <tt>name</tt> nor <tt>value</tt> can be * <tt>null</tt>. It is illegal to add a property if another with * the same name but different value already exists in this schema. * * @param name The name of the property to add * @param value The value for the property to add */ public void addProp(String name, String value) { addProp(name, TextNode.valueOf(value)); } public void addProp(String name, Object value) {
JsonProperties(Set<String> reserved, Map<String,?> propMap) { this.reserved = reserved; for (Entry<String, ?> a : propMap.entrySet()) { Object v = a.getValue(); JsonNode json = null; if (v instanceof String) { json = TextNode.valueOf((String)v); } else if (v instanceof JsonNode){ json = (JsonNode)v; } else { json = JacksonUtils.toJsonNode(v); } props.put(a.getKey(), json); } }
} else { final ArrayNode array = (ArrayNode) child; array.set(index, TextNode.valueOf(value)); return;
@Test public void addFieldMerger_IsUsedForSpecifiedFields() throws Exception { // Given ObjectNode a = mapper.createObjectNode(); ObjectNode b = mapper.createObjectNode(); a.put("a", "source"); a.put("b", "source"); b.put("a", "replacement"); b.put("b", "replacement"); // When JsonNode result = JsonMerger.merge(a, b, mergeStrategy().addFieldMerger("b", (source, replacement, fieldName) -> source.get(fieldName))); // Then assertThat(result).hasSize(2); assertThat(result.get("a")).isEqualTo(TextNode.valueOf("replacement")); assertThat(result.get("b")).isEqualTo(TextNode.valueOf("source")); }
@Test public void mergeDefinitions_ReplacesKeysFromSourceWithReplacement() throws Exception { // Given ObjectNode a = mapper.createObjectNode(); ObjectNode b = mapper.createObjectNode(); a.put("a", "a"); b.put("a", "b"); // When JsonNode result = JsonMerger.merge(a, b, mergeStrategy()); // Then assertThat(result).hasSize(1); assertThat(result.get("a")).isEqualTo(TextNode.valueOf("b")); }
@Test public void defaultMerger_IsUsedForAllFields() throws Exception { // Given ObjectNode a = mapper.createObjectNode(); ObjectNode b = mapper.createObjectNode(); a.put("a", "a"); b.put("a", "b"); // When JsonNode result = JsonMerger.merge(a, b, mergeStrategy().defaultMerger((source, replacement, fieldName) -> source.get(fieldName))); // Then assertThat(result).hasSize(1); assertThat(result.get("a")).isEqualTo(TextNode.valueOf("a")); }
@Test public void mergeDefinitions_AddsKeyFromReplacementToSource() throws Exception { // Given ObjectNode a = mapper.createObjectNode(); ObjectNode b = mapper.createObjectNode(); a.put("a", "a"); b.put("b", "b"); // When JsonNode resultWithEmptyMergeStrategy = JsonMerger.merge(a, b, mergeStrategy()); JsonNode resultWithoutOptions = JsonMerger.merge(a, b); // Then assertThat(resultWithEmptyMergeStrategy).isEqualTo(resultWithoutOptions); assertThat(resultWithEmptyMergeStrategy).hasSize(2); assertThat(resultWithEmptyMergeStrategy.get("a")).isEqualTo(TextNode.valueOf("a")); assertThat(resultWithEmptyMergeStrategy.get("b")).isEqualTo(TextNode.valueOf("b")); }
/** * Verify that the Helios master generates and returns a hash if the submitted job creation * request does not include one. */ @Test public void testHashLessJobCreation() throws Exception { startDefaultMaster(); final Job job = Job.newBuilder() .setName(testJobName) .setVersion(testJobVersion) .setImage(BUSYBOX) .setCommand(IDLE_COMMAND) .setCreatingUser(TEST_USER) .build(); // Remove the hash from the id in the json serialized job final ObjectNode json = (ObjectNode) Json.reader().readTree(Json.asString(job)); json.set("id", TextNode.valueOf(testJobName + ":" + testJobVersion)); final HttpURLConnection req = post("/jobs?user=" + TEST_USER, Json.asBytes(json)); assertEquals(req.getResponseCode(), 200); final CreateJobResponse res = Json.read(toByteArray(req.getInputStream()), CreateJobResponse.class); assertEquals(OK, res.getStatus()); assertTrue(res.getErrors().isEmpty()); assertEquals(job.getId().toString(), res.getId()); }
@Test public void testToJsonNode() { assertEquals(null, toJsonNode(null)); assertEquals(NullNode.getInstance(), toJsonNode(JsonProperties.NULL_VALUE)); assertEquals(BooleanNode.TRUE, toJsonNode(true)); assertEquals(IntNode.valueOf(1), toJsonNode(1)); assertEquals(LongNode.valueOf(2), toJsonNode(2L)); assertEquals(FloatNode.valueOf(1.0f), toJsonNode(1.0f)); assertEquals(DoubleNode.valueOf(2.0), toJsonNode(2.0)); assertEquals(TextNode.valueOf("\u0001\u0002"), toJsonNode(new byte[] { 1, 2 })); assertEquals(TextNode.valueOf("a"), toJsonNode("a")); assertEquals(TextNode.valueOf("UP"), toJsonNode(Direction.UP)); ArrayNode an = JsonNodeFactory.instance.arrayNode(); an.add(1); assertEquals(an, toJsonNode(Collections.singletonList(1))); ObjectNode on = JsonNodeFactory.instance.objectNode(); on.put("a", 1); assertEquals(on, toJsonNode(Collections.singletonMap("a", 1))); }
@Test public void testToObject() { assertEquals(null, toObject(null)); assertEquals(JsonProperties.NULL_VALUE, toObject(NullNode.getInstance())); assertEquals(true, toObject(BooleanNode.TRUE)); assertEquals(1, toObject(IntNode.valueOf(1))); assertEquals(2L, toObject(IntNode.valueOf(2), Schema.create(Schema.Type.LONG))); assertEquals(1.0f, toObject(DoubleNode.valueOf(1.0), Schema.create(Schema.Type.FLOAT))); assertEquals(2.0, toObject(DoubleNode.valueOf(2.0))); assertEquals(TextNode.valueOf("\u0001\u0002"), toJsonNode(new byte[]{1, 2})); assertArrayEquals(new byte[]{1, 2}, (byte[]) toObject(TextNode.valueOf("\u0001\u0002"), Schema.create(Schema.Type.BYTES))); assertEquals("a", toObject(TextNode.valueOf("a"))); assertEquals("UP", toObject(TextNode.valueOf("UP"), SchemaBuilder.enumeration("Direction").symbols("UP", "DOWN"))); ArrayNode an = JsonNodeFactory.instance.arrayNode(); an.add(1); assertEquals(Collections.singletonList(1), toObject(an)); ObjectNode on = JsonNodeFactory.instance.objectNode(); on.put("a", 1); assertEquals(Collections.singletonMap("a", 1), toObject(on)); assertEquals(Collections.singletonMap("a", 1L), toObject(on, SchemaBuilder.record("r").fields().requiredLong("a").endRecord())); assertEquals(JsonProperties.NULL_VALUE, toObject(NullNode.getInstance(), SchemaBuilder.unionOf().nullType().and().intType().endUnion())); assertEquals("a", toObject(TextNode.valueOf("a"), SchemaBuilder.unionOf().stringType().and().intType().endUnion())); }
@Test public void testSplitterAggregator() { List<String> payload = Arrays.asList("a", "b", "c", "d", "e"); QueueChannel replyChannel = new QueueChannel(); this.splitAggregateInput.send(MessageBuilder.withPayload(payload) .setReplyChannel(replyChannel) .build()); Message<?> receive = replyChannel.receive(2000); assertNotNull(receive); assertThat(receive.getPayload(), instanceOf(List.class)); @SuppressWarnings("unchecked") List<Object> result = (List<Object>) receive.getPayload(); for (int i = 0; i < payload.size(); i++) { assertThat(result.get(i), instanceOf(TextNode.class)); assertEquals(TextNode.valueOf(payload.get(i)), result.get(i)); } }
return value.getBOOL() ? BooleanNode.TRUE : BooleanNode.FALSE; } else if (value.getS() != null) { return TextNode.valueOf(value.getS()); } else if (value.getN() != null) { double v = Double.parseDouble(value.getN());
/** * Factory method for constructing a node that represents JSON * String value */ @Override public TextNode textNode(String text) { return TextNode.valueOf(text); }
@Override public JsonNode visitText(TextNode textNode) { return TextNode.valueOf(substitutor.replace(textNode.textValue())); } }
@Before public void setUp() throws Exception { mockObjectSpec = context.mock(ObjectSpecification.class); mockEncodableFacet = context.mock(EncodableFacet.class); mockObjectAdapter = context.mock(ObjectAdapter.class); mockAdapterManager = context.mock(AdapterManager.class); JsonValueEncoder.testSetAdapterManager(mockAdapterManager); representation = new JsonRepresentation(TextNode.valueOf("aString")); }