JsonCodec { public static JsonValue encodeWithTypeInfo(Object value) { assert value == null || canEncodeWithTypeInfo(value.getClass()); if (value instanceof Component) { return encodeNode(((Component) value).getElement()); } else if (value instanceof Node<?>) { return encodeNode((Node<?>) value); } else if (value instanceof ReturnChannelRegistration) { return encodeReturnChannel((ReturnChannelRegistration) value); } else { JsonValue encoded = encodeWithoutTypeInfo(value); if (encoded.getType() == JsonType.ARRAY) { encoded = wrapComplexValue(ARRAY_TYPE, encoded); } return encoded; } } 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 encodeWithTypeInfo_basicTypes() { assertJsonEquals(Json.create(true), JsonCodec.encodeWithTypeInfo(Boolean.TRUE)); assertJsonEquals(Json.createNull(), JsonCodec.encodeWithTypeInfo(null)); assertJsonEquals(Json.create(234), JsonCodec.encodeWithTypeInfo(Json.create(234))); assertJsonEquals(Json.create("string"), JsonCodec.encodeWithTypeInfo(Json.create("string"))); assertJsonEquals(Json.createObject(), JsonCodec.encodeWithTypeInfo(Json.createObject())); assertJsonEquals( JsonUtils.createArray(Json.create(JsonCodec.ARRAY_TYPE), Json.createArray()), JsonCodec.encodeWithTypeInfo(Json.createArray())); }
@Test public void encodeWithTypeInfo_attachedElement() { Element element = ElementFactory.createDiv(); StateTree tree = new StateTree(new UI().getInternals(), ElementChildrenList.class); tree.getRootNode().getFeature(ElementChildrenList.class).add(0, element.getNode()); JsonValue json = JsonCodec.encodeWithTypeInfo(element); assertJsonEquals(JsonUtils.createArray(Json.create(JsonCodec.NODE_TYPE), Json.create(element.getNode().getId())), json); }
@Test public void encodeWithTypeInfo_detachedElement() { Element element = ElementFactory.createDiv(); JsonValue json = JsonCodec.encodeWithTypeInfo(element); assertJsonEquals(Json.createNull(), json); }
@Test public void encodeWithTypeInfo_unsupportedTypes() { for (Object value : withTypeInfoUnsupportedValues) { try { JsonCodec.encodeWithTypeInfo(value); Assert.fail("Should throw for " + value.getClass()); } catch (AssertionError expected) { } } }