ClientJsonCodec { public static Object decodeWithTypeInfo(StateTree tree, JsonValue json) { if (json.getType() == JsonType.ARRAY) { JsonArray array = (JsonArray) json; int typeId = (int) array.getNumber(0); switch (typeId) { case JsonCodec.NODE_TYPE: { int nodeId = (int) array.getNumber(1); Node domNode = tree.getNode(nodeId).getDomNode(); return domNode; } case JsonCodec.ARRAY_TYPE: return jsonArrayAsJsArray(array.getArray(1)); case JsonCodec.RETURN_CHANNEL_TYPE: return createReturnChannelCallback((int) array.getNumber(1), (int) array.getNumber(2), tree.getRegistry().getServerConnector()); default: throw new IllegalArgumentException( "Unsupported complex type in " + array.toJson()); } } else { return decodeWithoutTypeInfo(json); } } private  ClientJsonCodec(); static StateNode decodeStateNode(StateTree tree, JsonValue json); static Object decodeWithTypeInfo(StateTree tree, JsonValue json); @SuppressWarnings("boxing") static Object decodeWithoutTypeInfo(JsonValue json); static JsonValue encodeWithoutTypeInfo(Object value); static JsArray<Object> jsonArrayAsJsArray(JsonArray jsonArray);  }
@Test public void decodeWithTypeInfo_primitives() { decodePrimitiveValues( json -> ClientJsonCodec.decodeWithTypeInfo(null, json)); }
@Test public void decodeWithTypeInfo_array() { JsonValue json = JsonCodec.encodeWithTypeInfo(JsonUtils .createArray(Json.create("string"), Json.create(true))); Object decoded = ClientJsonCodec.decodeWithTypeInfo(null, json); Assert.assertTrue(decoded instanceof JsArray); JsArray<?> decodedArray = (JsArray<?>) decoded; Assert.assertEquals(2, decodedArray.length()); Assert.assertEquals("string", decodedArray.get(0)); Assert.assertEquals(Boolean.TRUE, decodedArray.get(1)); }
@Test public void decodeWithTypeInfo_element() { StateTree tree = new StateTree(null); StateNode node = new StateNode(42, tree); tree.registerNode(node); JsElement element = new JsElement() { }; node.setDomNode(element); JsonArray json = JsonUtils.createArray(Json.create(JsonCodec.NODE_TYPE), Json.create(node.getId())); Object decoded = ClientJsonCodec.decodeWithTypeInfo(tree, json); Assert.assertSame(element, decoded); }