Search in sources :

Example 66 with JsonNode

use of com.fasterxml.jackson.databind.JsonNode in project fastjson by alibaba.

the class Bug_0_Test method f_jackson.

private void f_jackson() throws Exception {
    long startNano = System.nanoTime();
    for (int i = 0; i < COUNT; ++i) {
        ObjectMapper mapper = new ObjectMapper();
        ArrayNode node = (ArrayNode) mapper.readTree(text);
        JsonNode head = node.get(0);
        JsonNode body = node.get(1);
    }
    long nano = System.nanoTime() - startNano;
    System.out.println(NumberFormat.getInstance().format(nano));
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 67 with JsonNode

use of com.fasterxml.jackson.databind.JsonNode in project jsonschema2pojo by joelittlejohn.

the class AdditionalPropertiesIT method jacksonCanSerializeOurAdditionalProperties.

@Test
public void jacksonCanSerializeOurAdditionalProperties() throws ClassNotFoundException, IOException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException {
    ClassLoader resultsClassLoader = schemaRule.generateAndCompile("/schema/additionalProperties/defaultAdditionalProperties.json", "com.example");
    Class<?> classWithAdditionalProperties = resultsClassLoader.loadClass("com.example.DefaultAdditionalProperties");
    String jsonWithAdditionalProperties = "{\"a\":1, \"b\":2};";
    Object instanceWithAdditionalProperties = mapper.readValue(jsonWithAdditionalProperties, classWithAdditionalProperties);
    JsonNode jsonNode = mapper.readTree(mapper.writeValueAsString(instanceWithAdditionalProperties));
    assertThat(jsonNode.path("a").asText(), is("1"));
    assertThat(jsonNode.path("b").asInt(), is(2));
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.Test)

Example 68 with JsonNode

use of com.fasterxml.jackson.databind.JsonNode in project jsonschema2pojo by joelittlejohn.

the class CustomDateTimeFormatIT method testCustomDateTimePatternWithDefaultTimezoneWhenFormatDateTimesConfigIsTrue.

/**
     * This tests the class generated when formatDateTimes config option is set to TRUE
     * The field should have @JsonFormat annotation with pattern defined in json schema and UTC timezone
     * It also tests the serialization and deserialization process
     * 
     * @throws Exception
     */
@Test
public void testCustomDateTimePatternWithDefaultTimezoneWhenFormatDateTimesConfigIsTrue() throws Exception {
    Field field = classWhenConfigIsTrue.getDeclaredField("customFormatDefaultTZ");
    JsonFormat annotation = field.getAnnotation(JsonFormat.class);
    assertThat(annotation, notNullValue());
    // Assert that the patterns match
    assertEquals("yyyy-MM-dd'T'HH:mm:ss", annotation.pattern());
    // Assert that the timezones match
    assertEquals("UTC", annotation.timezone());
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setTimeZone(TimeZone.getTimeZone("UTC"));
    ObjectNode node = objectMapper.createObjectNode();
    node.put("customFormatDefaultTZ", "2016-11-06T00:00:00");
    Object pojo = objectMapper.treeToValue(node, classWhenConfigIsTrue);
    Method getter = new PropertyDescriptor("customFormatDefaultTZ", classWhenConfigIsTrue).getReadMethod();
    // Assert that the Date object in the deserialized class is as expected
    assertEquals(dateTimeFormatter.parse("2016-11-06T00:00:00").toString(), getter.invoke(pojo).toString());
    JsonNode jsonVersion = objectMapper.valueToTree(pojo);
    // Assert that when the class is serialized, the date object is serialized as expected 
    assertEquals("2016-11-06T00:00:00", jsonVersion.get("customFormatDefaultTZ").asText());
}
Also used : Field(java.lang.reflect.Field) JsonFormat(com.fasterxml.jackson.annotation.JsonFormat) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) PropertyDescriptor(java.beans.PropertyDescriptor) JsonNode(com.fasterxml.jackson.databind.JsonNode) Method(java.lang.reflect.Method) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 69 with JsonNode

use of com.fasterxml.jackson.databind.JsonNode in project jsonschema2pojo by joelittlejohn.

the class EnumIT method intEnumIsSerializedCorrectly.

@Test
@SuppressWarnings("unchecked")
public void intEnumIsSerializedCorrectly() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, JsonParseException, JsonMappingException, IOException, InstantiationException {
    ClassLoader resultsClassLoader = schemaRule.generateAndCompile("/schema/enum/integerEnumToSerialize.json", "com.example");
    // the schema for a valid instance
    Class<?> typeWithEnumProperty = resultsClassLoader.loadClass("com.example.enums.IntegerEnumToSerialize");
    Class<Enum> enumClass = (Class<Enum>) resultsClassLoader.loadClass("com.example.enums.IntegerEnumToSerialize$TestEnum");
    // create an instance
    Object valueWithEnumProperty = typeWithEnumProperty.newInstance();
    Method enumSetter = typeWithEnumProperty.getMethod("setTestEnum", enumClass);
    // call setTestEnum(TestEnum.ONE)
    enumSetter.invoke(valueWithEnumProperty, enumClass.getEnumConstants()[0]);
    ObjectMapper objectMapper = new ObjectMapper();
    // write our instance out to json
    String jsonString = objectMapper.writeValueAsString(valueWithEnumProperty);
    JsonNode jsonTree = objectMapper.readTree(jsonString);
    assertThat(jsonTree.size(), is(1));
    assertThat(jsonTree.has("testEnum"), is(true));
    assertThat(jsonTree.get("testEnum").isIntegralNumber(), is(true));
    assertThat(jsonTree.get("testEnum").asInt(), is(1));
}
Also used : BeforeClass(org.junit.BeforeClass) JsonNode(com.fasterxml.jackson.databind.JsonNode) Method(java.lang.reflect.Method) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 70 with JsonNode

use of com.fasterxml.jackson.databind.JsonNode in project jsonschema2pojo by joelittlejohn.

the class ContentResolverNetworkTest method httpLinkIsResolvedToContent.

@Test
public void httpLinkIsResolvedToContent() {
    URI httpUri = URI.create("http://" + ADDRESS + ":" + server.port() + "/address.json");
    JsonNode uriContent = resolver.resolve(httpUri);
    assertThat(uriContent.path("description").asText().length(), is(greaterThan(0)));
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) URI(java.net.URI) Test(org.junit.Test)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)879 Test (org.junit.Test)224 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)217 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)142 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)125 IOException (java.io.IOException)91 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)86 HashMap (java.util.HashMap)79 HttpGet (org.apache.http.client.methods.HttpGet)68 JsonException (jmri.server.json.JsonException)66 Deployment (org.activiti.engine.test.Deployment)66 ArrayList (java.util.ArrayList)62 InputStream (java.io.InputStream)55 StringEntity (org.apache.http.entity.StringEntity)54 ByteArrayInputStream (java.io.ByteArrayInputStream)51 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)49 Task (org.activiti.engine.task.Task)41 HttpPost (org.apache.http.client.methods.HttpPost)39 Map (java.util.Map)33 Tree (org.apache.jackrabbit.oak.api.Tree)30