Skip to content

Commit 360317e

Browse files
committed
#11 - Add convenience methods to Jsonb for toJson ... bytes, writer, outputStream
1 parent fe720f6 commit 360317e

File tree

3 files changed

+114
-9
lines changed

3 files changed

+114
-9
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
import io.avaje.jsonb.*;
44
import org.junit.jupiter.api.Test;
55

6+
import java.io.ByteArrayOutputStream;
7+
import java.io.OutputStreamWriter;
68
import java.io.StringWriter;
9+
import java.nio.charset.StandardCharsets;
710
import java.time.Instant;
811
import java.util.*;
912

@@ -29,6 +32,31 @@ void anyToJsonPretty() {
2932
assertThat(asJson).isEqualTo("{\n \"id\": 42,\n \"name\": \"rob\",\n \"status\": \"ACTIVE\"\n}");
3033
}
3134

35+
@Test
36+
void anyToWriter() {
37+
var customer = new Customer().id(42L).name("rob").status(Customer.Status.ACTIVE);
38+
StringWriter writer = new StringWriter();
39+
jsonb.toJson(customer, writer);
40+
assertThat(writer.toString()).isEqualTo("{\"id\":42,\"name\":\"rob\",\"status\":\"ACTIVE\"}");
41+
}
42+
43+
@Test
44+
void anyToOutputStream() {
45+
var customer = new Customer().id(42L).name("rob").status(Customer.Status.ACTIVE);
46+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
47+
jsonb.toJson(customer, baos);
48+
String asString = baos.toString(StandardCharsets.UTF_8);
49+
assertThat(asString).isEqualTo("{\"id\":42,\"name\":\"rob\",\"status\":\"ACTIVE\"}");
50+
}
51+
52+
@Test
53+
void anyToBytes() {
54+
var customer = new Customer().id(42L).name("rob").status(Customer.Status.ACTIVE);
55+
byte[] bytes = jsonb.toJsonBytes(customer);
56+
String asString = new String(bytes, StandardCharsets.UTF_8);
57+
assertThat(asString).isEqualTo("{\"id\":42,\"name\":\"rob\",\"status\":\"ACTIVE\"}");
58+
}
59+
3260
@Test
3361
void toJson_view() {
3462
JsonView<Customer> customerJsonView = jsonb.type(Customer.class).view("(id, name)");

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

Lines changed: 71 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ static Builder newBuilder() {
8989
/**
9090
* Return json content for the given object.
9191
* <p>
92-
* This is a convenience method for using {@code jsonb.type(Object.class).toJson(any) }
92+
* This is a convenience method for {@code jsonb.type(Object.class).toJson(any) }
9393
*
9494
* @param any The object to return as json string
9595
* @return Return json content for the given object.
@@ -99,28 +99,61 @@ static Builder newBuilder() {
9999
/**
100100
* Return json content in pretty format for the given object.
101101
* <p>
102-
* This is a convenience method for using {@code jsonb.type(Object.class).toJsonPretty(any) }
102+
* This is a convenience method for {@code jsonb.type(Object.class).toJsonPretty(any) }
103103
*
104104
* @param any The object to return as json string in pretty format
105105
* @return Return json content in pretty format for the given object.
106106
*/
107107
String toJsonPretty(Object any);
108108

109+
/**
110+
* Return the value as json content in bytes form.
111+
* <p>
112+
* This is a convenience method for {@code jsonb.type(Object.class).toJsonBytes(any) }
113+
*/
114+
byte[] toJsonBytes(Object any);
115+
116+
/**
117+
* Write to the given writer.
118+
* <p>
119+
* This is a convenience method for {@code jsonb.type(Object.class).toJson(any, writer) }
120+
*/
121+
void toJson(Object any, Writer writer);
122+
123+
/**
124+
* Write to the given outputStream.
125+
* <p>
126+
* This is a convenience method for {@code jsonb.type(Object.class).toJsonBytes(any, outputStream) }
127+
*/
128+
void toJson(Object any, OutputStream outputStream);
129+
109130
/**
110131
* Return the JsonType used to read and write json for the given class.
111132
*
112-
* <h3>Examples</h3>
133+
* <h3>fromJson() example</h3>
113134
* <pre>{@code
114135
*
115-
* JsonType<Customer> customerType = jsonb.type(Customer.class)
136+
* Customer customer = jsonb
137+
* .type(Customer.class)
138+
* .fromJson(jsonContent);
116139
*
117-
* Customer customer = ...
118-
* customerType.toJson(customer);
119140
*
120-
* JsonType<List<Customer>> customerListType = customerType.list()
141+
* // list
142+
* List<Customer> customers = jsonb
143+
* .type(Customer.class)
144+
* .list()
145+
* .fromJson(jsonContent);
146+
*
147+
* }</pre>
148+
*
149+
* <h3>toJson() example</h3>
150+
* <pre>{@code
151+
*
152+
* Customer customer = ...
121153
*
122-
* List<Customer> customers = ...
123-
* customerListType.toJson(customers);
154+
* String jsonContent = jsonb
155+
* .type(Customer.class)
156+
* .toJson(customer);
124157
*
125158
* }</pre>
126159
*
@@ -129,6 +162,21 @@ static Builder newBuilder() {
129162
* We can use <code>type(Object.class)</code> when we don't know the specific type that is being
130163
* written toJson or read fromJson.
131164
* <p>
165+
*
166+
* <h3>Object toJson()</h3>
167+
* <pre>{@code
168+
*
169+
* Object any = ...
170+
*
171+
* String jsonContent = jsonb
172+
* .type(Object.class)
173+
* .toJson(any);
174+
*
175+
* // the same as
176+
* String jsonContent = jsonb.toJson(any);
177+
*
178+
* }</pre>
179+
* <p>
132180
* When using <code>Object.class</code> and writing <code>toJson()</code> then the underlying JsonAdapter
133181
* is determined dynamically based on the type of the object value passed in.
134182
* <p>
@@ -158,6 +206,20 @@ static Builder newBuilder() {
158206
* <p>
159207
* We can use <code>type(Object.class)</code> when we don't know the specific type that is being
160208
* written toJson or read fromJson.
209+
*
210+
* <h3>Object toJson()</h3>
211+
* <pre>{@code
212+
*
213+
* Object any = ...
214+
*
215+
* String jsonContent = jsonb
216+
* .type(Object.class)
217+
* .toJson(any);
218+
*
219+
* // the same as
220+
* String jsonContent = jsonb.toJson(any);
221+
*
222+
* }</pre>
161223
* <p>
162224
* When using <code>Object.class</code> and writing <code>toJson()</code> then the underlying JsonAdapter
163225
* is determined dynamically based on the type of the object value passed in.

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,21 @@ public String toJsonPretty(Object any) {
5151
return anyType.toJsonPretty(any);
5252
}
5353

54+
@Override
55+
public byte[] toJsonBytes(Object any) {
56+
return anyType.toJsonBytes(any);
57+
}
58+
59+
@Override
60+
public void toJson(Object any, Writer writer) {
61+
anyType.toJson(any, writer);
62+
}
63+
64+
@Override
65+
public void toJson(Object any, OutputStream outputStream) {
66+
anyType.toJson(any, outputStream);
67+
}
68+
5469
@Override
5570
public PropertyNames properties(String... names) {
5671
return io.properties(names);

0 commit comments

Comments
 (0)