コード例 #1
0
  @Test
  public void shouldIncludeFieldFromCollection() {
    String expectedResult =
        "{\"order\":{\"id\":1,\"products\":[{\"id\":1,\"creationDate\":\""
            + currentDateAsStr
            + "\",\"name\":\"Product 1\",\"group\":{\"id\":1,\"name\":\"Group 1\"}},{\"id\":2,\"creationDate\":\""
            + currentDateAsStr
            + "\",\"name\":\"Product 2\",\"group\":{\"id\":2,\"name\":\"Group 2\"}}]}}";

    Order order =
        new Order(1L, new Customer(1L, "Franco", new Address("rua", "cidade", "9800989")));
    order.addProduct(createProductWithGroup(1L, 1L));
    order.addProduct(createProductWithGroup(2L, 2L));

    gsonSerialization.from(order).include("products", "products.group").serialize();
    assertThat(jsonResult(), is(equalTo(expectedResult)));
  }
コード例 #2
0
  @Test
  public void shouldExcudeHierarchicalField() {
    String expectedResult =
        "{\"order\":{\"id\":1,\"delivery\":{\"zipCode\":\"09887990\",\"street\":\"delivery street\",\"city\":\"Bristol\"},\"customer\":{\"id\":1,\"name\":\"Franco\"},\"products\":[{\"id\":1,\"creationDate\":\""
            + currentDateAsStr
            + "\",\"name\":\"Product 1\",\"group\":{\"name\":\"Group 1\"}},{\"id\":2,\"creationDate\":\""
            + currentDateAsStr
            + "\",\"name\":\"Product 2\",\"group\":{\"name\":\"Group 2\"}}]}}";

    Order order =
        new Order(
            1L,
            new Customer(1L, "Franco", new Address("rua", "cidade", "9800989")),
            new Address("delivery street", "Bristol", "09887990"));
    order.addProduct(createProductWithGroup(1L, 1L));
    order.addProduct(createProductWithGroup(2L, 2L));

    gsonSerialization
        .from(order)
        .include("customer", "delivery", "products", "products.group")
        .exclude("products.group.id")
        .serialize();
    assertThat(jsonResult(), is(equalTo(expectedResult)));
  }