/** * Factory method for getting an instance of JSON null node (which * represents literal null value) */ @Override public NullNode nullNode() { return NullNode.getInstance(); }
@Override public JsonNode getNullValue(DeserializationContext ctxt) { return NullNode.getInstance(); }
@JsonProperty(PropertyNames.PARAMETERS) public Builder parameters(JsonNode parameters) { this.parameters = parameters == null ? NullNode.getInstance() : parameters; return this; }
/** * 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); }
@Deprecated public ToString(com.google.common.base.Optional<JsonNode> option) throws JsonMappingException { JsonNode node = option.or(NullNode.getInstance()); if (node.isTextual()) { this.string = node.textValue(); } else if (node.isValueNode()) { this.string = node.toString(); } else { throw new JsonMappingException(String.format("Arrays and objects are invalid: '%s'", node)); } }
@JsonCreator public ToString(final Optional<JsonNode> option) throws JsonMappingException { final JsonNode node = option.orElse(NullNode.getInstance()); if (node.isTextual()) { this.string = node.textValue(); } else if (node.isValueNode()) { this.string = node.toString(); } else { throw new JsonMappingException(String.format("Arrays and objects are invalid: '%s'", node)); } }
@Override public ExposedPort deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { ObjectCodec oc = jsonParser.getCodec(); JsonNode node = oc.readTree(jsonParser); if (!node.equals(NullNode.getInstance())) { Entry<String, JsonNode> field = node.fields().next(); return ExposedPort.parse(field.getKey()); } else { return null; } } }
@Override public ExposedPorts deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { List<ExposedPort> exposedPorts = new ArrayList<ExposedPort>(); ObjectCodec oc = jsonParser.getCodec(); JsonNode node = oc.readTree(jsonParser); for (Iterator<Map.Entry<String, JsonNode>> it = node.fields(); it.hasNext();) { Map.Entry<String, JsonNode> field = it.next(); if (!field.getValue().equals(NullNode.getInstance())) { exposedPorts.add(ExposedPort.parse(field.getKey())); } } return new ExposedPorts(exposedPorts.toArray(new ExposedPort[0])); } }
/** * Retrieves the value of the field node * * @param fieldNode JmesPath field type * @param input Input json node whose value is * retrieved * @return Value of the input json node */ @Override public JsonNode visit(JmesPathField fieldNode, JsonNode input) { if (input.isObject()) { //TODO : CamelCase will need to change at some point return input.get(CamelCaseUtils.toCamelCase(fieldNode.getValue())); } return NullNode.getInstance(); }
@Override public Links deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException, JsonProcessingException { final List<Link> binds = new ArrayList<Link>(); final ObjectCodec oc = jsonParser.getCodec(); final JsonNode node = oc.readTree(jsonParser); for (final Iterator<JsonNode> it = node.elements(); it.hasNext();) { final JsonNode element = it.next(); if (!element.equals(NullNode.getInstance())) { binds.add(Link.parse(element.asText())); } } return new Links(binds.toArray(new Link[0])); } }
@Override public Volumes deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { List<Volume> volumes = new ArrayList<Volume>(); ObjectCodec oc = jsonParser.getCodec(); JsonNode node = oc.readTree(jsonParser); for (Iterator<Map.Entry<String, JsonNode>> it = node.fields(); it.hasNext();) { Map.Entry<String, JsonNode> field = it.next(); if (!field.getValue().equals(NullNode.getInstance())) { String path = field.getKey(); Volume volume = new Volume(path); volumes.add(volume); } } return new Volumes(volumes.toArray(new Volume[0])); } }
@Override public VolumeRW deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { ObjectCodec oc = jsonParser.getCodec(); JsonNode node = oc.readTree(jsonParser); if (!node.equals(NullNode.getInstance())) { Entry<String, JsonNode> field = node.fields().next(); String volume = field.getKey(); AccessMode accessMode = AccessMode.fromBoolean(field.getValue().asBoolean()); return new VolumeRW(new Volume(volume), accessMode); } else { return null; } } }
@Test public void shouldGetNullStringWhenParsingNullNode() throws Exception { Map<String, JsonNode> tree = new HashMap<>(); NullNode node = NullNode.getInstance(); tree.put("key", node); String text = deserializer.getString(tree, "key"); assertThat(text, is(nullValue())); }
@Test public void shouldGetNullDateWhenParsingNullNode() throws Exception { Map<String, JsonNode> tree = new HashMap<>(); NullNode node = NullNode.getInstance(); tree.put("key", node); Date date = deserializer.getDateFromSeconds(tree, "key"); assertThat(date, is(nullValue())); }
@Test public void shouldGetNullStringWhenParsingNullNode() throws Exception { Map<String, JsonNode> tree = new HashMap<>(); NullNode node = NullNode.getInstance(); tree.put("key", node); String text = deserializer.getString(tree, "key"); assertThat(text, is(nullValue())); }
@Test public void shouldGetNullArrayWhenParsingNullNode() throws Exception { Map<String, JsonNode> tree = new HashMap<>(); NullNode node = NullNode.getInstance(); tree.put("key", node); List<String> values = deserializer.getStringOrArray(tree, "key"); assertThat(values, is(nullValue())); }
@Test public void shouldHaveTree() throws Exception { HashMap<String, JsonNode> map = new HashMap<>(); JsonNode node = NullNode.getInstance(); map.put("key", node); BasicHeader header = new BasicHeader(null, null, null, null, map, objectReader); assertThat(header.getTree(), is(notNullValue())); assertThat(header.getTree(), is(IsMapContaining.hasEntry("key", node))); }
public static Entity createRoot(ContentPack contentPack) { return EntityV1.builder() .type(ModelTypes.ROOT) .id(ModelId.of("virtual-root-" + contentPack.id() + "-" + contentPack.revision())) .data(NullNode.getInstance()) .constraints(ImmutableSet.of()) .build(); }
@Test public void shouldReturnBaseClaimWhenParsingNullNode() throws Exception { JsonNode value = NullNode.getInstance(); Claim claim = claimFromNode(value); assertThat(claim, is(notNullValue())); assertThat(claim, is(instanceOf(NullClaim.class))); assertThat(claim.isNull(), is(true)); }
@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))); }