XStreamJSONSerialization implements JSONSerialization { public <T> Serializer from(T object) { return from(object, null); } XStreamJSONSerialization(HttpServletResponse response, TypeNameExtractor extractor, ProxyInitializer initializer, XStreamBuilder builder); boolean accepts(String format); Serializer from(T object); Serializer from(T object, String alias); NoRootSerialization withoutRoot(); JSONSerialization indented();  }
@Test public void shouldSerializeGenericClass() { String expectedResult = "{\"genericWrapper\": {\"entityList\": [{\"name\": \"washington botelho\"},{\"name\": \"washington botelho\"}],\"total\": 2}}"; Collection<Client> entityList = new ArrayList<Client>(); entityList.add(new Client("washington botelho")); entityList.add(new Client("washington botelho")); GenericWrapper<Client> wrapper = new GenericWrapper<Client>(entityList, entityList.size()); serialization.from(wrapper).include("entityList").serialize(); assertThat(result(), is(equalTo(expectedResult))); }
@Test public void shouldSerializeAllBasicFields() { String expectedResult = "{\"order\": {\"price\": 15.0,\"comments\": \"pack it nicely, please\"}}"; Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please"); serialization.from(order).serialize(); assertThat(result(), is(equalTo(expectedResult))); }
@Test public void shouldUseAlias() { String expectedResult = "{\"customOrder\": {\"price\": 15.0,\"comments\": \"pack it nicely, please\"}}"; Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please"); serialization.from(order, "customOrder").serialize(); assertThat(result(), is(equalTo(expectedResult))); }
@Test public void shouldSerializeEnumFields() { Order order = new BasicOrder(new Client("guilherme silveira"), 15.0, "pack it nicely, please", Type.basic); serialization.from(order).serialize(); String result = result(); assertThat(result, containsString("\"type\": \"basic\"")); }
@Test public void shouldSerializeCollection() { String expectedResult = "{\"price\": 15.0,\"comments\": \"pack it nicely, please\"}"; expectedResult += ',' + expectedResult; expectedResult = "{\"list\": [" + expectedResult + "]}"; Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please"); serialization.from(Arrays.asList(order, order)).serialize(); assertThat(result(), is(equalTo(expectedResult))); }
@Test public void shouldSerializeCollectionWithPrefixTag() { String expectedResult = "{\"price\": 15.0,\"comments\": \"pack it nicely, please\"}"; expectedResult += ',' + expectedResult; expectedResult = "{\"orders\": [" + expectedResult + "]}"; Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please"); serialization.from(Arrays.asList(order, order), "orders").serialize(); assertThat(result(), is(equalTo(expectedResult))); }
@Test public void shouldExcludeNonPrimitiveFieldsFromACollection() { Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please", new Item("name", 12.99)); serialization.from(Arrays.asList(order, order), "orders").exclude("price").serialize(); assertThat(result(), not(containsString("\"items\""))); assertThat(result(), not(containsString("name"))); assertThat(result(), not(containsString("\"price\""))); assertThat(result(), not(containsString("12.99"))); assertThat(result(), not(containsString("15.0"))); }
@Test public void shouldSerializeParentFields() { Order order = new AdvancedOrder(null, 15.0, "pack it nicely, please", "complex package"); serialization.from(order).serialize(); assertThat(result(), containsString("\"notes\": \"complex package\"")); }
@Test public void shouldExcludeNonPrimitiveParentFields() { WithAdvanced advanced = new WithAdvanced(); advanced.order = new AdvancedOrder(new Client("john"), 15.0, "pack it nicely, please", "complex package"); serialization.from(advanced).include("order").serialize(); assertThat(result(), not(containsString("\"client\""))); }
@Test public void shouldExcludeParentFields() { Order order = new AdvancedOrder(null, 15.0, "pack it nicely, please", "complex package"); serialization.from(order).exclude("comments").serialize(); assertThat(result(), not(containsString("\"comments\""))); }
@Test public void shouldExcludeAllPrimitiveFields() { String expectedResult = "{\"order\": {}}"; Order order = new Order(new Client("nykolas lima"), 15.0, "gift bags, please"); serialization.from(order).excludeAll().serialize(); assertThat(result(), is(equalTo(expectedResult))); }
@Test public void shouldExcludeAllPrimitiveParentFields() { String expectedResult = "{\"advancedOrder\": {}}"; Order order = new AdvancedOrder(null, 15.0, "pack it nicely, please", "complex package"); serialization.from(order).excludeAll().serialize(); assertThat(result(), is(equalTo(expectedResult))); }
@Test public void shouldExcludeAllPrimitiveFieldsInACollection() { String expectedResult = "{\"list\": [{},{}]}"; List<Order> orders = new ArrayList<Order>(); orders.add(new Order(new Client("nykolas lima"), 15.0, "gift bags, please")); orders.add(new Order(new Client("Rafael Dipold"), 15.0, "gift bags, please")); serialization.from(orders).excludeAll().serialize(); assertThat(result(), is(equalTo(expectedResult))); }
@Test public void shouldExcludeAllThanIncludeAndSerialize() { String expectedResult = "{\"order\": {\"price\": 15.0}}"; Order order = new Order(new Client("nykolas lima"), 15.0, "gift bags, please"); serialization.from(order).excludeAll().include("price").serialize(); assertThat(result(), is(equalTo(expectedResult))); }
@Test public void shouldOptionallyExcludeFields() { String expectedResult = "{\"order\": {\"comments\": \"pack it nicely, please\"}}"; Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please"); serialization.from(order).exclude("price").serialize(); assertThat(result(), is(equalTo(expectedResult))); }
@Test public void shouldOptionallyIncludeFieldAndNotItsNonPrimitiveFields() { Order order = new Order(new Client("guilherme silveira", new Address("R. Vergueiro")), 15.0, "pack it nicely, please"); serialization.from(order).include("client").serialize(); assertThat(result(), containsString("\"name\": \"guilherme silveira\"")); assertThat(result(), not(containsString("R. Vergueiro"))); }
@Test public void shouldOptionallyIncludeChildField() { Order order = new Order(new Client("guilherme silveira", new Address("R. Vergueiro")), 15.0, "pack it nicely, please"); serialization.from(order).include("client", "client.address").serialize(); assertThat(result(), containsString("\"street\": \"R. Vergueiro\"")); }
@Test public void shouldOptionallyExcludeChildField() { Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please"); serialization.from(order).include("client").exclude("client.name").serialize(); assertThat(result(), containsString("\"client\"")); assertThat(result(), not(containsString("guilherme silveira"))); }
@Test public void shouldOptionallyIncludeListChildFields() { Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please", new Item("any item", 12.99)); serialization.from(order).include("items").serialize(); assertThat(result(), containsString("\"items\"")); assertThat(result(), containsString("\"name\": \"any item\"")); assertThat(result(), containsString("\"price\": 12.99")); }
@Test public void shouldOptionallyExcludeFieldsFromIncludedListChildFields() { Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please", new Item("any item", 12.99)); serialization.from(order).include("items").exclude("items.price").serialize(); assertThat(result(), containsString("\"items\"")); assertThat(result(), containsString("\"name\": \"any item\"")); assertThat(result(), not(containsString("12.99"))); }
@Test public void shouldRunHibernateLazyInitialization() throws Exception { LazyInitializer initializer = mock(LazyInitializer.class); SomeProxy proxy = new SomeProxy(initializer); proxy.name = "my name"; proxy.aField = "abc"; when(initializer.getPersistentClass()).thenReturn(Client.class); serialization.from(proxy).serialize(); assertThat(result(), is("{\"client\": {\"name\": \"my name\",\"aField\": \"abc\"}}")); verify(initializer).initialize(); }