/** * Parse a text representation of a JSON data structure * * @param text JSON text to parse * @return a data structure of lists and maps */ public Object parseText(String text) { if (text == null || text.length() == 0) { throw new IllegalArgumentException("The JSON input text should neither be null nor empty."); } return JsonFactory.create().fromJson ( text ); }
@Test public void test_caseInsensitiveProperty_normal() { String json = "{\"typeName\":\"Processes\",\"fields\":[{\"name\":\"process\",\"type\":\"ConversionRateProcess[]\",\"properties\":[\"REQUIRED\"]}]} "; ApiDynamicType map = objectMapper.fromJson(json, ApiDynamicType.class); puts(json); puts(objectMapper.toJson(map)); assertThat(objectMapper.fromJson(objectMapper.toJson(map)), is(objectMapper.fromJson("{\"typeName\":\"Processes\",\"fields\":[{\"name\":\"process\",\"type\":\"ConversionRateProcess[]\",\"properties\":[\"REQUIRED\"]}]}"))); }
@Test public void test_stringArrayProperty_fromSingleValue() { String json = "{\"typeName\":\"Processes\",\"descriptions\":\"lala\"}"; ApiDynamicType map = objectMapper.fromJson(json, ApiDynamicType.class); puts(json); puts(objectMapper.toJson(map)); assertThat(objectMapper.fromJson(objectMapper.toJson(map)), is(objectMapper.fromJson("{\"typeName\":\"Processes\",\"descriptions\":[\"lala\"]}"))); }
@Test public void test_caseInsensitiveProperty_uppercase() { String json = "{\"TYPENAME\":\"Processes\",\"fields\":[{\"name\":\"process\",\"type\":\"ConversionRateProcess[]\",\"properties\":[\"REQUIRED\"]}]} "; ApiDynamicType map = objectMapper.fromJson(json, ApiDynamicType.class); puts(json); puts(objectMapper.toJson(map)); assertThat(objectMapper.fromJson(objectMapper.toJson(map)), is(objectMapper.fromJson("{\"typeName\":\"Processes\",\"fields\":[{\"name\":\"process\",\"type\":\"ConversionRateProcess[]\",\"properties\":[\"REQUIRED\"]}]}"))); }
@Test public void test_caseInsensitiveProperty_normal() { String json = "{\"typeName\":\"Processes\",\"fields\":[{\"name\":\"process\",\"type\":\"ConversionRateProcess[]\",\"properties\":[\"REQUIRED\"]}]} "; ApiDynamicType map = objectMapper.fromJson(json, ApiDynamicType.class); puts(json); puts(objectMapper.toJson(map)); assertThat(objectMapper.fromJson(objectMapper.toJson(map)), is(objectMapper.fromJson("{\"typeName\":\"Processes\",\"fields\":[{\"name\":\"process\",\"type\":\"ConversionRateProcess[]\",\"properties\":[\"REQUIRED\"]}]}"))); }
@Test public void test_stringArrayProperty() { String json = "{\"typeName\":\"Processes\",\"descriptions\":[\"lala\"]}"; ApiDynamicType map = objectMapper.fromJson(json, ApiDynamicType.class); puts(json); puts(objectMapper.toJson(map)); assertThat(objectMapper.fromJson(objectMapper.toJson(map)), is(objectMapper.fromJson("{\"typeName\":\"Processes\",\"descriptions\":[\"lala\"]}"))); }
@Test public void test_stringListProperty() { String json = "{\"typeName\":\"Processes\",\"descriptions2\":[\"lala\"]} "; ApiDynamicType map = objectMapper.fromJson(json, ApiDynamicType.class); puts(json); puts(objectMapper.toJson(map)); assertThat(objectMapper.fromJson(objectMapper.toJson(map)), is(objectMapper.fromJson("{\"typeName\":\"Processes\",\"descriptions2\":[\"lala\"]}"))); }
@Test public void test_caseInsensitiveProperty_uppercase() { String json = "{\"TYPENAME\":\"Processes\",\"fields\":[{\"name\":\"process\",\"type\":\"ConversionRateProcess[]\",\"properties\":[\"REQUIRED\"]}]} "; ApiDynamicType map = objectMapper.fromJson(json, ApiDynamicType.class); puts(json); puts(objectMapper.toJson(map)); assertThat(objectMapper.fromJson(objectMapper.toJson(map)), is(objectMapper.fromJson("{\"typeName\":\"Processes\",\"fields\":[{\"name\":\"process\",\"type\":\"ConversionRateProcess[]\",\"properties\":[\"REQUIRED\"]}]}"))); }
@Test public void test_caseInsensitiveProperty_lowercase() { String json = "{\"typename\":\"Processes\",\"fields\":[{\"name\":\"process\",\"type\":\"ConversionRateProcess[]\",\"properties\":[\"REQUIRED\"]}]} "; ApiDynamicType map = objectMapper.fromJson(json, ApiDynamicType.class); puts(json); puts(objectMapper.toJson(map)); assertThat(objectMapper.fromJson(objectMapper.toJson(map)), is(objectMapper.fromJson("{\"typeName\":\"Processes\",\"fields\":[{\"name\":\"process\",\"type\":\"ConversionRateProcess[]\",\"properties\":[\"REQUIRED\"]}]}"))); }
@Test public void test_caseInsensitiveProperty_normal() { String json = "{\"typeName\":\"Processes\",\"fields\":[{\"name\":\"process\",\"type\":\"ConversionRateProcess[]\",\"properties\":[\"REQUIRED\"]}]} "; ApiDynamicType map = objectMapper.fromJson(json, ApiDynamicType.class); puts(json); puts(objectMapper.toJson(map)); assertThat(objectMapper.fromJson(objectMapper.toJson(map)), is(objectMapper.fromJson("{\"typeName\":\"Processes\",\"fields\":[{\"name\":\"process\",\"type\":\"ConversionRateProcess[]\",\"properties\":[\"REQUIRED\"]}]}"))); }
@Test public void test_caseInsensitiveProperty_uppercase() { String json = "{\"TYPENAME\":\"Processes\",\"fields\":[{\"name\":\"process\",\"type\":\"ConversionRateProcess[]\",\"properties\":[\"REQUIRED\"]}]} "; ApiDynamicType map = objectMapper.fromJson(json, ApiDynamicType.class); puts(json); puts(objectMapper.toJson(map)); assertThat(objectMapper.fromJson(objectMapper.toJson(map)), is(objectMapper.fromJson("{\"typeName\":\"Processes\",\"fields\":[{\"name\":\"process\",\"type\":\"ConversionRateProcess[]\",\"properties\":[\"REQUIRED\"]}]}"))); }
@Test public void test_arrayProperty() { String json = "{\"typeName\":\"Processes\",\"fields\":[{\"name\":\"process\",\"type\":\"ConversionRateProcess[]\",\"properties\":[\"REQUIRED\"]}]} "; ApiDynamicType map = objectMapper.fromJson(json, ApiDynamicType.class); puts(json); puts(objectMapper.toJson(map)); assertThat(objectMapper.fromJson(objectMapper.toJson(map)), is(objectMapper.fromJson("{\"typeName\":\"Processes\",\"fields\":[{\"name\":\"process\",\"type\":\"ConversionRateProcess[]\",\"properties\":[\"REQUIRED\"]}]}"))); }
@Test public void test_caseInsensitiveProperty_lowercase() { String json = "{\"typename\":\"Processes\",\"fields\":[{\"name\":\"process\",\"type\":\"ConversionRateProcess[]\",\"properties\":[\"REQUIRED\"]}]} "; ApiDynamicType map = objectMapper.fromJson(json, ApiDynamicType.class); puts(json); puts(objectMapper.toJson(map)); assertThat(objectMapper.fromJson(objectMapper.toJson(map)), is(objectMapper.fromJson("{\"typeName\":\"Processes\",\"fields\":[{\"name\":\"process\",\"type\":\"ConversionRateProcess[]\",\"properties\":[\"REQUIRED\"]}]}"))); }
@Test public void test_arrayProperty_fromSingleValue() { String json = "{\"typeName\":\"Processes\",\"fields\":{\"name\":\"process\",\"type\":\"ConversionRateProcess[]\",\"properties\":[\"REQUIRED\"]}} "; ApiDynamicType map = objectMapper.fromJson(json, ApiDynamicType.class); puts(json); puts(objectMapper.toJson(map)); assertThat(objectMapper.fromJson(objectMapper.toJson(map)), is(objectMapper.fromJson("{\"typeName\":\"Processes\",\"fields\":[{\"name\":\"process\",\"type\":\"ConversionRateProcess[]\",\"properties\":[\"REQUIRED\"]}]}"))); }
@Test public void test_listProperty_fromSingleValue() { String json = "{\"typeName\":\"Processes\",\"fields2\":{\"name\":\"process\",\"type\":\"ConversionRateProcess[]\",\"properties\":[\"REQUIRED\"]}} "; ApiDynamicType map = objectMapper.fromJson(json, ApiDynamicType.class); puts(json); puts(objectMapper.toJson(map)); assertThat(objectMapper.fromJson(objectMapper.toJson(map)), is(objectMapper.fromJson("{\"typeName\":\"Processes\",\"fields2\":[{\"name\":\"process\",\"type\":\"ConversionRateProcess[]\",\"properties\":[\"REQUIRED\"]}]}"))); }
@Test public void test_stringListProperty_fromSingleValue() { String json = "{\"typeName\":\"Processes\",\"descriptions2\":\"lala\"} "; ApiDynamicType map = objectMapper.fromJson(json, ApiDynamicType.class); puts(json); puts(objectMapper.toJson(map)); assertThat(objectMapper.fromJson(objectMapper.toJson(map)), is(objectMapper.fromJson("{\"typeName\":\"Processes\",\"descriptions2\":[\"lala\"]}"))); }
@Test public void test_caseInsensitiveProperty_lowercase() { String json = "{\"typename\":\"Processes\",\"fields\":[{\"name\":\"process\",\"type\":\"ConversionRateProcess[]\",\"properties\":[\"REQUIRED\"]}]} "; ApiDynamicType map = objectMapper.fromJson(json, ApiDynamicType.class); puts(json); puts(objectMapper.toJson(map)); assertThat(objectMapper.fromJson(objectMapper.toJson(map)), is(objectMapper.fromJson("{\"typeName\":\"Processes\",\"fields\":[{\"name\":\"process\",\"type\":\"ConversionRateProcess[]\",\"properties\":[\"REQUIRED\"]}]}"))); }
@Test public void test_caseInsensitiveProperty_normal() { String json = "{\"typeName\":\"Processes\",\"fields\":[{\"name\":\"process\",\"type\":\"ConversionRateProcess[]\",\"properties\":[\"REQUIRED\"]}]} "; ApiDynamicType map = objectMapper.fromJson(json, ApiDynamicType.class); puts(json); puts(objectMapper.toJson(map)); assertThat(objectMapper.fromJson(objectMapper.toJson(map)), is(objectMapper.fromJson("{\"typeName\":\"Processes\",\"fields\":[{\"name\":\"process\",\"type\":\"ConversionRateProcess[]\",\"properties\":[\"REQUIRED\"]}]}"))); }
@Test public void test2() { user = new User("red", "yellow", "green", "purple"); puts(user.getFavoriteColors()); final ObjectMapper objectMapper = JsonFactory.createUseAnnotations(true); json = objectMapper.toJson(user); puts(json); user2 = objectMapper.fromJson(json, User.class); puts(user.getFavoriteColors()); ok = user.equals(user2) || die("Users should be equal", user, user2); }
public void test3() { user = new User("red", "yellow", "green", "purple"); puts(user.getFavoriteColors()); final JsonParserFactory jsonParserFactory = new JsonParserFactory().usePropertiesFirst().useAnnotations(); final JsonSerializerFactory serializerFactory = new JsonSerializerFactory().includeNulls().useAnnotations(); final ObjectMapper objectMapper = new ObjectMapperImpl(jsonParserFactory, serializerFactory); json = objectMapper.toJson(user); puts(json); user2 = objectMapper.fromJson(json, User.class); puts(user.getFavoriteColors()); ok = user.equals(user2) || die("Users should be equal", user, user2); }