Java Code Examples for com.fasterxml.jackson.databind.ObjectMapper#treeAsTokens()

The following examples show how to use com.fasterxml.jackson.databind.ObjectMapper#treeAsTokens() . 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: InstantDeserializerTest.java    From java-gitlab-api with Apache License 2.0 6 votes vote down vote up
@Test
void deserialize() throws IOException {
    final ObjectMapper objectMapper = new ObjectMapper();

    final InstantDeserializer deserializer = new InstantDeserializer();
    final JsonParser parser = objectMapper.treeAsTokens(objectMapper.readTree("\"2016-08-11T11:28:34.085Z\""));
    parser.nextToken();
    final Instant instant = deserializer.deserialize(parser, objectMapper.getDeserializationContext());

    assertEquals(Instant.from(
        ZonedDateTime.of(
            LocalDate.of(2016, 8, 11),
            LocalTime.of(11, 28, 34, (int) TimeUnit.MILLISECONDS.toNanos(85)),
            ZoneOffset.UTC
        )
    ), instant);
}
 
Example 2
Source File: CommandJson.java    From concursus with MIT License 5 votes vote down vote up
private BiFunction<JsonNode, Type, Object> makeDeserialiser(ObjectMapper mapper) {
    final TypeFactory typeFactory = mapper.getTypeFactory();
    return (node, type) -> {
        JavaType javaType = typeFactory.constructType(type);
        try {
            final JsonParser jsonParser = mapper.treeAsTokens(node);
            final ObjectCodec codec = jsonParser.getCodec();

            return codec.readValue(jsonParser, javaType);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    };
}
 
Example 3
Source File: EventJson.java    From concursus with MIT License 5 votes vote down vote up
private BiFunction<JsonNode, Type, Object> makeDeserialiser(ObjectMapper mapper) {
    final TypeFactory typeFactory = mapper.getTypeFactory();
    return (node, type) -> {
        JavaType javaType = typeFactory.constructType(type);
        try {
            final JsonParser jsonParser = mapper.treeAsTokens(node);
            final ObjectCodec codec = jsonParser.getCodec();

            return codec.readValue(jsonParser, javaType);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    };
}
 
Example 4
Source File: ObjectMapperHelper.java    From jackson-datatype-protobuf with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T extends MessageOrBuilder> List<T> writeAndReadBack(ObjectMapper mapper, List<T> values) {
  if (values.isEmpty()) {
    return Collections.emptyList();
  }

  Class<T> messageType = (Class<T>) values.get(0).getClass();
  JsonParser parser = mapper.treeAsTokens(toTree(mapper, values));

  try {
    return Lists.newArrayList(mapper.readValues(parser, messageType));
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}