APISonar


io.ebean.DB.json

> io > ebean > DB > json
io APIs ebean APIs DB APIs json APIs

Example 1
public void testIsSupportedType() {

    JsonContext json = DB.json();
    assertTrue(json.isSupportedType(Customer.class));
    assertFalse(json.isSupportedType(System.class));
  }
Example 2
public void test_toJsonPretty() {
    ResetBasicData.reset();

    List<Order> orders = DB.find(Order.class)
      .select("status")
      .fetch("customer", "id, name")
      .findList();

    String json = DB.json().toJsonPretty(orders);
    assertThat(json).contains("[ {");
    assertThat(json).contains("\"customer\": {");
  }
Example 3
private void jsonToFrom(EcsPerson foundFirst) {
    String asJson = DB.json().toJson(foundFirst);
    EcsPerson fromJson = DB.json().toBean(EcsPerson.class, asJson);
    assertThat(fromJson.getPhoneNumbers()).containsAll(foundFirst.getPhoneNumbers());
  }
Example 4
private void jsonToFrom(EcbmPerson foundFirst) {
    String asJson = DB.json().toJson(foundFirst);

    EcbmPerson fromJson = DB.json().toBean(EcbmPerson.class, asJson);

    String phoneString = fromJson.getPhoneNumbers().toString();
    assertThat(phoneString).contains("64-021-1234");
    assertThat(phoneString).contains("64-021-4321");
  }
Example 5
public void test_unknownProperty() {

    String jsonWithUnknown = "{\"id\":42,\"unknownProp\":\"foo\",\"name\":\"rob\",\"version\":1}";

    Customer customer = DB.json().toBean(Customer.class, jsonWithUnknown);
    assertEquals(Integer.valueOf(42), customer.getId());
    assertEquals("rob", customer.getName());
  }
Example 6
public void json() {

    EcmPerson person = new EcmPerson("Fiona021");
    person.getPhoneNumbers().put("home", "021 1234");
    person.getPhoneNumbers().put("work", "021 4321");

    final String asJson = DB.json().toJson(person);

    assertThat(asJson).isEqualTo("{\"name\":\"Fiona021\",\"phoneNumbers\":{\"home\":\"021 1234\",\"work\":\"021 4321\"}}");

    final EcmPerson fromJson = DB.json().toBean(EcmPerson.class, asJson);
    assertThat(fromJson.getName()).isEqualTo("Fiona021");
    assertThat(fromJson.getPhoneNumbers()).hasSize(2);
    assertThat(fromJson.getPhoneNumbers().toString()).isEqualTo("BeanMap size[2] map{home=021 1234, work=021 4321}");
  }
Example 7
public void test_withVisitor_noUnmapped() {

    String someJsonAllKnown = "{\"id\":42,\"name\":\"rob\",\"version\":1}";

    CustReadVisitor custReadVisitor = new CustReadVisitor();
    JsonReadOptions options = new JsonReadOptions();
    options.addRootVisitor(custReadVisitor);

    Customer customer = DB.json().toBean(Customer.class, someJsonAllKnown, options);
    assertEquals(Integer.valueOf(42), customer.getId());
    assertEquals("rob", customer.getName());

    assertSame(customer, custReadVisitor.bean);
    assertNull(custReadVisitor.unmapped);
  }
Example 8
public void json() {

    EcEnumPerson person = new EcEnumPerson("Enum Person");
    person.getTags().add(EcEnumPerson.Tags.BLUE);
    person.getTags().add(EcEnumPerson.Tags.RED);

    final String asJson = DB.json().toJson(person);

    assertThat(asJson).isEqualTo("{\"name\":\"Enum Person\",\"tags\":[\"BLUE\",\"RED\"]}");

    final EcEnumPerson fromJson = DB.json().toBean(EcEnumPerson.class, asJson);
    assertThat(fromJson.getName()).isEqualTo("Enum Person");
    assertThat(fromJson.getTags()).hasSize(2);
    assertThat(fromJson.getTags().toString()).isEqualTo("BeanSet size[2] set[BLUE, RED]");
  }