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));
}
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));
}
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());
}
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));
}
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)));
}
Aggregations