mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#1882 - ENH: Add JsonContext.toJsonPretty() ... for pretty JSON output of beans and lists of beans
This commit is contained in:
@@ -149,6 +149,13 @@ public interface JsonContext {
|
||||
*/
|
||||
String toJson(Object value) throws JsonIOException;
|
||||
|
||||
/**
|
||||
* Return the bean or collection as JSON string in pretty format.
|
||||
*
|
||||
* @throws JsonIOException When IOException occurs
|
||||
*/
|
||||
String toJsonPretty(Object value) throws JsonIOException;
|
||||
|
||||
/**
|
||||
* Write the bean or collection in JSON format to the writer.
|
||||
*
|
||||
|
||||
@@ -5,6 +5,8 @@ import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonToken;
|
||||
import com.fasterxml.jackson.core.PrettyPrinter;
|
||||
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
|
||||
import io.ebean.FetchPath;
|
||||
import io.ebean.bean.EntityBean;
|
||||
import io.ebean.config.JsonConfig;
|
||||
@@ -43,6 +45,8 @@ import java.util.Set;
|
||||
*/
|
||||
public class DJsonContext implements SpiJsonContext {
|
||||
|
||||
private static final PrettyPrinter PRETTY_PRINTER = new Pretty();
|
||||
|
||||
private final SpiEbeanServer server;
|
||||
|
||||
private final JsonFactory jsonFactory;
|
||||
@@ -53,6 +57,12 @@ public class DJsonContext implements SpiJsonContext {
|
||||
|
||||
private final DJsonScalar jsonScalar;
|
||||
|
||||
private static class Pretty extends DefaultPrettyPrinter {
|
||||
Pretty() {
|
||||
_objectFieldValueSeparatorWithSpaces = ": ";
|
||||
}
|
||||
}
|
||||
|
||||
public DJsonContext(SpiEbeanServer server, JsonFactory jsonFactory, TypeManager typeManager) {
|
||||
this.server = server;
|
||||
this.jsonFactory = (jsonFactory != null) ? jsonFactory : new JsonFactory();
|
||||
@@ -289,19 +299,27 @@ public class DJsonContext implements SpiJsonContext {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toJsonPretty(Object value) throws JsonIOException {
|
||||
return toJsonString(value, null, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toJson(Object o) throws JsonIOException {
|
||||
return toJsonString(o, null);
|
||||
return toJsonString(o, null, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toJson(Object o, JsonWriteOptions options) throws JsonIOException {
|
||||
return toJsonString(o, options);
|
||||
return toJsonString(o, options, false);
|
||||
}
|
||||
|
||||
private String toJsonString(Object value, JsonWriteOptions options) throws JsonIOException {
|
||||
private String toJsonString(Object value, JsonWriteOptions options, boolean pretty) throws JsonIOException {
|
||||
StringWriter writer = new StringWriter(500);
|
||||
try (JsonGenerator gen = createGenerator(writer)) {
|
||||
if (pretty) {
|
||||
gen.setPrettyPrinter(PRETTY_PRINTER);
|
||||
}
|
||||
toJsonInternal(value, gen, options);
|
||||
} catch (IOException e) {
|
||||
throw new JsonIOException(e);
|
||||
|
||||
@@ -17,7 +17,7 @@ public class JsonBeanReaderTest extends BaseTestCase {
|
||||
static JsonContext json = Ebean.json();
|
||||
|
||||
@Test
|
||||
public void read() throws Exception {
|
||||
public void read() {
|
||||
|
||||
JsonParser parser = getParser();
|
||||
JsonBeanReader<Customer> beanReader = json.createBeanReader(Customer.class, parser, null);
|
||||
@@ -40,7 +40,7 @@ public class JsonBeanReaderTest extends BaseTestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void forJson() throws Exception {
|
||||
public void forJson() {
|
||||
|
||||
JsonParser parser = getParser();
|
||||
JsonBeanReader<Customer> beanReader = json.createBeanReader(Customer.class, parser, null);
|
||||
|
||||
@@ -81,6 +81,20 @@ public class JsonContextTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_toJsonPretty() {
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<Order> orders = DB.find(Order.class)
|
||||
.select("status")
|
||||
.fetch("customer", "id, name")
|
||||
.findList();
|
||||
|
||||
String json = DB.json().toJsonPretty(orders);
|
||||
assertThat(json).contains("[ {");
|
||||
assertThat(json).contains("\"customer\": {");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_toObject() {
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package io.ebeaninternal.server.text.json;
|
||||
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.FetchPath;
|
||||
import io.ebean.Query;
|
||||
import io.ebean.text.PathProperties;
|
||||
@@ -8,7 +8,6 @@ import org.junit.Test;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -16,22 +15,21 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
public class WriteJsonTest {
|
||||
|
||||
@Test
|
||||
public void test_push() throws IOException {
|
||||
public void test_push() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
FetchPath fetchPath = PathProperties.parse("id,status,name,customer(id,name,billingAddress(street,city)),details(qty,product(sku,prodName))");
|
||||
|
||||
Query<Order> query = Ebean.find(Order.class);
|
||||
Query<Order> query = DB.find(Order.class);
|
||||
|
||||
fetchPath.apply(query);
|
||||
List<Order> list = query.findList();
|
||||
|
||||
String json = Ebean.json().toJson(list);
|
||||
System.out.println(json);
|
||||
String json = DB.json().toJsonPretty(list);
|
||||
|
||||
assertThat(json).contains("\"customer\":{");
|
||||
assertThat(json).contains("\"billingAddress\":{");
|
||||
assertThat(json).contains("\"details\":[{");
|
||||
assertThat(json).contains("\"customer\": {");
|
||||
assertThat(json).contains("\"billingAddress\": {");
|
||||
assertThat(json).contains("\"details\": [ {");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user