XStreamJSONPSerialization implements JSONPSerialization { public JSONSerialization withCallback(final String callbackName) { return new XStreamJSONSerialization(response, extractor, initializer, builder) { @Override protected SerializerBuilder getSerializer() { try { final PrintWriter writer = response.getWriter(); final StringWriter out = new StringWriter(); return new XStreamSerializer(super.getXStream(), new PrintWriter(out), extractor, initializer) { @Override public void serialize() { super.serialize(); writer.append(callbackName).append("(").append(out.getBuffer()).append(")"); writer.close(); } }; } catch (IOException e) { throw new ResultException("Unable to serialize data", e); } } }; } XStreamJSONPSerialization(HttpServletResponse response, TypeNameExtractor extractor, ProxyInitializer initializer, XStreamBuilder builder); JSONSerialization withCallback(final String callbackName);  }
@Test public void shouldIncludeCallbackPadding() { String expectedResult = "myCallback({\"order\": {\"price\": 15.0,\"comments\": \"pack it nicely, please\"}})"; Order order = new Order(15.0, "pack it nicely, please"); serialization.withCallback("myCallback").from(order).serialize(); assertThat(result(), is(equalTo(expectedResult))); }
@Test public void shouldSerializeGenericClass() { String expectedResult = "myCallback({\"genericWrapper\": {\"entityList\": [{\"street\": \"vergueiro street\"},{\"street\": \"vergueiro street\"}],\"total\": 2}})"; Collection<Address> entityList = new ArrayList<Address>(); entityList.add(new Address("vergueiro street")); entityList.add(new Address("vergueiro street")); GenericWrapper<Address> wrapper = new GenericWrapper<Address>(entityList, entityList.size()); serialization.withCallback("myCallback").from(wrapper).include("entityList").serialize(); assertThat(result(), is(equalTo(expectedResult))); }