#280 - ENH: Add ServerConfig.setJsonInclude(...) ALL, NON_NULL, NON_EMPTY

This commit is contained in:
Robin Bygrave
2015-07-20 08:45:46 +12:00
parent 160e4d7dd1
commit f6466d3211
55 changed files with 616 additions and 109 deletions
@@ -1,5 +1,6 @@
package com.avaje.ebeaninternal.server.text.json;
import com.avaje.ebean.config.JsonConfig;
import com.avaje.ebean.text.PathProperties;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
@@ -19,7 +20,7 @@ public class WriteJsonTest {
JsonGenerator generator = jsonFactory.createGenerator(new StringWriter());
PathProperties pathProperties = PathProperties.parse("id,status,name,customer(id,name,address(street,city)),orders(qty,product(sku,prodName))");
WriteJson writeJson = new WriteJson(null, generator, pathProperties, null);
WriteJson writeJson = new WriteJson(null, generator, pathProperties, null, JsonConfig.Include.ALL);
WriteJson.WriteBean rootLevel = writeJson.createWriteBean(null, null);
assertTrue(rootLevel.currentIncludeProps.contains("id"));
@@ -1,5 +1,7 @@
package com.avaje.ebeaninternal.server.type;
import com.avaje.ebean.config.JsonConfig;
import com.avaje.ebeaninternal.server.text.json.WriteJson;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
@@ -29,7 +31,9 @@ public class JsonTester<T> {
JsonGenerator generator = factory.createGenerator(writer);
generator.writeStartObject();
type.jsonWrite(generator, "key", value);
WriteJson writeJson = new WriteJson(generator, JsonConfig.Include.ALL);
type.jsonWrite(writeJson, "key", value);
generator.writeEndObject();
generator.flush();
@@ -1,5 +1,7 @@
package com.avaje.ebeaninternal.server.type;
import com.avaje.ebean.config.JsonConfig;
import com.avaje.ebeaninternal.server.text.json.WriteJson;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
@@ -100,7 +102,8 @@ public class ScalarTypePostgresHstoreTest {
// wrap in an object to form proper json
generator.writeStartObject();
hstore.jsonWrite(generator, "key", map);
WriteJson writeJson = new WriteJson(generator, JsonConfig.Include.ALL);
hstore.jsonWrite(writeJson, "key", map);
generator.writeEndObject();
generator.flush();
@@ -4,11 +4,9 @@ import com.avaje.ebean.BaseTestCase;
import com.avaje.ebean.Ebean;
import com.avaje.ebean.text.json.EJson;
import com.avaje.tests.model.json.EBasicJsonMap;
import org.junit.Ignore;
import org.junit.Test;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import static org.junit.Assert.assertEquals;
@@ -0,0 +1,53 @@
package com.avaje.tests.json.include;
import com.avaje.ebean.Ebean;
import com.avaje.ebean.config.JsonConfig;
import com.avaje.ebean.text.json.JsonWriteOptions;
import com.avaje.tests.model.basic.Order;
import com.avaje.tests.model.basic.OrderDetail;
import org.junit.Test;
import java.util.ArrayList;
import static org.junit.Assert.assertEquals;
public class TestJsonExcludeEmptyList {
@Test
public void testToBeanToJson_NonNull() throws Exception {
Order bean = new Order();
bean.setId(99);
bean.setStatus(null);
bean.setOrderDate(null);
bean.setDetails(new ArrayList<OrderDetail>());
JsonWriteOptions options = new JsonWriteOptions();
options.setInclude(JsonConfig.Include.NON_NULL);
String asJson = Ebean.json().toJson(bean, options);
String expectedJson = "{\"id\":99,\"details\":[]}";
assertEquals(expectedJson, asJson);
}
@Test
public void testToBeanToJson_NonEmpty() throws Exception {
Order bean = new Order();
bean.setId(99);
bean.setStatus(null);
bean.setOrderDate(null);
bean.setDetails(new ArrayList<OrderDetail>());
JsonWriteOptions options = new JsonWriteOptions();
options.setInclude(JsonConfig.Include.NON_EMPTY);
String asJson = Ebean.json().toJson(bean, options);
String expectedJson = "{\"id\":99}";
assertEquals(expectedJson, asJson);
}
}
@@ -0,0 +1,48 @@
package com.avaje.tests.json.include;
import com.avaje.ebean.Ebean;
import com.avaje.ebean.config.JsonConfig;
import com.avaje.ebean.text.json.JsonWriteOptions;
import com.avaje.tests.model.json.EBasicJsonMap;
import org.junit.Test;
import java.util.LinkedHashMap;
import static org.junit.Assert.assertEquals;
public class TestJsonExcludeEmptyMap {
@Test
public void testToBeanToJson_NonNull() throws Exception {
EBasicJsonMap bean = new EBasicJsonMap();
bean.setId(99L);
bean.setContent(new LinkedHashMap<String, Object>());
JsonWriteOptions options = new JsonWriteOptions();
options.setInclude(JsonConfig.Include.NON_NULL);
String asJson = Ebean.json().toJson(bean, options);
String expectedJson = "{\"id\":99,\"content\":{}}";
assertEquals(expectedJson, asJson);
}
@Test
public void testToBeanToJson_NonEmpty() throws Exception {
EBasicJsonMap bean = new EBasicJsonMap();
bean.setId(99L);
bean.setContent(new LinkedHashMap<String, Object>());
JsonWriteOptions options = new JsonWriteOptions();
options.setInclude(JsonConfig.Include.NON_EMPTY);
String asJson = Ebean.json().toJson(bean, options);
String expectedJson = "{\"id\":99}";
assertEquals(expectedJson, asJson);
}
}
@@ -0,0 +1,49 @@
package com.avaje.tests.json.include;
import com.avaje.ebean.Ebean;
import com.avaje.ebean.config.JsonConfig;
import com.avaje.ebean.text.json.JsonWriteOptions;
import com.avaje.tests.model.basic.Attribute;
import com.avaje.tests.model.basic.AttributeHolder;
import org.junit.Test;
import java.util.LinkedHashSet;
import static org.junit.Assert.assertEquals;
public class TestJsonExcludeEmptySet {
@Test
public void testToBeanToJson_NonNull() throws Exception {
AttributeHolder bean = new AttributeHolder();
bean.setId(99);
bean.setAttributes(new LinkedHashSet<Attribute>());
JsonWriteOptions options = new JsonWriteOptions();
options.setInclude(JsonConfig.Include.NON_NULL);
String asJson = Ebean.json().toJson(bean, options);
String expectedJson = "{\"id\":99,\"attributes\":[]}";
assertEquals(expectedJson, asJson);
}
@Test
public void testToBeanToJson_NonEmpty() throws Exception {
AttributeHolder bean = new AttributeHolder();
bean.setId(99);
bean.setAttributes(new LinkedHashSet<Attribute>());
JsonWriteOptions options = new JsonWriteOptions();
options.setInclude(JsonConfig.Include.NON_EMPTY);
String asJson = Ebean.json().toJson(bean, options);
String expectedJson = "{\"id\":99}";
assertEquals(expectedJson, asJson);
}
}
@@ -0,0 +1,31 @@
package com.avaje.tests.json.include;
import com.avaje.ebean.Ebean;
import com.avaje.ebean.config.JsonConfig;
import com.avaje.ebean.text.json.JsonWriteOptions;
import com.avaje.tests.json.transientproperties.EJsonTransientObject;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class TestJsonExcludeNull {
@Test
public void testToBeanToJson() throws Exception {
EJsonTransientObject bean = new EJsonTransientObject();
bean.setId(99L);
bean.setName(null);
JsonWriteOptions options = new JsonWriteOptions();
options.setInclude(JsonConfig.Include.NON_NULL);
String asJson = Ebean.json().toJson(bean, options);
String expectedJson = "{\"id\":99}";
assertEquals(expectedJson, asJson);
}
}
@@ -0,0 +1,48 @@
package com.avaje.tests.json.include;
import com.avaje.ebean.Ebean;
import com.avaje.ebean.config.JsonConfig;
import com.avaje.ebean.text.json.JsonWriteOptions;
import com.avaje.tests.json.transientproperties.EJsonTransientList;
import org.junit.Test;
import java.util.ArrayList;
import static org.junit.Assert.assertEquals;
public class TestJsonExcludeTransientEmptyList {
@Test
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);
}
@Test
public void testToBeanToJson_NonEmpty() throws Exception {
EJsonTransientList bean = new EJsonTransientList();
bean.setId(99L);
bean.setFileNames(new ArrayList<String>());
JsonWriteOptions options = new JsonWriteOptions();
options.setInclude(JsonConfig.Include.NON_EMPTY);
String asJson = Ebean.json().toJson(bean, options);
String expectedJson = "{\"id\":99}";
assertEquals(expectedJson, asJson);
}
}