JsonCodec { public static <T> T decodeAs(JsonValue json, Class<T> type) { assert json != null; if (json.getType() == JsonType.NULL && !type.isPrimitive()) { return null; } Class<?> convertedType = ReflectTools.convertPrimitiveType(type); if (type == String.class) { return type.cast(json.asString()); } else if (convertedType == Boolean.class) { return (T) convertedType.cast(Boolean.valueOf(json.asBoolean())); } else if (convertedType == Double.class) { return (T) convertedType.cast(Double.valueOf(json.asNumber())); } else if (convertedType == Integer.class) { return (T) convertedType .cast(Integer.valueOf((int) json.asNumber())); } else if (JsonValue.class.isAssignableFrom(type)) { return type.cast(json); } else { assert !canEncodeWithoutTypeInfo(type); throw new IllegalArgumentException( "Unknown type " + type.getName()); } } private  JsonCodec(); static JsonValue encodeWithTypeInfo(Object value); static boolean canEncodeWithoutTypeInfo(Class<?> type); static boolean canEncodeWithTypeInfo(Class<?> type); static JsonValue encodeWithConstantPool(Object value,
            ConstantPool constantPool); static JsonValue encodeWithoutTypeInfo(Object value); static Serializable decodeWithoutTypeInfo(JsonValue json); static T decodeAs(JsonValue json, Class<T> type); static final int NODE_TYPE; static final int ARRAY_TYPE; static final int RETURN_CHANNEL_TYPE; }
@Test public void decodeAs_booleanJson() { JsonBoolean json = Json.create(true); Assert.assertTrue(JsonCodec.decodeAs(json, Boolean.class)); Assert.assertEquals("true", JsonCodec.decodeAs(json, String.class)); Assert.assertEquals(Integer.valueOf(1), JsonCodec.decodeAs(json, Integer.class)); Assert.assertEquals(Double.valueOf(1.0), JsonCodec.decodeAs(json, Double.class)); Assert.assertEquals(json, JsonCodec.decodeAs(json, JsonValue.class)); }
@Test public void decodeAs_stringJson() { JsonString json = Json.create("Test123 String\n !%"); Assert.assertTrue(JsonCodec.decodeAs(json, Boolean.class)); Assert.assertEquals("Test123 String\n !%", JsonCodec.decodeAs(json, String.class)); Assert.assertEquals(Integer.valueOf(0), JsonCodec.decodeAs(json, Integer.class)); Assert.assertTrue(JsonCodec.decodeAs(json, Double.class).isNaN()); Assert.assertEquals(json, JsonCodec.decodeAs(json, JsonValue.class)); }
@Test public void decodeAs_numberJson() { JsonNumber json = Json.create(15.7); Assert.assertTrue(JsonCodec.decodeAs(json, Boolean.class)); Assert.assertEquals("15.7", JsonCodec.decodeAs(json, String.class)); Assert.assertEquals(Integer.valueOf(15), JsonCodec.decodeAs(json, Integer.class)); Assert.assertEquals(Double.valueOf(15.7), JsonCodec.decodeAs(json, Double.class)); Assert.assertEquals(json, JsonCodec.decodeAs(json, JsonValue.class)); }
@Test public void decodeAs_nullJson() { JsonNull json = Json.createNull(); Assert.assertNull(JsonCodec.decodeAs(json, Boolean.class)); Assert.assertNull(JsonCodec.decodeAs(json, String.class)); Assert.assertNull(JsonCodec.decodeAs(json, Integer.class)); Assert.assertNull(JsonCodec.decodeAs(json, Double.class)); Assert.assertNull(JsonCodec.decodeAs(json, JsonValue.class)); }
@Test public void decodeAs_jsonValue() { JsonObject json = Json.createObject(); json.put("foo", "bar"); Assert.assertEquals("[object Object]", JsonCodec.decodeAs(json, String.class)); Assert.assertEquals(json, JsonCodec.decodeAs(json, JsonValue.class)); Assert.assertTrue(JsonCodec.decodeAs(json, Boolean.class)); Assert.assertNull(JsonCodec.decodeAs(Json.createNull(), Boolean.class)); Assert.assertTrue(JsonCodec.decodeAs(json, boolean.class)); Assert.assertFalse( JsonCodec.decodeAs(Json.createNull(), boolean.class)); Assert.assertEquals(Integer.valueOf(0), JsonCodec.decodeAs(json, Integer.class)); Assert.assertNull(JsonCodec.decodeAs(Json.createNull(), Integer.class)); Assert.assertEquals(Integer.valueOf(0), JsonCodec.decodeAs(json, int.class)); Assert.assertEquals(Integer.valueOf(0), JsonCodec.decodeAs(Json.createNull(), int.class)); Assert.assertNull(JsonCodec.decodeAs(Json.createNull(), Double.class)); Assert.assertTrue(JsonCodec.decodeAs(json, Double.class).isNaN()); Assert.assertTrue(JsonCodec.decodeAs(json, double.class).isNaN()); Assert.assertEquals(0.0d, JsonCodec.decodeAs(Json.createNull(), double.class), 0.0001d); }
@Test(expected = ClassCastException.class) public void decodeAs_jsonValueWrongType_classCastException() { JsonObject json = Json.createObject(); json.put("foo", "bar"); JsonCodec.decodeAs(json, JsonNumber.class); }
@Test(expected = IllegalArgumentException.class) public void decodeAs_unsupportedType() { Assert.assertNull( JsonCodec.decodeAs(Json.create("foo"), float.class)); }