ClientJsonCodec { public static StateNode decodeStateNode(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); return tree.getNode(nodeId); } case JsonCodec.ARRAY_TYPE: case JsonCodec.RETURN_CHANNEL_TYPE: return null; default: throw new IllegalArgumentException( "Unsupported complex type in " + array.toJson()); } } else { return null; } } 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 decodeStateNode_node() { StateTree tree = new StateTree(null); StateNode node = new StateNode(43, tree); tree.registerNode(node); JsElement element = new JsElement() { }; node.setDomNode(element); JsonArray json = JsonUtils.createArray(Json.create(JsonCodec.NODE_TYPE), Json.create(node.getId())); StateNode decoded = ClientJsonCodec.decodeStateNode(tree, json); Assert.assertSame(node, decoded); }
@Test public void decodeStateNode_array() { JsonValue json = JsonCodec.encodeWithTypeInfo(JsonUtils .createArray(Json.create("string"), Json.create(true))); Assert.assertNull(ClientJsonCodec.decodeStateNode(null, json)); }
@Test public void decodeStateNode_primitive() { Assert.assertNull( ClientJsonCodec.decodeStateNode(null, Json.create("string"))); }