JsonSerializer extends BaseSerializer { @Override protected byte[] serialize(Object data) throws IOException { return gson.toJson(data).getBytes(Charsets.UTF_8); } @Inject  JsonSerializer(Gson gson);   }
@Test public void serializeEmpty() throws Exception { Gson gson = new Gson(); JsonSerializer serializer = new JsonSerializer(gson); Invokable<?, Object> method = Invokable.from(Methods.class.getMethod("empty")); Parameter[] parameters = Iterables.toArray(method.getParameters(), Parameter.class); byte[] bytes = serializer.serialize(parameters, new Object[] { }); Assert.assertArrayEquals("[]".getBytes(Charsets.UTF_8), bytes); }
@Test public void serializeOne() throws Exception { Gson gson = new Gson(); JsonSerializer serializer = new JsonSerializer(gson); Invokable<?, Object> method = Invokable.from(Methods.class.getMethod("one", Pojo.class)); Parameter[] parameters = Iterables.toArray(method.getParameters(), Parameter.class); Pojo pojo = new Pojo("Hello world!", 42); byte[] bytes = serializer.serialize(parameters, new Object[] { pojo }); Assert.assertArrayEquals(gson.toJson(pojo).getBytes(Charsets.UTF_8), bytes); }
@Test public void serializeOneAnnotated() throws Exception { Gson gson = new Gson(); JsonSerializer serializer = new JsonSerializer(gson); Invokable<?, Object> method = Invokable.from(Methods.class.getMethod("oneAnnotated", Pojo.class)); Parameter[] parameters = Iterables.toArray(method.getParameters(), Parameter.class); Pojo pojo = new Pojo("Hello world!", 42); byte[] bytes = serializer.serialize(parameters, new Object[] { pojo }); String expected = gson.toJson(Collections.singletonMap("value", pojo)); JSONAssert.assertEquals(expected, new String(bytes, Charsets.UTF_8), true); }
@Test(expected = IllegalStateException.class) public void serializeMultiMissingAnnotation() throws Exception { Gson gson = new Gson(); JsonSerializer serializer = new JsonSerializer(gson); Invokable<?, Object> method = Invokable.from( Methods.class.getMethod("multiMissingAnnotation", String.class, Pojo.class)); Parameter[] parameters = Iterables.toArray(method.getParameters(), Parameter.class); Pojo pojo = new Pojo("Hello world!", 42); serializer.serialize(parameters, new Object[] { "bad", pojo }); }
@Test(expected = IllegalStateException.class) public void serializeMultiNameDuplication() throws Exception { Gson gson = new Gson(); JsonSerializer serializer = new JsonSerializer(gson); Invokable<?, Object> method = Invokable.from( Methods.class.getMethod("multiNameDuplication", String.class, Pojo.class)); Parameter[] parameters = Iterables.toArray(method.getParameters(), Parameter.class); Pojo pojo = new Pojo("Hello world!", 42); serializer.serialize(parameters, new Object[] { "bad", pojo }); }
@Test public void serializeMulti() throws Exception { Gson gson = new Gson(); JsonSerializer serializer = new JsonSerializer(gson); Invokable<?, Object> method = Invokable.from(Methods.class.getMethod("multi", String.class, Pojo.class)); Parameter[] parameters = Iterables.toArray(method.getParameters(), Parameter.class); Pojo pojo = new Pojo("Hello world!", 42); String string = "bad"; byte[] bytes = serializer.serialize(parameters, new Object[] { string, pojo }); Map<String, Object> map = Maps.newHashMap(); map.put("string", string); map.put("pojo", pojo); String expected = gson.toJson(map); JSONAssert.assertEquals(expected, new String(bytes, Charsets.UTF_8), true); }