JsonCodec { public static JsonValue encodeWithoutTypeInfo(Object value) { if (value == null) { return Json.createNull(); } assert canEncodeWithoutTypeInfo(value.getClass()); Class<?> type = value.getClass(); if (String.class.equals(value.getClass())) { return Json.create((String) value); } else if (Integer.class.equals(type) || Double.class.equals(type)) { return Json.create(((Number) value).doubleValue()); } else if (Boolean.class.equals(type)) { return Json.create(((Boolean) value).booleanValue()); } else if (JsonValue.class.isAssignableFrom(type)) { return (JsonValue) value; } assert !canEncodeWithoutTypeInfo(type); throw new IllegalArgumentException( "Can't encode " + value.getClass() + " to json"); } 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 encodeWithoutTypeInfo_supportedTypes() { assertJsonEquals(Json.create(true), JsonCodec.encodeWithoutTypeInfo(Boolean.TRUE)); assertJsonEquals(Json.create("string"), JsonCodec.encodeWithoutTypeInfo("string")); assertJsonEquals(Json.create(3.14), JsonCodec.encodeWithoutTypeInfo(Double.valueOf(3.14))); assertJsonEquals(Json.create(42), JsonCodec.encodeWithoutTypeInfo(Integer.valueOf(42))); assertJsonEquals(Json.createNull(), JsonCodec.encodeWithoutTypeInfo(null)); JsonObject json = Json.createObject(); json.put("foo", "bar"); assertJsonEquals(json, JsonCodec.encodeWithoutTypeInfo(json)); assertJsonEquals(Json.createNull(), Json.createNull()); assertJsonEquals(Json.create(false), Json.create(false)); assertJsonEquals(Json.create(234), Json.create(234)); assertJsonEquals(Json.create("string"), Json.create("string")); assertJsonEquals(json, json); assertJsonEquals(Json.createArray(), Json.createArray()); }
@Test public void encodeWithoutTypeInfo_unsupportedTypes() { List<Object> unsupported = new ArrayList<>( withTypeInfoUnsupportedValues); unsupported.add(ElementFactory.createDiv()); for (Object value : unsupported) { try { JsonCodec.encodeWithoutTypeInfo(value); Assert.fail("Should throw for " + value.getClass()); } catch (AssertionError expected) { } } }