JsonCodec implements MsgCodec { @Override public Object decode(InputStream in) throws IOException { JsonFactory f = new JsonFactory(); JsonParser p = f.createParser(in); JsonToken token = p.nextToken(); if (token == JsonToken.VALUE_NULL) { return null; } else if (token != JsonToken.START_OBJECT) { throw new DecodeException("Expected {"); } return dynamicGroupHandler.readValue(p); } @SuppressWarnings("rawtypes")  JsonCodec(Schema schema, boolean jsSafe); JsonValueHandler<?> getValueHandler(String groupName, String ... fieldNames); @Override void encode(Object group, OutputStream out); @Override void encode(Object group, ByteSink out); void encodeStatic(Object group, OutputStream out); void encodeStatic(Object group, ByteSink out); @Override Object decode(InputStream in); @Override Object decode(ByteSource in); Object decodeStatic(Object groupType, InputStream in); Object decodeStatic(Object groupType, ByteSource in); Object decodeStatic(String groupName, InputStream in); Object decodeStatic(String groupName, ByteSource in);  }
@Test public void testDecodeHelloTypeOutOfOrder() throws Exception { Schema schema = new SchemaBuilder().build(Hello.class); MsgCodec codec = new JsonCodec(schema, false); ByteArrayInputStream in = new ByteArrayInputStream( "{\"greeting\":\"Hello world!\", \"$type\":\"Hello\"}".getBytes(Charset.forName("UTF8"))); assertEquals(new Hello("Hello world!"), codec.decode(in)); }
@Test public void testDecodeNestedTypeOutOfOrder1() throws Exception { Schema schema = new SchemaBuilder().build(Nested.class, Hello.class); MsgCodec codec = new JsonCodec(schema, false); String json = "{\"i\":123, \"$type\":\"Nested\", \"payload\": " + "{\"$type\":\"Hello\", \"greeting\":\"Hello world!\"}" + "}"; ByteArrayInputStream in = new ByteArrayInputStream(json.getBytes(Charset.forName("UTF8"))); assertEquals(new Nested(123, new Hello("Hello world!")), codec.decode(in)); }
@Test public void testDecodeNestedTypeOutOfOrder2() throws Exception { Schema schema = new SchemaBuilder().build(Nested.class, Hello.class); MsgCodec codec = new JsonCodec(schema, false); String json = "{\"i\":123, \"$type\":\"Nested\", \"payload\": " + "{\"greeting\":\"Hello world!\", \"$type\":\"Hello\"}" + "}"; ByteArrayInputStream in = new ByteArrayInputStream(json.getBytes(Charset.forName("UTF8"))); assertEquals(new Nested(123, new Hello("Hello world!")), codec.decode(in)); }
@Test public void testDecodeNestedTypeOutOfOrder3() throws Exception { Schema schema = new SchemaBuilder().build(Nested.class, Hello.class); MsgCodec codec = new JsonCodec(schema, false); String json = "{\"payload\": " + "{\"$type\":\"Hello\", \"greeting\":\"Hello world!\"}" + ", \"i\":123, \"$type\":\"Nested\"}"; ByteArrayInputStream in = new ByteArrayInputStream(json.getBytes(Charset.forName("UTF8"))); assertEquals(new Nested(123, new Hello("Hello world!")), codec.decode(in)); }
@Test(expected = DecodeException.class) public void testDecodeMissingRequiredField() throws IOException { SchemaBuilder builder = new SchemaBuilder(); Schema schema = builder.build(Hello.class); MsgCodec codec = new JsonCodec(schema, false); ByteArrayInputStream in = new ByteArrayInputStream( "{\"$type\":\"Hello\"}".getBytes(Charset.forName("UTF8"))); codec.decode(in); }
@Test public void testDecodeBinaryTypeOutOfOrder() throws IOException { Schema schema = new SchemaBuilder().build(BinaryMessage.class); JsonCodec codec = new JsonCodec(schema, false); String json = "{\"data\":\"AQID\",\"$type\":\"BinaryMessage\"}"; ByteArrayInputStream in = new ByteArrayInputStream(json.getBytes(Charset.forName("UTF8"))); BinaryMessage msg = (BinaryMessage) codec.decode(in); assertEquals(new BinaryMessage(new byte[] {1,2,3}), msg); }
@Test(expected = DecodeException.class) public void testFailDecodeAbstractMessage() throws IOException { Schema schema = new SchemaBuilder().build(AbstractMessage.class); JsonCodec codec = new JsonCodec(schema, false); String json = "{\"$type\":\"AbstractMessage\"}"; ByteArrayInputStream in = new ByteArrayInputStream(json.getBytes(Charset.forName("UTF8"))); codec.decode(in); }