@JsonCreator private static PluginType create(final JsonNode typeJson) { if (typeJson.isTextual()) { return createFromString(((TextNode) typeJson).textValue()); } else if (typeJson.isObject()) { final HashMap<String, String> stringMap = new HashMap<String, String>(); final ObjectNode typeObject = (ObjectNode) typeJson; final Iterator<Map.Entry<String, JsonNode>> fieldIterator = typeObject.fields(); while (fieldIterator.hasNext()) { final Map.Entry<String, JsonNode> field = fieldIterator.next(); final JsonNode fieldValue = field.getValue(); if (fieldValue instanceof ContainerNode) { throw new IllegalArgumentException("\"type\" must be a string or a 1-depth mapping."); } stringMap.put(field.getKey(), fieldValue.textValue()); } return createFromStringMap(stringMap); } else { throw new IllegalArgumentException("\"type\" must be a string or a 1-depth mapping."); } }
String format = node.get("format") == null ? "" : node.get("format").textValue(); if (type != null && "array".equals(((TextNode) type).textValue())) { schema = Json.mapper().convertValue(node, ArraySchema.class); } else if (type != null) {
@Test public void testJsonStringAndJsonNode() { ObjectToJsonTransformer transformer = new ObjectToJsonTransformer(ObjectToJsonTransformer.ResultType.NODE); Object result = transformer.transform(new GenericMessage<>("{\"foo\": \"FOO\", \"bar\": 1}")).getPayload(); assertThat(result, instanceOf(ObjectNode.class)); ObjectNode objectNode = (ObjectNode) result; assertEquals(2, objectNode.size()); assertEquals("FOO", objectNode.path("foo").textValue()); assertEquals(1, objectNode.path("bar").intValue()); result = transformer.transform(new GenericMessage<>("foo")).getPayload(); assertThat(result, instanceOf(TextNode.class)); assertEquals("foo", ((TextNode) result).textValue()); }
public class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> { @Override public LocalDateTime deserialize(JsonParser jp, DeserializationContext dc) throws IOException, JsonProcessingException { ObjectCodec codec = jp.getCodec(); TextNode node = (TextNode)codec.readTree(jp); String dateString = node.textValue(); Instant instant = Instant.parse(dateString); LocalDateTime dateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()); return dateTime; } } public class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> { @Override public void serialize(LocalDateTime dateTime, JsonGenerator jg, SerializerProvider sp) throws IOException, JsonProcessingException { Instant instant = dateTime.toInstant(ZoneOffset.UTC); jg.writeString(DateTimeFormatter.ISO_INSTANT.format(instant)); } }
@Override public Object serialize(Object dataFetcherResult) { if (dataFetcherResult instanceof String) { return dataFetcherResult; } if (dataFetcherResult instanceof TextNode) { return ((TextNode) dataFetcherResult).textValue(); } else { throw serializationException(dataFetcherResult, String.class, TextNode.class); } }
@Override public LocalDate deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { ObjectCodec oc = jsonParser.getCodec(); TextNode node = oc.readTree(jsonParser); String dateString = node.textValue(); if (dateString == null || dateString.trim().isEmpty()) { return null; } try { return LocalDate.parse(dateString); } catch (DateTimeException ignored) { LOG.log(Level.SEVERE, "Failed to parse a LocalDate from an object: " + dateString); } return null; } });
@Override public LocalDateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { ObjectCodec oc = jsonParser.getCodec(); TextNode node = oc.readTree(jsonParser); String dateString = node.textValue(); if (dateString == null || dateString.trim().isEmpty()) { return null; } try { return LocalDateTime.parse(dateString); } catch (DateTimeException ignored) { LOG.log(Level.SEVERE, "Failed to parse a LocalDateTime from an object: " + dateString); } return null; } });
@Override public LocalTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { ObjectCodec oc = jsonParser.getCodec(); TextNode node = oc.readTree(jsonParser); String timeString = node.textValue(); if (timeString == null || timeString.trim().isEmpty()) { return null; } try { return LocalTime.parse(timeString); } catch (DateTimeException ignored) { LOG.log(Level.SEVERE, "Failed to parse a LocalTime from an object: " + timeString); } return null; } });
private String toString(JsonNode item) { if (item.getNodeType() == JsonNodeType.STRING) { return ((TextNode) item).textValue(); } else if (item.isArray() && ((ArrayNode) item).size() < 1) { if (((ArrayNode) item).size() == 0) { return ""; } else { return toString(((ArrayNode) item).get(0)); } } return item.toString(); }
@Override public JsonNode visitText(TextNode textNode) { return TextNode.valueOf(substitutor.replace(textNode.textValue())); } }
public Path pathRef(TextNode ref, String location, ParseResult result) { RefPath output = new RefPath(); output.set$ref(ref.textValue()); return output; }
/** * Create a new replace patch operation. * * @param path The path targeted by this patch operation. The path * must not be {@code null}. * @param value The value(s) to replace. The value(s) must not be {@code null}. * * @return The new replace patch operation. * @throws ScimException if an error occurs. */ public static PatchOperation replace( final Path path, final Date value) throws ScimException { String valueString = GenericScimResource.getDateJsonNode(value).textValue(); return replace(path, valueString); }
/** * Create a new replace patch operation. * * @param path The path targeted by this patch operation. The path * must not be {@code null}. * @param value The value(s) to replace. The value(s) must not be {@code null}. * * @return The new replace patch operation. * @throws ScimException if an error occurs. */ public static PatchOperation replace( final Path path, final Date value) throws ScimException { String valueString = GenericScimResource.getDateJsonNode(value).textValue(); return replace(path, valueString); }
public static TextNode resolveSecrets(TextNode node, SecretProvider secrets) { return new TextNode(UserSecretTemplate.of(node.textValue()).format(secrets)); }
public RefModel refModel(ObjectNode node, String location, ParseResult result) { RefModel output = new RefModel(); if(node.getNodeType().equals(JsonNodeType.OBJECT)) { String refValue = ((TextNode)node.get("$ref")).textValue(); output.set$ref(refValue); } else { result.invalidType(location, "$ref", "object", node); return null; } // extra keys Set<String> keys = getKeys(node); for(String key : keys) { if(!REF_MODEL_KEYS.contains(key)) { result.extra(location, key, node.get(key)); } } return output; }
@Override public void testRequest() throws Exception { super.testRequest(); ObjectNode node = assertInstanceOf(ObjectNode.class, payload); Iterator<String> fields = node.fieldNames(); assertEquals("foo", fields.next()); assertFalse(fields.hasNext()); TextNode text = assertInstanceOf(TextNode.class, node.get("foo")); assertEquals("bar", text.textValue()); } }
@Test public void testGetMap() throws Exception { TupleEntry entry = new TupleEntry( new Fields( "json", JSONCoercibleType.TYPE ), Tuple.size( 1 ) ); entry.setObject( 0, JSONData.nested ); // the map ctor really expects large numbers of entries to be useful JSONGetFunction function = new JSONGetFunction( singletonMap( new Fields( "result" ), "/person/name" ) ); TupleListCollector result = invokeFunction( function, entry, new Fields( "result" ) ); Object value = result.iterator().next().getObject( 0 ); assertNotNull( value ); assertEquals( "John Doe", ( (TextNode) value ).textValue() ); } }
@Test public void testGetMap() throws Exception { TupleEntry entry = new TupleEntry( new Fields( "json", JSONCoercibleType.TYPE ), Tuple.size( 1 ) ); entry.setObject( 0, JSONData.nested ); // the map ctor really expects large numbers of entries to be useful JSONGetFunction function = new JSONGetFunction( singletonMap( new Fields( "result" ), "/person/name" ) ); TupleListCollector result = invokeFunction( function, entry, new Fields( "result" ) ); Object value = result.iterator().next().getObject( 0 ); assertNotNull( value ); assertEquals( "John Doe", ( (TextNode) value ).textValue() ); } }
@Test public void testGet() throws Exception { TupleEntry entry = new TupleEntry( new Fields( "json", JSONCoercibleType.TYPE ), Tuple.size( 1 ) ); entry.setObject( 0, JSONData.nested ); JSONGetFunction function = new JSONGetFunction( new Fields( "result" ), "/person/name" ); TupleListCollector result = invokeFunction( function, entry, new Fields( "result" ) ); Object value = result.iterator().next().getObject( 0 ); assertNotNull( value ); assertEquals( "John Doe", ( (TextNode) value ).textValue() ); }
@Test public void testGet() throws Exception { TupleEntry entry = new TupleEntry( new Fields( "json", JSONCoercibleType.TYPE ), Tuple.size( 1 ) ); entry.setObject( 0, JSONData.nested ); JSONGetFunction function = new JSONGetFunction( new Fields( "result" ), "/person/name" ); TupleListCollector result = invokeFunction( function, entry, new Fields( "result" ) ); Object value = result.iterator().next().getObject( 0 ); assertNotNull( value ); assertEquals( "John Doe", ( (TextNode) value ).textValue() ); }