Skip to content

Commit 1c90e5b

Browse files
committed
Add toJsonPretty() convenience method
1 parent 3b7f0dc commit 1c90e5b

File tree

7 files changed

+101
-1
lines changed

7 files changed

+101
-1
lines changed

blackbox-test/src/test/java/org/example/customer/BasicPubFieldsTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ void toJson() {
2727
var view = type.view("(two, four)");
2828
String viewJson = view.toJson(b);
2929
assertThat(viewJson).isEqualTo("{\"two\":43,\"four\":\"fourValue\"}");
30+
31+
String viewJsonPretty = view.toJsonPretty(b).replace("\" : ", "\": ");
32+
assertThat(viewJsonPretty).isEqualTo("{\n \"two\": 43,\n \"four\": \"fourValue\"\n}");
3033
}
3134

3235
@Test

blackbox-test/src/test/java/org/example/customer/CustomerTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@ class CustomerTest {
1515

1616
Jsonb jsonb = Jsonb.newBuilder().build();
1717

18+
@Test
19+
void anyToJson() {
20+
var customer = new Customer().id(42L).name("rob").status(Customer.Status.ACTIVE);
21+
String asJson = jsonb.toJson(customer);
22+
assertThat(asJson).isEqualTo("{\"id\":42,\"name\":\"rob\",\"status\":\"ACTIVE\"}");
23+
}
24+
25+
@Test
26+
void anyToJsonPretty() {
27+
var customer = new Customer().id(42L).name("rob").status(Customer.Status.ACTIVE);
28+
String asJson = jsonb.toJsonPretty(customer).replace("\" : ", "\": ");
29+
assertThat(asJson).isEqualTo("{\n \"id\": 42,\n \"name\": \"rob\",\n \"status\": \"ACTIVE\"\n}");
30+
}
31+
1832
@Test
1933
void toJson_view() {
2034
JsonView<Customer> customerJsonView = jsonb.type(Customer.class).view("(id, name)");

jsonb/src/main/java/io/avaje/jsonb/JsonView.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ public interface JsonView<T> {
3434
*/
3535
String toJson(T value);
3636

37+
/**
38+
* Return as json string in pretty format.
39+
*/
40+
String toJsonPretty(T value);
41+
3742
/**
3843
* Return the value as json content in bytes form.
3944
*/

jsonb/src/main/java/io/avaje/jsonb/Jsonb.java

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,44 @@ static Builder newBuilder() {
8686
return DefaultBootstrap.newBuilder();
8787
}
8888

89+
/**
90+
* Return JSON content for the given object.
91+
* <p>
92+
* This is a convenience method for using {@code jsonb.type(Object.class).toJson(any) }
93+
*
94+
* @param any The object to return as JSON string
95+
* @return Return JSON content for the given object.
96+
*/
97+
String toJson(Object any);
98+
99+
/**
100+
* Return JSON content in pretty format for the given object.
101+
* <p>
102+
* This is a convenience method for using {@code jsonb.type(Object.class).toJsonPretty(any) }
103+
*
104+
* @param any The object to return as JSON string in pretty format
105+
* @return Return JSON content in pretty format for the given object.
106+
*/
107+
String toJsonPretty(Object any);
108+
89109
/**
90110
* Return the JsonType used to read and write json for the given class.
91111
*
112+
* <h3>Examples</h3>
113+
* <pre>{@code
114+
*
115+
* JsonType<Customer> customerType = jsonb.type(Customer.class)
116+
*
117+
* Customer customer = ...
118+
* customerType.toJson(customer);
119+
*
120+
* JsonType<List<Customer>> customerListType = customerType.list()
121+
*
122+
* List<Customer> customers = ...
123+
* customerListType.toJson(customers);
124+
*
125+
* }</pre>
126+
*
92127
* <h3>Using Object.class</h3>
93128
* <p>
94129
* We can use <code>type(Object.class)</code> when we don't know the specific type that is being
@@ -105,6 +140,19 @@ static Builder newBuilder() {
105140

106141
/**
107142
* Return the JsonType used to read and write json for the given type.
143+
* <p>
144+
* We can use {@link Types} to obtain common generic types for List, Set, Map, Array etc.
145+
*
146+
* <h3>Example</h3>
147+
* <pre>{@code
148+
*
149+
* JsonType<List<String>> listOfStringType = jsonb.type(Types.listOf(String.class))
150+
*
151+
* JsonType<List<Customer>> listOfCustomerType = jsonb.type(Types.listOf(Customer.class))
152+
*
153+
* JsonType<Map<String,Integer>> adapter = jsonb.type(Types.mapOf(Integer.class))
154+
*
155+
* }</pre>
108156
*
109157
* <h3>Using Object.class</h3>
110158
* <p>
@@ -121,7 +169,7 @@ static Builder newBuilder() {
121169
<T> JsonType<T> type(Type type);
122170

123171
/**
124-
* Return the JsonType for the given value using the values class.
172+
* Return the JsonType for the given value using the class of the value being passed in.
125173
* <p>
126174
* This is a helper method that supports returning an inferred generic type.
127175
*

jsonb/src/main/java/io/avaje/jsonb/core/DJsonType.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ public String toJson(T value) {
5353
}
5454
}
5555

56+
@Override
57+
public String toJsonPretty(T value) {
58+
try (BufferedJsonWriter writer = jsonb.bufferedWriter()) {
59+
writer.pretty(true);
60+
toJson(value, writer);
61+
return writer.result();
62+
}
63+
}
64+
5665
@Override
5766
public byte[] toJsonBytes(T value) {
5867
try (BytesJsonWriter writer = jsonb.bufferedWriterAsBytes()) {

jsonb/src/main/java/io/avaje/jsonb/core/DJsonb.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class DJsonb implements Jsonb {
2424
private final JsonStreamAdapter io;
2525
private final Map<Type, DJsonType<?>> typeCache = new ConcurrentHashMap<>();
2626
private final ConcurrentHashMap<ViewKey,JsonView<?>> viewCache = new ConcurrentHashMap<>();
27+
private final JsonType<Object> anyType;
2728

2829
DJsonb(JsonStreamAdapter adapter, List<JsonAdapter.Factory> factories, boolean serializeNulls, boolean serializeEmpty, boolean failOnUnknown, boolean mathAsString) {
2930
this.builder = new CoreAdapterBuilder(this, factories, mathAsString);
@@ -37,6 +38,17 @@ class DJsonb implements Jsonb {
3738
this.io = new JsonStream(serializeNulls, serializeEmpty, failOnUnknown);
3839
}
3940
}
41+
this.anyType = type(Object.class);
42+
}
43+
44+
@Override
45+
public String toJson(Object any) {
46+
return anyType.toJson(any);
47+
}
48+
49+
@Override
50+
public String toJsonPretty(Object any) {
51+
return anyType.toJsonPretty(any);
4052
}
4153

4254
@Override

jsonb/src/main/java/io/avaje/jsonb/core/ViewBuilder.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,15 @@ public String toJson(T value) {
201201
}
202202
}
203203

204+
@Override
205+
public String toJsonPretty(T value) {
206+
try (BufferedJsonWriter writer = jsonb.bufferedWriter()) {
207+
writer.pretty(true);
208+
toJson(value, writer);
209+
return writer.result();
210+
}
211+
}
212+
204213
@Override
205214
public byte[] toJsonBytes(T value) {
206215
try (BytesJsonWriter writer = jsonb.bufferedWriterAsBytes()) {

0 commit comments

Comments
 (0)