// [JACKSON-484] public void testInetAddress() throws IOException { InetAddress address = MAPPER.readValue(quote("127.0.0.1"), InetAddress.class); assertEquals("127.0.0.1", address.getHostAddress()); // should we try resolving host names? That requires connectivity... final String HOST = "google.com"; address = MAPPER.readValue(quote(HOST), InetAddress.class); assertEquals(HOST, address.getHostName()); }
// for [JACKSON-652] // @since 1.9 public void testUntypedWithJsonArrays() throws Exception { // by default we get: Object ob = MAPPER.readValue("[1]", Object.class); assertTrue(ob instanceof List<?>); // but can change to produce Object[]: MAPPER.configure(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY, true); ob = MAPPER.readValue("[1]", Object.class); assertEquals(Object[].class, ob.getClass()); }
// [JACKSON-597] public void testClass() throws IOException { ObjectMapper mapper = new ObjectMapper(); assertSame(String.class, mapper.readValue(quote("java.lang.String"), Class.class)); // then primitive types assertSame(Boolean.TYPE, mapper.readValue(quote("boolean"), Class.class)); assertSame(Byte.TYPE, mapper.readValue(quote("byte"), Class.class)); assertSame(Short.TYPE, mapper.readValue(quote("short"), Class.class)); assertSame(Character.TYPE, mapper.readValue(quote("char"), Class.class)); assertSame(Integer.TYPE, mapper.readValue(quote("int"), Class.class)); assertSame(Long.TYPE, mapper.readValue(quote("long"), Class.class)); assertSame(Float.TYPE, mapper.readValue(quote("float"), Class.class)); assertSame(Double.TYPE, mapper.readValue(quote("double"), Class.class)); assertSame(Void.TYPE, mapper.readValue(quote("void"), Class.class)); }
// [JACKSON-605] public void testClassWithParams() throws IOException { String json = MAPPER.writeValueAsString(new ParamClassBean("Foobar")); ParamClassBean result = MAPPER.readValue(json, ParamClassBean.class); assertEquals("Foobar", result.name); assertSame(String.class, result.clazz); }
/** Related to issues [JACKSON-155], [#170]. */ public void testFile() throws Exception { // Not portable etc... has to do: File src = new File("/test").getAbsoluteFile(); String abs = src.getAbsolutePath(); // escape backslashes (for portability with windows) String json = MAPPER.writeValueAsString(abs); File result = MAPPER.readValue(json, File.class); assertEquals(abs, result.getAbsolutePath()); // Then #170 final ObjectMapper mapper2 = new ObjectMapper(); mapper2.setVisibility(PropertyAccessor.CREATOR, Visibility.NONE); result = mapper2.readValue(json, File.class); assertEquals(abs, result.getAbsolutePath()); }
public void testInetSocketAddress() throws IOException { InetSocketAddress address = MAPPER.readValue(quote("127.0.0.1"), InetSocketAddress.class); assertEquals("127.0.0.1", address.getAddress().getHostAddress()); InetSocketAddress ip6 = MAPPER.readValue(quote("2001:db8:85a3:8d3:1319:8a2e:370:7348"), InetSocketAddress.class); assertEquals("2001:db8:85a3:8d3:1319:8a2e:370:7348", ip6.getAddress().getHostAddress()); InetSocketAddress ip6port = MAPPER.readValue( quote("[2001:db8:85a3:8d3:1319:8a2e:370:7348]:443"), InetSocketAddress.class); assertEquals("2001:db8:85a3:8d3:1319:8a2e:370:7348", ip6port.getAddress().getHostAddress()); assertEquals(443, ip6port.getPort()); // should we try resolving host names? That requires connectivity... final String HOST = "www.ning.com"; address = MAPPER.readValue(quote(HOST), InetSocketAddress.class); assertEquals(HOST, address.getHostName()); }
public void testRegexps() throws IOException { final String PATTERN_STR = "abc:\\s?(\\d+)"; Pattern exp = Pattern.compile(PATTERN_STR); /* Ok: easiest way is to just serialize first; problem * is the backslash */ String json = MAPPER.writeValueAsString(exp); Pattern result = MAPPER.readValue(json, Pattern.class); assertEquals(exp.pattern(), result.pattern()); }
// Test for verifying that Long values are coerced to boolean correctly as well public void testLongToBoolean() throws Exception { long value = 1L + Integer.MAX_VALUE; BooleanBean b = MAPPER.readValue( "{\"primitive\" : " + value + ", \"wrapper\":" + value + ", \"ctor\":" + value + "}", BooleanBean.class); assertEquals(Boolean.TRUE, b.wrapper); assertTrue(b.primitive); assertEquals(Boolean.TRUE, b.ctor); }
// [Issue#239] public void testByteBuffer() throws Exception { byte[] INPUT = new byte[] {1, 3, 9, -1, 6}; String exp = MAPPER.writeValueAsString(INPUT); ByteBuffer result = MAPPER.readValue(exp, ByteBuffer.class); assertNotNull(result); assertEquals(INPUT.length, result.remaining()); for (int i = 0; i < INPUT.length; ++i) { assertEquals(INPUT[i], result.get()); } assertEquals(0, result.remaining()); }
// [JACKSON-888] public void testStackTraceElement() throws Exception { StackTraceElement elem = null; try { throw new IllegalStateException(); } catch (Exception e) { elem = e.getStackTrace()[0]; } String json = MAPPER.writeValueAsString(elem); StackTraceElement back = MAPPER.readValue(json, StackTraceElement.class); assertEquals("testStackTraceElement", back.getMethodName()); assertEquals(elem.getLineNumber(), back.getLineNumber()); assertEquals(elem.getClassName(), back.getClassName()); assertEquals(elem.isNativeMethod(), back.isNativeMethod()); assertTrue(back.getClassName().endsWith("TestJdkTypes")); assertFalse(back.isNativeMethod()); }
// for [JACKSON-616] // @since 1.9 public void testEmptyStringForPrimitives() throws IOException { PrimitivesBean bean; bean = MAPPER.readValue("{\"booleanValue\":\"\"}", PrimitivesBean.class); assertFalse(bean.booleanValue); bean = MAPPER.readValue("{\"byteValue\":\"\"}", PrimitivesBean.class); assertEquals((byte) 0, bean.byteValue); bean = MAPPER.readValue("{\"charValue\":\"\"}", PrimitivesBean.class); assertEquals((char) 0, bean.charValue); bean = MAPPER.readValue("{\"shortValue\":\"\"}", PrimitivesBean.class); assertEquals((short) 0, bean.shortValue); bean = MAPPER.readValue("{\"intValue\":\"\"}", PrimitivesBean.class); assertEquals(0, bean.intValue); bean = MAPPER.readValue("{\"longValue\":\"\"}", PrimitivesBean.class); assertEquals(0L, bean.longValue); bean = MAPPER.readValue("{\"floatValue\":\"\"}", PrimitivesBean.class); assertEquals(0.0f, bean.floatValue); bean = MAPPER.readValue("{\"doubleValue\":\"\"}", PrimitivesBean.class); assertEquals(0.0, bean.doubleValue); }
// by default, should return nulls, n'est pas? public void testEmptyStringForWrappers() throws IOException { WrappersBean bean; // by default, ok to rely on defaults bean = MAPPER.readValue("{\"booleanValue\":\"\"}", WrappersBean.class); assertNull(bean.booleanValue); bean = MAPPER.readValue("{\"byteValue\":\"\"}", WrappersBean.class); assertNull(bean.byteValue); // char/Character is different... not sure if this should work or not: bean = MAPPER.readValue("{\"charValue\":\"\"}", WrappersBean.class); assertNull(bean.charValue); bean = MAPPER.readValue("{\"shortValue\":\"\"}", WrappersBean.class); assertNull(bean.shortValue); bean = MAPPER.readValue("{\"intValue\":\"\"}", WrappersBean.class); assertNull(bean.intValue); bean = MAPPER.readValue("{\"longValue\":\"\"}", WrappersBean.class); assertNull(bean.longValue); bean = MAPPER.readValue("{\"floatValue\":\"\"}", WrappersBean.class); assertNull(bean.floatValue); bean = MAPPER.readValue("{\"doubleValue\":\"\"}", WrappersBean.class); assertNull(bean.doubleValue); }
/** Test for [JACKSON-419] */ public void testLocale() throws IOException { assertEquals(new Locale("en"), MAPPER.readValue(quote("en"), Locale.class)); assertEquals(new Locale("es", "ES"), MAPPER.readValue(quote("es_ES"), Locale.class)); assertEquals( new Locale("FI", "fi", "savo"), MAPPER.readValue(quote("fi_FI_savo"), Locale.class)); }
/** Test for [JACKSON-483], allow handling of CharSequence */ public void testCharSequence() throws IOException { CharSequence cs = MAPPER.readValue("\"abc\"", CharSequence.class); assertEquals(String.class, cs.getClass()); assertEquals("abc", cs.toString()); }
public void testMultiValueArrayException() throws IOException { final ObjectMapper mapper = new ObjectMapper(); mapper.enable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS); try { mapper.readValue("[42,42]", Integer.class); fail( "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled"); } catch (JsonMappingException exp) { // Exception was thrown correctly } try { mapper.readValue("[42,42]", Integer.TYPE); fail( "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled"); } catch (JsonMappingException exp) { // Exception was thrown correctly } try { mapper.readValue("[42.273,42.273]", Double.class); fail( "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled"); } catch (JsonMappingException exp) { // Exception was thrown correctly } try { mapper.readValue("[42.2723,42.273]", Double.TYPE); fail( "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled"); } catch (JsonMappingException exp) { // Exception was thrown correctly } try { mapper.readValue("[42342342342342,42342342342342]", Long.class); fail( "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled"); } catch (JsonMappingException exp) { // Exception was thrown correctly } try { mapper.readValue("[42342342342342342,42342342342342]", Long.TYPE); fail( "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled"); } catch (JsonMappingException exp) { // Exception was thrown correctly } try { mapper.readValue("[42,42]", Short.class); fail( "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled"); } catch (JsonMappingException exp) { // Exception was thrown correctly } try { mapper.readValue("[42,42]", Short.TYPE); fail( "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled"); } catch (JsonMappingException exp) { // Exception was thrown correctly } try { mapper.readValue("[327.2323,327.2323]", Float.class); fail( "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled"); } catch (JsonMappingException exp) { // Exception was thrown correctly } try { mapper.readValue("[82.81902,327.2323]", Float.TYPE); fail( "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled"); } catch (JsonMappingException exp) { // Exception was thrown correctly } try { mapper.readValue("[22,23]", Byte.class); fail( "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled"); } catch (JsonMappingException exp) { // Exception was thrown correctly } try { mapper.readValue("[22,23]", Byte.TYPE); fail( "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled"); } catch (JsonMappingException exp) { // Exception was thrown correctly } try { mapper.readValue(asArray(quote("c") + "," + quote("d")), Character.class); fail( "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled"); } catch (JsonMappingException exp) { // Exception was thrown correctly } try { mapper.readValue(asArray(quote("c") + "," + quote("d")), Character.TYPE); fail( "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled"); } catch (JsonMappingException exp) { // Exception was thrown correctly } try { mapper.readValue("[true,false]", Boolean.class); fail( "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled"); } catch (JsonMappingException exp) { // Exception was thrown correctly } try { mapper.readValue("[true,false]", Boolean.TYPE); fail( "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled"); } catch (JsonMappingException exp) { // Exception was thrown correctly } }
public void testSingleElementArrayException() throws Exception { final ObjectMapper mapper = new ObjectMapper(); mapper.disable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS); try { mapper.readValue("[42]", Integer.class); fail( "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled"); } catch (JsonMappingException exp) { // Exception was thrown correctly } try { mapper.readValue("[42]", Integer.TYPE); fail( "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled"); } catch (JsonMappingException exp) { // Exception was thrown correctly } try { mapper.readValue("[42.273]", Double.class); fail( "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled"); } catch (JsonMappingException exp) { // Exception was thrown correctly } try { mapper.readValue("[42.2723]", Double.TYPE); fail( "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled"); } catch (JsonMappingException exp) { // Exception was thrown correctly } try { mapper.readValue("[42342342342342]", Long.class); fail( "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled"); } catch (JsonMappingException exp) { // Exception was thrown correctly } try { mapper.readValue("[42342342342342342]", Long.TYPE); fail( "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled"); } catch (JsonMappingException exp) { // Exception was thrown correctly } try { mapper.readValue("[42]", Short.class); fail( "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled"); } catch (JsonMappingException exp) { // Exception was thrown correctly } try { mapper.readValue("[42]", Short.TYPE); fail( "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled"); } catch (JsonMappingException exp) { // Exception was thrown correctly } try { mapper.readValue("[327.2323]", Float.class); fail( "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled"); } catch (JsonMappingException exp) { // Exception was thrown correctly } try { mapper.readValue("[82.81902]", Float.TYPE); fail( "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled"); } catch (JsonMappingException exp) { // Exception was thrown correctly } try { mapper.readValue("[22]", Byte.class); fail( "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled"); } catch (JsonMappingException exp) { // Exception was thrown correctly } try { mapper.readValue("[22]", Byte.TYPE); fail( "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled"); } catch (JsonMappingException exp) { // Exception was thrown correctly } try { mapper.readValue("['d']", Character.class); fail( "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled"); } catch (JsonMappingException exp) { // Exception was thrown correctly } try { mapper.readValue("['d']", Character.TYPE); fail( "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled"); } catch (JsonMappingException exp) { // Exception was thrown correctly } try { mapper.readValue("[true]", Boolean.class); fail( "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled"); } catch (JsonMappingException exp) { // Exception was thrown correctly } try { mapper.readValue("[true]", Boolean.TYPE); fail( "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled"); } catch (JsonMappingException exp) { // Exception was thrown correctly } }
// [Issue#381] public void testSingleElementArray() throws Exception { final int intTest = 932832; final double doubleTest = 32.3234; final long longTest = 2374237428374293423L; final short shortTest = (short) intTest; final float floatTest = 84.3743f; final byte byteTest = (byte) 43; final char charTest = 'c'; final ObjectMapper mapper = new ObjectMapper(); mapper.enable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS); final int intValue = mapper.readValue(asArray(intTest), Integer.TYPE); assertEquals(intTest, intValue); final Integer integerWrapperValue = mapper.readValue(asArray(Integer.valueOf(intTest)), Integer.class); assertEquals(Integer.valueOf(intTest), integerWrapperValue); final double doubleValue = mapper.readValue(asArray(doubleTest), Double.class); assertEquals(doubleTest, doubleValue); final Double doubleWrapperValue = mapper.readValue(asArray(Double.valueOf(doubleTest)), Double.class); assertEquals(Double.valueOf(doubleTest), doubleWrapperValue); final long longValue = mapper.readValue(asArray(longTest), Long.TYPE); assertEquals(longTest, longValue); final Long longWrapperValue = mapper.readValue(asArray(Long.valueOf(longTest)), Long.class); assertEquals(Long.valueOf(longTest), longWrapperValue); final short shortValue = mapper.readValue(asArray(shortTest), Short.TYPE); assertEquals(shortTest, shortValue); final Short shortWrapperValue = mapper.readValue(asArray(Short.valueOf(shortTest)), Short.class); assertEquals(Short.valueOf(shortTest), shortWrapperValue); final float floatValue = mapper.readValue(asArray(floatTest), Float.TYPE); assertEquals(floatTest, floatValue); final Float floatWrapperValue = mapper.readValue(asArray(Float.valueOf(floatTest)), Float.class); assertEquals(Float.valueOf(floatTest), floatWrapperValue); final byte byteValue = mapper.readValue(asArray(byteTest), Byte.TYPE); assertEquals(byteTest, byteValue); final Byte byteWrapperValue = mapper.readValue(asArray(Byte.valueOf(byteTest)), Byte.class); assertEquals(Byte.valueOf(byteTest), byteWrapperValue); final char charValue = mapper.readValue(asArray(quote(String.valueOf(charTest))), Character.TYPE); assertEquals(charTest, charValue); final Character charWrapperValue = mapper.readValue(asArray(quote(String.valueOf(charTest))), Character.class); assertEquals(Character.valueOf(charTest), charWrapperValue); final boolean booleanTrueValue = mapper.readValue(asArray(true), Boolean.TYPE); assertTrue(booleanTrueValue); final boolean booleanFalseValue = mapper.readValue(asArray(false), Boolean.TYPE); assertFalse(booleanFalseValue); final Boolean booleanWrapperTrueValue = mapper.readValue(asArray(Boolean.valueOf(true)), Boolean.class); assertEquals(Boolean.TRUE, booleanWrapperTrueValue); }
// [JACKSON-789] public void testCharset() throws Exception { Charset UTF8 = Charset.forName("UTF-8"); assertSame(UTF8, MAPPER.readValue(quote("UTF-8"), Charset.class)); }
/** Test for [JACKSON-420] (add DeserializationConfig.FAIL_ON_NULL_FOR_PRIMITIVES) */ public void testNullForPrimitives() throws IOException { // by default, ok to rely on defaults PrimitivesBean bean = MAPPER.readValue( "{\"intValue\":null, \"booleanValue\":null, \"doubleValue\":null}", PrimitivesBean.class); assertNotNull(bean); assertEquals(0, bean.intValue); assertEquals(false, bean.booleanValue); assertEquals(0.0, bean.doubleValue); bean = MAPPER.readValue( "{\"byteValue\":null, \"longValue\":null, \"floatValue\":null}", PrimitivesBean.class); assertNotNull(bean); assertEquals((byte) 0, bean.byteValue); assertEquals(0L, bean.longValue); assertEquals(0.0f, bean.floatValue); // but not when enabled final ObjectMapper mapper2 = new ObjectMapper(); mapper2.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, true); // boolean try { mapper2.readValue("{\"booleanValue\":null}", PrimitivesBean.class); fail("Expected failure for boolean + null"); } catch (JsonMappingException e) { verifyException(e, "Can not map JSON null into type boolean"); } // byte/char/short/int/long try { mapper2.readValue("{\"byteValue\":null}", PrimitivesBean.class); fail("Expected failure for byte + null"); } catch (JsonMappingException e) { verifyException(e, "Can not map JSON null into type byte"); } try { mapper2.readValue("{\"charValue\":null}", PrimitivesBean.class); fail("Expected failure for char + null"); } catch (JsonMappingException e) { verifyException(e, "Can not map JSON null into type char"); } try { mapper2.readValue("{\"shortValue\":null}", PrimitivesBean.class); fail("Expected failure for short + null"); } catch (JsonMappingException e) { verifyException(e, "Can not map JSON null into type short"); } try { mapper2.readValue("{\"intValue\":null}", PrimitivesBean.class); fail("Expected failure for int + null"); } catch (JsonMappingException e) { verifyException(e, "Can not map JSON null into type int"); } try { mapper2.readValue("{\"longValue\":null}", PrimitivesBean.class); fail("Expected failure for long + null"); } catch (JsonMappingException e) { verifyException(e, "Can not map JSON null into type long"); } // float/double try { mapper2.readValue("{\"floatValue\":null}", PrimitivesBean.class); fail("Expected failure for float + null"); } catch (JsonMappingException e) { verifyException(e, "Can not map JSON null into type float"); } try { mapper2.readValue("{\"doubleValue\":null}", PrimitivesBean.class); fail("Expected failure for double + null"); } catch (JsonMappingException e) { verifyException(e, "Can not map JSON null into type double"); } }