mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
JSON Refactor - compound type support
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
package com.avaje.ebean.json;
|
||||
|
||||
import java.io.StringReader;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.json.Json;
|
||||
import javax.json.stream.JsonParser;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -101,4 +105,48 @@ public class EJsonTests {
|
||||
String jsonOutput = EJson.write(result);
|
||||
Assert.assertEquals(jsonInput, jsonOutput);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_partial_read() {
|
||||
|
||||
String jsonInput = "{\"name\":\"rob\",\"age\":null,\"friend\":{\"name\":\"mike\",\"age\":13}},some more json would follow...";
|
||||
StringReader reader = new StringReader(jsonInput);
|
||||
JsonParser parser = Json.createParser(reader);
|
||||
|
||||
Object result = EJson.parsePartial(parser);
|
||||
|
||||
Assert.assertTrue(result instanceof Map);
|
||||
Map<?,?> map = (Map<?,?>)result;
|
||||
Assert.assertEquals("rob", map.get("name"));
|
||||
Assert.assertNull(map.get("age"));
|
||||
|
||||
Map<?,?> friend = (Map<?,?>)map.get("friend");
|
||||
Assert.assertEquals("mike", friend.get("name"));
|
||||
Assert.assertEquals(13L, friend.get("age"));
|
||||
|
||||
}
|
||||
|
||||
// @Test
|
||||
// public void test_partial_read_primitives() {
|
||||
//
|
||||
// Object result = null;
|
||||
// JsonParser parser = Json.createParser(new StringReader(","));
|
||||
//
|
||||
//// Object result = EJson.parsePartial(parser);
|
||||
//// Assert.assertNull(result);
|
||||
//
|
||||
// parser = Json.createParser(new StringReader("12L"));
|
||||
// result = EJson.parsePartial(parser);
|
||||
// Assert.assertEquals(12L, result);
|
||||
//
|
||||
// parser = Json.createParser(new StringReader("true"));
|
||||
// result = EJson.parsePartial(parser);
|
||||
// Assert.assertEquals(Boolean.TRUE, result);
|
||||
//
|
||||
// parser = Json.createParser(new StringReader("\"foo\""));
|
||||
// result = EJson.parsePartial(parser);
|
||||
// Assert.assertEquals("foo", result);
|
||||
//
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
@@ -2,12 +2,14 @@ package com.avaje.tests.ddd.iud;
|
||||
|
||||
import java.util.Currency;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.bean.EntityBean;
|
||||
import com.avaje.ebean.config.GlobalProperties;
|
||||
import com.avaje.ebean.text.json.JsonContext;
|
||||
import com.avaje.ebeaninternal.api.SpiEbeanServer;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import com.avaje.ebeaninternal.server.el.ElPropertyValue;
|
||||
@@ -21,47 +23,44 @@ public class TestDPersonEl extends TestCase {
|
||||
|
||||
GlobalProperties.put("classes", DPerson.class.toString());
|
||||
|
||||
// Currency NZD = Currency.getInstance("NZD");
|
||||
//
|
||||
// DPerson p = new DPerson();
|
||||
// p.setFirstName("first");
|
||||
// p.setLastName("last");
|
||||
// p.setSalary(new Money("12200"));
|
||||
// p.setCmoney(new CMoney(new Money("12"), NZD));
|
||||
//
|
||||
// SpiEbeanServer server = (SpiEbeanServer)Ebean.getServer(null);
|
||||
//
|
||||
// BeanDescriptor<DPerson> descriptor = server.getBeanDescriptor(DPerson.class);
|
||||
//
|
||||
// ElPropertyValue elCmoney = descriptor.getElGetValue("cmoney");
|
||||
Currency NZD = Currency.getInstance("NZD");
|
||||
|
||||
DPerson p = new DPerson();
|
||||
p.setFirstName("first");
|
||||
p.setLastName("last");
|
||||
p.setSalary(new Money("12200"));
|
||||
p.setCmoney(new CMoney(new Money("12"), NZD));
|
||||
|
||||
SpiEbeanServer server = (SpiEbeanServer)Ebean.getServer(null);
|
||||
|
||||
BeanDescriptor<DPerson> descriptor = server.getBeanDescriptor(DPerson.class);
|
||||
|
||||
ElPropertyValue elCmoney = descriptor.getElGetValue("cmoney");
|
||||
// ElPropertyValue elCmoneyAmt = descriptor.getElGetValue("cmoney.amount");
|
||||
// ElPropertyValue elCmoneyCur = descriptor.getElGetValue("cmoney.currency");
|
||||
//
|
||||
// EntityBean entityBean = (EntityBean)p;
|
||||
//
|
||||
// Object cmoney = elCmoney.elGetValue(entityBean);
|
||||
|
||||
JsonContext jsonContext = server.createJsonContext();
|
||||
String json = jsonContext.toJsonString(p);
|
||||
|
||||
DPerson bean = jsonContext.toBean(DPerson.class, json);
|
||||
Assert.assertEquals("first", bean.getFirstName());
|
||||
Assert.assertEquals(new Money("12200"), bean.getSalary());
|
||||
Assert.assertEquals(new Money("12"), bean.getCmoney().getAmount());
|
||||
Assert.assertEquals(NZD, bean.getCmoney().getCurrency());
|
||||
|
||||
|
||||
EntityBean entityBean = (EntityBean)p;
|
||||
|
||||
Object cmoney = elCmoney.elGetValue(entityBean);
|
||||
// Object amt = elCmoneyAmt.elGetValue(entityBean);
|
||||
// Object cur = elCmoneyCur.elGetValue(entityBean);
|
||||
//
|
||||
// Assert.assertNotNull(cmoney);
|
||||
|
||||
Assert.assertNotNull(cmoney);
|
||||
// Assert.assertEquals(new Money("12"), amt);
|
||||
// Assert.assertEquals(NZD, cur);
|
||||
//
|
||||
// p.setCmoney(null);
|
||||
// Assert.assertNull(p.getCmoney());
|
||||
//
|
||||
// // won't trigger CMoney build as not all properties
|
||||
// // have been set yet...
|
||||
// elCmoneyAmt.elSetValue(entityBean, new Money("13"), true, false);
|
||||
// Assert.assertNull(p.getCmoney());
|
||||
//
|
||||
// // will trigger the build and setting of CMoney
|
||||
// elCmoneyCur.elSetValue(entityBean, NZD, true, false);
|
||||
//
|
||||
// // this time not null as all required properties for
|
||||
// // the compound object have been collected
|
||||
// Assert.assertNotNull(p.getCmoney());
|
||||
|
||||
|
||||
p.setCmoney(null);
|
||||
Assert.assertNull(p.getCmoney());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ public class TestJsonInheritanceDiscriminator extends BaseTestCase {
|
||||
|
||||
List<Animal> animals = Ebean.find(Animal.class).findList();
|
||||
|
||||
String listJson = json.toJsonString(animals, false);
|
||||
String listJson = json.toJsonString(animals);
|
||||
|
||||
List<Animal> animals2 = json.toList(Animal.class, listJson);
|
||||
Assert.assertEquals(animals.size(), animals2.size());
|
||||
|
||||
@@ -24,7 +24,7 @@ public class TestJsonMap extends BaseTestCase {
|
||||
JsonContext jsonContext = Ebean.createJsonContext();
|
||||
JsonWriteOptions jsonWriteOptions = JsonWriteOptions.parsePath("(id,status,name)");
|
||||
|
||||
String jsonString = jsonContext.toJsonString(map, true, jsonWriteOptions);
|
||||
String jsonString = jsonContext.toJsonString(map, jsonWriteOptions);
|
||||
System.out.println(jsonString);
|
||||
|
||||
jsonContext = Ebean.createJsonContext();
|
||||
@@ -33,7 +33,7 @@ public class TestJsonMap extends BaseTestCase {
|
||||
// jsonWriteOptions =
|
||||
// JsonWriteOptions.parsePath("(id,status,billingAddress(*))");
|
||||
|
||||
jsonString = jsonContext.toJsonString(map, true, jsonWriteOptions);
|
||||
jsonString = jsonContext.toJsonString(map, jsonWriteOptions);
|
||||
System.out.println(jsonString);
|
||||
|
||||
// Assert.assertTrue(jsonString.indexOf("{\"1\":") > -1);
|
||||
|
||||
@@ -14,19 +14,15 @@ import org.junit.Test;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.json.EJson;
|
||||
import com.avaje.ebean.text.json.JsonContext;
|
||||
import com.avaje.ebean.text.json.JsonElement;
|
||||
import com.avaje.ebean.text.json.JsonElementObject;
|
||||
import com.avaje.ebeaninternal.server.text.json.InternalJsonParser;
|
||||
|
||||
public class TestJsonSimple extends BaseTestCase {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void test() throws IOException {
|
||||
|
||||
boolean b = JsonElement.class.isAssignableFrom(JsonElementObject.class);
|
||||
Assert.assertTrue(b);
|
||||
|
||||
InputStream is = this.getClass().getResourceAsStream("/example1.json");
|
||||
|
||||
final Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
|
||||
@@ -41,35 +37,30 @@ public class TestJsonSimple extends BaseTestCase {
|
||||
|
||||
String jsonText = sb.toString();
|
||||
|
||||
JsonElement el = InternalJsonParser.parse(jsonText);
|
||||
|
||||
Object el = EJson.parse(jsonText);
|
||||
System.out.println("Got " + el);
|
||||
|
||||
JsonElement e2 = InternalJsonParser
|
||||
.parse("{\"a\":12, \"name\":{\"first\":\"rob\", \"last\":\"byg\"}}");
|
||||
Map<String,Object> e2 = EJson.parseObject("{\"a\":12, \"name\":{\"first\":\"rob\", \"last\":\"byg\"}}");
|
||||
|
||||
Assert.assertEquals(Double.valueOf(12), e2.eval("a"));
|
||||
Assert.assertEquals(12, e2.evalInt("a"));
|
||||
Assert.assertEquals("rob", e2.evalString("name.first"));
|
||||
|
||||
Assert.assertEquals("byg", e2.evalString("name.last"));
|
||||
Assert.assertEquals(12L, e2.get("a"));
|
||||
Assert.assertEquals("rob", ((Map<String,Object>)e2.get("name")).get("first"));
|
||||
|
||||
Map<String, String> m = new LinkedHashMap<String, String>();
|
||||
m.put("hello", "rob");
|
||||
m.put("test", "me");
|
||||
|
||||
JsonContext jsonContext = Ebean.createJsonContext();
|
||||
String jsonString = jsonContext.toJsonString(m, true);
|
||||
String jsonString = jsonContext.toJsonString(m);
|
||||
System.out.println(jsonString);
|
||||
|
||||
String s = "{\"parishId\":\"18\",\"contentId\":null,\"contentStatus\":null,\"contentType\":\"pg-hello\",\"content\":\"<p>\n\tSomeThing</p>\n\"}";
|
||||
String s = "{\"parishId\":\"18\",\"contentId\":null,\"contentStatus\":null,\"contentType\":\"pg-hello\",\"content\":\"asd\"}";
|
||||
|
||||
JsonElement jsonElement = InternalJsonParser.parse(s);
|
||||
Object jsonElement = EJson.parse(s);
|
||||
Assert.assertNotNull(jsonElement);
|
||||
|
||||
JsonElement e3 = InternalJsonParser.parse("{\"name\":\"\\u60a8\\u597d\"}");
|
||||
Map<String,Object> e3 = EJson.parseObject("{\"name\":\"\\u60a8\\u597d\"}");
|
||||
|
||||
Assert.assertTrue(e3.evalString("name").length()==2);
|
||||
Assert.assertTrue(((String)e3.get("name")).length()==2);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@ package com.avaje.tests.text.json;
|
||||
|
||||
import java.io.StringReader;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
@@ -10,11 +9,6 @@ import org.junit.Test;
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.text.json.JsonContext;
|
||||
import com.avaje.ebean.text.json.JsonElement;
|
||||
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;
|
||||
|
||||
@@ -34,16 +28,11 @@ public class TestTextJsonBeanReadVisitor extends BaseTestCase {
|
||||
|
||||
JsonContext json = Ebean.createJsonContext();
|
||||
|
||||
JsonReadOptions options = new JsonReadOptions();
|
||||
options.addRootVisitor(new CVisitor());
|
||||
options.addVisitor("contacts", new ContactVisitor());
|
||||
options.addVisitor("billingAddress", new AVisitor());
|
||||
options.addVisitor("shippingAddress", new ASVisitor());
|
||||
|
||||
String s = json.toJsonString(list, true);
|
||||
String s = json.toJsonString(list);
|
||||
System.out.println(s);
|
||||
|
||||
List<Customer> mList = json.toList(Customer.class, s, options);
|
||||
List<Customer> mList = json.toList(Customer.class, s);
|
||||
System.out.println("VIA STRING: " + mList);
|
||||
|
||||
StringReader reader = new StringReader(s);
|
||||
@@ -53,32 +42,5 @@ public class TestTextJsonBeanReadVisitor extends BaseTestCase {
|
||||
Assert.assertEquals(mList.size(), mList2.size());
|
||||
}
|
||||
|
||||
private static class CVisitor implements JsonReadBeanVisitor<Customer> {
|
||||
|
||||
public void visit(Customer bean, Map<String, JsonElement> unmapped) {
|
||||
System.out.println("visit customer: " + bean);
|
||||
}
|
||||
}
|
||||
|
||||
private static class AVisitor implements JsonReadBeanVisitor<Address> {
|
||||
|
||||
public void visit(Address bean, Map<String, JsonElement> unmapped) {
|
||||
System.out.println("visit billing address: " + bean);
|
||||
}
|
||||
}
|
||||
|
||||
private static class ASVisitor implements JsonReadBeanVisitor<Address> {
|
||||
|
||||
public void visit(Address bean, Map<String, JsonElement> unmapped) {
|
||||
System.out.println("visit shipping address: " + bean);
|
||||
}
|
||||
}
|
||||
|
||||
private static class ContactVisitor implements JsonReadBeanVisitor<Contact> {
|
||||
|
||||
public void visit(Contact bean, Map<String, JsonElement> unmapped) {
|
||||
System.out.println("visit contact: " + bean);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
-105
@@ -1,105 +0,0 @@
|
||||
package com.avaje.tests.text.json;
|
||||
|
||||
import java.io.StringReader;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.BeanState;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.text.json.JsonContext;
|
||||
import com.avaje.ebean.text.json.JsonElement;
|
||||
import com.avaje.ebean.text.json.JsonElementNumber;
|
||||
import com.avaje.ebean.text.json.JsonReadBeanVisitor;
|
||||
import com.avaje.ebean.text.json.JsonReadOptions;
|
||||
import com.avaje.ebean.text.json.JsonWriteBeanVisitor;
|
||||
import com.avaje.ebean.text.json.JsonWriteOptions;
|
||||
import com.avaje.ebean.text.json.JsonWriter;
|
||||
import com.avaje.tests.model.basic.Contact;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestTextJsonBeanReadVisitorWithCustomJson extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<Customer> list = Ebean.find(Customer.class).select("id, name, status, shippingAddress")
|
||||
.fetch("billingAddress", "line1, city").fetch("billingAddress.country", "*")
|
||||
.fetch("contacts", "firstName,email").order().desc("id").findList();
|
||||
|
||||
JsonContext json = Ebean.createJsonContext();
|
||||
|
||||
JsonWriteOptions writeOptions = new JsonWriteOptions();
|
||||
writeOptions.setRootPathVisitor(new JsonWriteBeanVisitor<Customer>() {
|
||||
|
||||
public void visit(Customer bean, JsonWriter ctx) {
|
||||
System.out.println("write visit customer: " + bean);
|
||||
ctx.appendRawValue("dummyCust", "34");
|
||||
ctx.appendRawValue("smallCustObject", "{\"a\":34,\"b\":\"asdasdasd\"}");
|
||||
}
|
||||
});
|
||||
|
||||
writeOptions.setPathProperties("contacts", "firstName,id");
|
||||
writeOptions.setPathVisitor("contacts", new JsonWriteBeanVisitor<Contact>() {
|
||||
|
||||
public void visit(Contact bean, JsonWriter ctx) {
|
||||
System.out.println("write additional custom json on customer: " + bean);
|
||||
ctx.appendRawValue("dummy", " 3400" + bean.getId() + "");
|
||||
ctx.appendRawValue("smallObject", "{\"contactA\":34,\"contactB\":\"banana\"}");
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
String s = json.toJsonString(list, true, writeOptions);
|
||||
System.out.println(s);
|
||||
|
||||
JsonReadOptions readOptions = new JsonReadOptions();
|
||||
readOptions.addRootVisitor(new CVisitor());
|
||||
readOptions.addVisitor("contacts", new ContactVisitor());
|
||||
|
||||
StringReader reader = new StringReader(s);
|
||||
List<Customer> mList2 = json.toList(Customer.class, reader, readOptions);
|
||||
System.out.println("VIA READER: " + mList2);
|
||||
|
||||
for (Customer customer : mList2) {
|
||||
BeanState beanState = Ebean.getBeanState(customer);
|
||||
Assert.assertNotNull(beanState.getLoadedProps());
|
||||
Assert.assertTrue(beanState.getLoadedProps().contains("status"));
|
||||
Assert.assertTrue(beanState.getLoadedProps().contains("smallnote"));
|
||||
Assert.assertFalse(beanState.getLoadedProps().contains("anniversary"));
|
||||
|
||||
String note = customer.getSmallnote();
|
||||
Assert.assertEquals("Set in Json Visitor", note);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class CVisitor implements JsonReadBeanVisitor<Customer> {
|
||||
|
||||
public void visit(Customer bean, Map<String, JsonElement> unmapped) {
|
||||
System.out.println("visit customer: " + bean);
|
||||
bean.setSmallnote("Set in Json Visitor");
|
||||
}
|
||||
}
|
||||
|
||||
private static class ContactVisitor implements JsonReadBeanVisitor<Contact> {
|
||||
|
||||
public void visit(Contact bean, Map<String, JsonElement> unmapped) {
|
||||
System.out.println("visit contact: " + bean);
|
||||
Assert.assertNotNull(unmapped);
|
||||
JsonElement dummyEl = unmapped.get("dummy");
|
||||
JsonElement smallObjEl = unmapped.get("smallObject");
|
||||
JsonElementNumber dummyNum = (JsonElementNumber) dummyEl;
|
||||
Assert.assertTrue(dummyNum.getValue().startsWith("3400"));
|
||||
Assert.assertNotNull(smallObjEl);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -26,7 +26,7 @@ public class TestTextJsonInheritance extends BaseTestCase {
|
||||
Assert.assertEquals(2, list.size());
|
||||
|
||||
JsonContext jsonContext = Ebean.createJsonContext();
|
||||
String jsonString = jsonContext.toJsonString(list, true);
|
||||
String jsonString = jsonContext.toJsonString(list);
|
||||
System.out.println(jsonString);
|
||||
|
||||
List<Vehicle> rebuiltList = jsonContext.toList(Vehicle.class, jsonString);
|
||||
|
||||
@@ -24,7 +24,7 @@ public class TestTextJsonInvokeLazy extends BaseTestCase {
|
||||
opt.setRootPathProperties("name, status");
|
||||
|
||||
JsonContext jsonContext = Ebean.createJsonContext();
|
||||
String jsonString = jsonContext.toJsonString(list, true, opt);
|
||||
String jsonString = jsonContext.toJsonString(list, opt);
|
||||
|
||||
System.out.println(jsonString);
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ public class TestTextJsonReadManyLazyLoad extends BaseTestCase {
|
||||
o.setPathProperties(pp);
|
||||
|
||||
System.out.println("Expect lazy loading of Customer beans and customer contacts");
|
||||
String s = json.toJsonString(list, true, o);
|
||||
String s = json.toJsonString(list, o);
|
||||
System.out.println(s);
|
||||
Assert.assertTrue(s.contains("\"contacts\""));
|
||||
Assert.assertTrue(s.contains("\"name\""));
|
||||
@@ -54,7 +54,7 @@ public class TestTextJsonReadManyLazyLoad extends BaseTestCase {
|
||||
o.setPathProperties(pp);
|
||||
|
||||
System.out.println("expecting lazy load of Customer beans to fetch customer name");
|
||||
String s = json.toJsonString(list, true, o);
|
||||
String s = json.toJsonString(list, o);
|
||||
System.out.println(s);
|
||||
Assert.assertTrue(s.contains("\"contacts\""));
|
||||
Assert.assertTrue(s.contains("\"name\""));
|
||||
@@ -78,7 +78,7 @@ public class TestTextJsonReadManyLazyLoad extends BaseTestCase {
|
||||
o.setPathProperties(pp);
|
||||
|
||||
System.out.println("expecting lazy load of Customer contacts ");
|
||||
String s = json.toJsonString(list, true, o);
|
||||
String s = json.toJsonString(list, o);
|
||||
System.out.println(s);
|
||||
Assert.assertTrue(s.contains("\"contacts\""));
|
||||
Assert.assertTrue(s.contains("\"name\""));
|
||||
|
||||
@@ -70,7 +70,7 @@ public class TestTextJsonReferenceBean extends BaseTestCase {
|
||||
JsonWriteOptions options = new JsonWriteOptions();
|
||||
options.setPathProperties("details.product", "id");
|
||||
|
||||
String jsonOrder = jsonContext.toJsonString(order, true, options);
|
||||
String jsonOrder = jsonContext.toJsonString(order, options);
|
||||
System.out.println(jsonOrder);
|
||||
|
||||
Order o2 = jsonContext.toBean(Order.class, jsonOrder);
|
||||
|
||||
@@ -8,9 +8,7 @@ import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.EbeanServer;
|
||||
import com.avaje.ebean.text.json.JsonContext;
|
||||
import com.avaje.ebean.text.json.JsonWriteBeanVisitor;
|
||||
import com.avaje.ebean.text.json.JsonWriteOptions;
|
||||
import com.avaje.ebean.text.json.JsonWriter;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
@@ -23,10 +21,7 @@ public class TestTextJsonSimple extends BaseTestCase {
|
||||
|
||||
List<Customer> list = Ebean.find(Customer.class).select("id, name, status, shippingAddress")
|
||||
.fetch("billingAddress", "line1, city").fetch("billingAddress.country", "*")
|
||||
.fetch("contacts", "firstName,email")// , new
|
||||
// FetchConfig().queryFirst(2))
|
||||
// .filterMany("contacts").ilike("firstName", "J%").query()
|
||||
// .where().lt("id", 3)
|
||||
.fetch("contacts", "firstName,email")
|
||||
.order().desc("id").findList();
|
||||
|
||||
EbeanServer server = Ebean.getServer(null);
|
||||
@@ -35,18 +30,8 @@ public class TestTextJsonSimple extends BaseTestCase {
|
||||
|
||||
JsonWriteOptions options = new JsonWriteOptions();
|
||||
options.setRootPathProperties("name, id");
|
||||
options.setRootPathVisitor(new JsonWriteBeanVisitor<Customer>() {
|
||||
|
||||
public void visit(Customer bean, JsonWriter ctx) {
|
||||
System.out.println("visiting " + bean);
|
||||
ctx.appendRawValue("dummy", "34");
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
String s = json.toJsonString(list, true);
|
||||
// String s = json.toJsonString(list, true, options);
|
||||
|
||||
|
||||
String s = json.toJsonString(list);
|
||||
System.out.println(s);
|
||||
|
||||
List<Customer> mList = json.toList(Customer.class, s);
|
||||
|
||||
@@ -29,7 +29,7 @@ public class TestTextJsonSuperSimple extends BaseTestCase {
|
||||
if (list.size() > 1) {
|
||||
Customer customer = list.get(0);
|
||||
|
||||
String s = json.toJsonString(customer, true);
|
||||
String s = json.toJsonString(customer);
|
||||
System.out.println(s);
|
||||
int statusPos = s.indexOf("status");
|
||||
Assert.assertEquals(-1, statusPos);
|
||||
|
||||
@@ -57,8 +57,7 @@ public class TestTextJsonUpdateCascade extends BaseTestCase {
|
||||
EbeanServer server = Ebean.getServer(null);
|
||||
|
||||
JsonContext jsonContext = server.createJsonContext();
|
||||
String jsonString = jsonContext.toJsonString(order, true);
|
||||
|
||||
String jsonString = jsonContext.toJsonString(order);
|
||||
System.out.println(jsonString);
|
||||
|
||||
Order updOrder = jsonContext.toBean(Order.class, jsonString);
|
||||
@@ -85,7 +84,7 @@ public class TestTextJsonUpdateCascade extends BaseTestCase {
|
||||
|
||||
Ebean.save(u0);
|
||||
|
||||
String jsonUser = jsonContext.toJsonString(u0, true);
|
||||
String jsonUser = jsonContext.toJsonString(u0);
|
||||
|
||||
System.out.println(jsonUser);
|
||||
|
||||
|
||||
@@ -1,67 +0,0 @@
|
||||
package com.avaje.tests.text.json;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.sql.Timestamp;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.config.GlobalProperties;
|
||||
import com.avaje.ebean.text.json.JsonContext;
|
||||
import com.avaje.ebean.text.json.JsonValueAdapter;
|
||||
import com.avaje.ebean.text.json.JsonWriteOptions;
|
||||
import com.avaje.ebeaninternal.server.text.json.DefaultJsonValueAdapter;
|
||||
import com.avaje.tests.model.basic.Car;
|
||||
import com.avaje.tests.model.basic.Vehicle;
|
||||
|
||||
public class TestTextJsonUtilDateFormat extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
GlobalProperties.put("ebean.ddl.generate", "false");
|
||||
GlobalProperties.put("ebean.ddl.run", "false");
|
||||
|
||||
Vehicle v = new Car();
|
||||
v.setId(100);
|
||||
v.setRegistrationDate(new java.util.Date());
|
||||
v.setUpdtime(new Timestamp(System.currentTimeMillis()));
|
||||
|
||||
JsonContext context = Ebean.createJsonContext();
|
||||
JsonWriteOptions o = new JsonWriteOptions();
|
||||
o.setValueAdapter(new CustomDateFormatAdapter());
|
||||
|
||||
String jsonString = context.toJsonString(v, true, o);
|
||||
System.out.println(jsonString);
|
||||
|
||||
Assert.assertTrue(jsonString.contains("\"registrationDate\":'"));
|
||||
}
|
||||
|
||||
class CustomDateFormatAdapter implements JsonValueAdapter {
|
||||
|
||||
DefaultJsonValueAdapter defaultImplementation = new DefaultJsonValueAdapter();
|
||||
|
||||
public String jsonFromDate(Date date) {
|
||||
// TODO
|
||||
return null;
|
||||
}
|
||||
|
||||
public String jsonFromTimestamp(Timestamp date) {
|
||||
// add some single quotes around the timestamp value
|
||||
return "'" + defaultImplementation.jsonFromTimestamp(date) + "'";
|
||||
}
|
||||
|
||||
public Date jsonToDate(String jsonDate) {
|
||||
// TODO
|
||||
return null;
|
||||
}
|
||||
|
||||
public Timestamp jsonToTimestamp(String jsonDateTime) {
|
||||
// TODO
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -31,7 +31,7 @@ public class TestJsonStatelessUpdate extends BaseTestCase {
|
||||
JsonContext jsonContext = Ebean.createJsonContext();
|
||||
|
||||
JsonWriteOptions writeOptions = JsonWriteOptions.parsePath("(id,name,master(*))");
|
||||
String jsonString = jsonContext.toJsonString(twoX, true, writeOptions);
|
||||
String jsonString = jsonContext.toJsonString(twoX, writeOptions);
|
||||
|
||||
System.out.println(jsonString);
|
||||
jsonString = jsonString.replace("twoName", "twoNameModified");
|
||||
|
||||
Reference in New Issue
Block a user