APISonar


com.avaje.ebean.Ebean.json

> com > avaje > ebean > Ebean > json
com APIs avaje APIs ebean APIs Ebean APIs json APIs

Example 1
public void toJson() {

    LocalTime now = new LocalTime();

    TJodaEntity bean = new TJodaEntity();
    bean.setId(42);
    bean.setLocalTime(now);

    String json = Ebean.json().toJson(bean);
    TJodaEntity bean1 = Ebean.json().toBean(TJodaEntity.class, json);

    assertEquals(bean1.getLocalTime(), now);
  }
Example 2
public void testToBeanToJson() throws Exception {

    String rawJson = "{\"name\":\"entityBeanName\",\"basic\":true,\"someBean\":{\"name\":\"transientBeanName\",\"baz\":\"foo\"}}";

    EJsonTransientObject bean = Ebean.json().toBean(EJsonTransientObject.class, rawJson);

    assertEquals("entityBeanName",bean.getName());
    assertEquals(true,bean.getBasic());
    assertEquals("transientBeanName",bean.getSomeBean().name);
    assertEquals("foo",bean.getSomeBean().baz);

    String asJson = Ebean.json().toJson(bean);

    assertEquals(rawJson, asJson);
  }
Example 3
public void test_unknownProperty() {

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

    Customer customer = Ebean.json().toBean(Customer.class, jsonWithUnknown);
    assertEquals(Integer.valueOf(42), customer.getId());
    assertEquals("rob", customer.getName());
  }
Example 4
public void testToJson_with_transientExcludeFromPathProperties() throws Exception {

    EJsonTransientEntityList bean = new EJsonTransientEntityList();
    bean.setId(99L);
    bean.setName("John");

    PathProperties pathProps = PathProperties.parse("id,name");

    String asJson = Ebean.json().toJson(bean, pathProps);

    assertThat(asJson).isEqualTo("{\"id\":99,\"name\":\"John\"}");
  }
Example 5
public void testToBeanToJson_NonNull() throws Exception {

    EJsonTransientList bean = new EJsonTransientList();
    bean.setId(99L);
    bean.setFileNames(new ArrayList<String>());

    JsonWriteOptions options = new JsonWriteOptions();
    options.setInclude(JsonConfig.Include.NON_NULL);

    String asJson = Ebean.json().toJson(bean, options);

    String expectedJson = "{\"id\":99,\"fileNames\":[]}";

    assertEquals(expectedJson, asJson);
  }
Example 6
public void json_parse_format() {

    String asJson = Ebean.json().toJson(found);
    assertThat(asJson).contains("\"phoneNumbers\":[\"4321\",\"9823\"]");
    assertThat(asJson).contains("\"id\":");

    EArrayBean fromJson = Ebean.json().toBean(EArrayBean.class, asJson);
    assertEquals(found.getId(), fromJson.getId());
    assertEquals(found.getId(), fromJson.getId());
    assertEquals(found.getName(), fromJson.getName());
    assertThat(fromJson.getPhoneNumbers()).containsExactly("4321", "9823");
  }
Example 7
public void test() throws IOException {

    ResetBasicData.reset();

    List<Customer> list = Ebean.find(Customer.class).select("name").findList();

    JsonWriteOptions opt = JsonWriteOptions.parsePath("name, status");

    JsonContext jsonContext = Ebean.json();
    jsonContext.toJson(list, opt);
  }
Example 8
void json_parse_format() {

    String asJson = Ebean.json().toJson(bean);
    assertThat(asJson).contains("\"map\":{\"home\":\"123\",\"work\":\"987\"}");

    EBasicHstore fromJson = Ebean.json().toBean(EBasicHstore.class, asJson);
    assertEquals(bean.getId(), fromJson.getId());
    assertEquals(bean.getName(), fromJson.getName());
    Assertions.assertThat(fromJson.getMap().keySet()).containsExactly("home", "work");
  }
Example 9
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 = Ebean.json().toBean(Customer.class, someJsonAllKnown, options);
    assertEquals(Integer.valueOf(42), customer.getId());
    assertEquals("rob", customer.getName());

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

    InnerReport report = Ebean.json().toBean(InnerReport.class, "{}");
    Ebean.save(report);

    // other service ...
    InnerReport.Forecast f = new InnerReport.Forecast();
    report.setForecast(f);
    f.innerReport = report;

    Ebean.save(f);

  }