GsonJSONSerialization implements JSONSerialization { public <T> Serializer from(T object) { return from(object, null); } GsonJSONSerialization(HttpServletResponse response, TypeNameExtractor extractor,
			ProxyInitializer initializer, VRaptorGsonBuilder builder, Serializee serializee); 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 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); HibernateProxy proxy = mock(HibernateProxy.class); when(proxy.getHibernateLazyInitializer()).thenReturn(initializer); Bazinga bazinga = new Bazinga(proxy); serialization.from(bazinga).include("value").serialize(); assertThat(result(), is("{\"bazinga\":{}}")); }
@Test public void shouldSerializeCalendarLikeXstream() { GsonJSONSerialization serialization = serializationWithAdapter(new CalendarSerializer()); Client c = new Client("renan"); c.included = new GregorianCalendar(2012, 8, 3); serialization.from(c).serialize(); String result = result(); String expectedResult = "{\"client\":{\"name\":\"renan\",\"included\":{\"time\":\"" + c.included.getTimeInMillis() + "\",\"timezone\":\"" + c.included.getTimeZone().getID() + "\"}}}"; assertThat(result, is(equalTo(expectedResult))); }
@Test public void shouldSerializeCalendarLikeISO8601() { GsonJSONSerialization serialization = serializationWithAdapter(new CalendarISO8601Serializer(new ISO8601Util())); Client c = new Client("Rafael"); c.included = new GregorianCalendar(2013, 6, 27, 9, 52, 38); c.included.setTimeZone(TimeZone.getTimeZone("America/Sao_Paulo")); serialization.from(c).serialize(); String result = result(); String expectedResult = "{\"client\":{\"name\":\"Rafael\",\"included\":\"2013-07-27T09:52:38.000-0300\"}}"; assertThat(result, is(equalTo(expectedResult))); }
@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 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))); }