#307 - Add back support for JsonValueAdapter and JsonReadOptions into 4.x

This commit is contained in:
rbygrave
2015-06-21 01:46:23 +12:00
parent f0b8270e5e
commit 721a50219d
15 changed files with 407 additions and 47 deletions
@@ -9,6 +9,7 @@ import org.junit.Test;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Map;
import static org.junit.Assert.*;
@@ -60,6 +61,57 @@ public class JsonContextTest {
assertEquals("rob", customer.getName());
}
class CustReadVisitor implements JsonReadBeanVisitor<Customer> {
Customer bean;
Map<String, Object> unmapped;
@Override
public void visit(Customer bean, Map<String, Object> unmapped) {
this.bean = bean;
this.unmapped = unmapped;
}
}
@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"));
}
@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);
}
@Test
public void testCreateGenerator() throws Exception {
@@ -4,6 +4,7 @@ import java.io.IOException;
import java.io.StringReader;
import java.util.Set;
import com.avaje.ebeaninternal.server.text.json.ReadJson;
import org.junit.Assert;
import org.junit.Test;
@@ -27,8 +28,10 @@ public class TestJsonBeanDescriptorParse extends BaseTestCase {
StringReader reader = new StringReader("{\"id\":123,\"name\":\"Hello rob\"}");
JsonParser parser = server.json().createParser(reader);
ReadJson readJson = new ReadJson(parser, null);
Customer customer = (Customer)descriptor.jsonRead(parser, null);
Customer customer = descriptor.jsonRead(readJson, null);
Assert.assertEquals(Integer.valueOf(123), customer.getId());
Assert.assertEquals("Hello rob", customer.getName());
@@ -5,6 +5,10 @@ import com.avaje.ebean.Ebean;
import com.avaje.ebean.EbeanServer;
import com.avaje.ebean.text.PathProperties;
import com.avaje.ebean.text.json.JsonContext;
import com.avaje.ebean.text.json.JsonReadBeanVisitor;
import com.avaje.ebean.text.json.JsonReadOptions;
import com.avaje.tests.model.basic.Address;
import com.avaje.tests.model.basic.Contact;
import com.avaje.tests.model.basic.Customer;
import com.avaje.tests.model.basic.ResetBasicData;
import org.junit.Assert;
@@ -12,9 +16,45 @@ import org.junit.Test;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.assertEquals;
public class TestTextJsonSimple extends BaseTestCase {
class CustJsonRead implements JsonReadBeanVisitor<Customer> {
Customer bean; Map<String, Object> unmapped;
@Override
public void visit(Customer bean, Map<String, Object> unmapped) {
this.bean = bean;
this.unmapped = unmapped;
}
}
class ContactJsonRead implements JsonReadBeanVisitor<Contact> {
Contact bean; Map<String, Object> unmapped;
@Override
public void visit(Contact bean, Map<String, Object> unmapped) {
this.bean = bean;
this.unmapped = unmapped;
}
}
class AddressJsonRead implements JsonReadBeanVisitor<Address> {
Address bean; Map<String, Object> unmapped;
@Override
public void visit(Address bean, Map<String, Object> unmapped) {
this.bean = bean;
this.unmapped = unmapped;
}
}
@Test
public void test() throws IOException {
@@ -37,7 +77,21 @@ public class TestTextJsonSimple extends BaseTestCase {
Assert.assertTrue(jsonOutput.contains("\"selected\":"));
List<Customer> mList = json.toList(Customer.class, jsonOutput);
Assert.assertEquals(list.size(), mList.size());
assertEquals(list.size(), mList.size());
CustJsonRead custJsonRead = new CustJsonRead();
ContactJsonRead contactJsonRead = new ContactJsonRead();
AddressJsonRead addressJsonRead = new AddressJsonRead();
JsonReadOptions options = new JsonReadOptions();
options.addRootVisitor(custJsonRead);
options.addVisitor("contacts", contactJsonRead);
options.addVisitor("billingAddress", addressJsonRead);
List<Customer> customers = json.toList(Customer.class, jsonOutput, options);
assertEquals(list.size(), customers.size());
}
@Test
@@ -61,7 +115,7 @@ public class TestTextJsonSimple extends BaseTestCase {
Assert.assertTrue(jsonOutput.contains("\"selected\":"));
List<Customer> mList = json.toList(Customer.class, jsonOutput);
Assert.assertEquals(list.size(), mList.size());
assertEquals(list.size(), mList.size());
}
@Test