APISonar


io.ebean.Ebean.json

> io > ebean > Ebean > json
io 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 fromJson() {

    String json = "{\"name\":\"someName\",\"one\":42,\"nested\":{\"alpha\":\"aa\",\"beta\":\"bb\"}}";

    EBasicJsonUnmapped bean = Ebean.json().toBean(EBasicJsonUnmapped.class, json);

    assertThat(bean.getName()).isEqualTo("someName");
    assertThat(bean.getUnmapped().get("one")).isEqualTo(42L);

    @SuppressWarnings("unchecked")
    Map<String,Object> fromNested = (Map<String,Object>)bean.getUnmapped().get("nested");
    assertThat(fromNested).containsKeys("alpha", "beta");
    assertThat(fromNested).containsValues("aa", "bb");
  }
Example 3
public void testWithTransientListProperty() throws Exception {

    String rawJson = "{\"basic\":true,\"fileNames\":[\"1\"]}";

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

    assertEquals(true, bean.getBasic());
    assertEquals(1, bean.getFileNames().size());

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

    assertEquals(rawJson, asJson);
  }
Example 4
private UUOne rebuildViaJson(UUOne input) {
    String asJson = Ebean.json().toJson(input);
    return Ebean.json().toBean(UUOne.class, asJson);
  }
Example 5
private void jsonToFrom(EcPerson foundFirst) {

    String asJson = Ebean.json().toJson(foundFirst);
    EcPerson fromJson = Ebean.json().toBean(EcPerson.class, asJson);
    assertThat(fromJson.getPhoneNumbers()).containsAll(foundFirst.getPhoneNumbers());
  }
Example 6
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 7
public void testToBeanToJson_NonEmpty() throws Exception {

    EBasicJsonMap bean = new EBasicJsonMap();
    bean.setId(99L);
    bean.setContent(new LinkedHashMap<>());

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

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

    String expectedJson = "{\"id\":99,\"content\":{}}";

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

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

    EArraySetBean fromJson = Ebean.json().toBean(EArraySetBean.class, asJson);
    assertEquals(found.getId(), fromJson.getId());
    assertEquals(found.getId(), fromJson.getId());
    assertEquals(found.getName(), fromJson.getName());
    assertThat(fromJson.getPhoneNumbers()).containsExactly("4321", "9823");
  }
Example 9
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 10
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 11
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);

  }
Example 12
public void parseJson_when_inheritanceType_outOfOrderDtype() {

    String fom = "{\"id\":90,\"name\":\"Frank\",\"vehicle\":{\"id\":42,\"licenseNumber\":\"T100\",\"capacity\":99.0,\"dtype\":\"T\"}}";

    VehicleDriver driver1 = Ebean.json().toBean(VehicleDriver.class, fom);
    assertThat(driver1.getVehicle()).isInstanceOf(Truck.class);
    assertThat(driver1.getVehicle().getLicenseNumber()).isEqualTo("T100");
  }