@Test public void test_jsonWithPersistenceContext() { ResetBasicData.reset(); List<Order> orders = Ebean.find(Order.class) .fetch("customer", "id, name") .where() .eq("customer.id", 1) .findList(); String json = Ebean.json().toJson(orders); List<Order> orders1 = Ebean.json().toList(Order.class, json); Customer customer = null; for (Order order : orders1) { Customer tempCustomer = order.getCustomer(); if (customer == null) { customer = tempCustomer; } else { assertThat(tempCustomer).isSameAs(customer); } } }
@Test 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()); }
@Test public void test_json_loadContext() { ResetBasicData.reset(); List<Order> orders = Ebean.find(Order.class).select("status").fetch("customer", "id, name").findList(); String json = Ebean.json().toJson(orders); JsonReadOptions options = new JsonReadOptions().setEnableLazyLoading(true); List<Order> orders1 = Ebean.json().toList(Order.class, json, options); for (Order order : orders1) { Customer customer = order.getCustomer(); customer.getName(); customer.getSmallnote(); List<Contact> contacts = customer.getContacts(); contacts.size(); } }
public void runExampleUsingOrders() { LoadExampleData.load(); PathProperties pathProperties = PathProperties.parse("(id,status,orderDate,customer(id,name),details(*,product(sku)))"); Query<Order> query = Ebean.createQuery(Order.class); pathProperties.apply(query); List<Order> orders = query.where().gt("id", 1).findList(); String rawJson = Ebean.json().toJson(orders); System.out.println(rawJson); }
@Test 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); }
@SuppressWarnings("unchecked") @Test public void test_unknownProperty_withVisitor() { String jsonWithUnknown = "{\"id\":42,\"unknownProp\":\"foo\",\"name\":\"rob\",\"version\":1,\"extraProp\":{\"name\":\"foobie\",\"sim\":\"bo\"}}"; CustReadVisitor custReadVisitor = new CustReadVisitor(); JsonReadOptions options = new JsonReadOptions(); options.addRootVisitor(custReadVisitor); Customer customer = Ebean.json().toBean(Customer.class, jsonWithUnknown, options); assertEquals(Integer.valueOf(42), customer.getId()); assertEquals("rob", customer.getName()); assertSame(customer, custReadVisitor.bean); assertEquals("foo", custReadVisitor.unmapped.get("unknownProp")); assertEquals(2, custReadVisitor.unmapped.size()); assertEquals( "foobie", ((Map<String, Object>) custReadVisitor.unmapped.get("extraProp")).get("name")); assertEquals( "bo", ((Map<String, Object>) custReadVisitor.unmapped.get("extraProp")).get("sim")); }