File tree Expand file tree Collapse file tree 4 files changed +27
-35
lines changed
jsonb/src/main/java/io/avaje/jsonb Expand file tree Collapse file tree 4 files changed +27
-35
lines changed Original file line number Diff line number Diff line change 13
13
* Moshi note: JsonType does not exist in Moshi and has been added to provide a
14
14
* slightly nicer API to use than JsonAdapter.
15
15
*/
16
- public interface JsonType <T > {
16
+ public interface JsonType <T > extends JsonView < T > {
17
17
18
18
/**
19
19
* Build and return the view given the DSL that specifies the properties to include.
@@ -51,31 +51,6 @@ public interface JsonType<T> {
51
51
*/
52
52
JsonType <Map <String , T >> map ();
53
53
54
- /**
55
- * Return the value as json content.
56
- */
57
- String toJson (T value );
58
-
59
- /**
60
- * Return the value as json content in bytes form.
61
- */
62
- byte [] toJsonBytes (T value );
63
-
64
- /**
65
- * Write the value as json content to the given JsonWriter.
66
- */
67
- void toJson (JsonWriter writer , T value );
68
-
69
- /**
70
- * Write the value as json content to the given writer.
71
- */
72
- void toJson (Writer writer , T value );
73
-
74
- /**
75
- * Write the value as json content to the given outputStream.
76
- */
77
- void toJson (OutputStream outputStream , T value );
78
-
79
54
/**
80
55
* Read the return the value from the reader.
81
56
*/
Original file line number Diff line number Diff line change @@ -34,6 +34,11 @@ public interface JsonView<T> {
34
34
*/
35
35
String toJson (T value );
36
36
37
+ /**
38
+ * Return the value as json content in bytes form.
39
+ */
40
+ byte [] toJsonBytes (T value );
41
+
37
42
/**
38
43
* Write to the given writer.
39
44
*/
Original file line number Diff line number Diff line change @@ -47,16 +47,18 @@ public JsonType<Map<String, T>> map() {
47
47
48
48
@ Override
49
49
public String toJson (T value ) {
50
- BufferedJsonWriter bufferedJsonWriter = jsonb .bufferedWriter ();
51
- toJson (bufferedJsonWriter , value );
52
- return bufferedJsonWriter .result ();
50
+ try (BufferedJsonWriter bufferedJsonWriter = jsonb .bufferedWriter ()) {
51
+ toJson (bufferedJsonWriter , value );
52
+ return bufferedJsonWriter .result ();
53
+ }
53
54
}
54
55
55
56
@ Override
56
57
public byte [] toJsonBytes (T value ) {
57
- BytesJsonWriter bytesWriter = jsonb .bufferedWriterAsBytes ();
58
- toJson (bytesWriter , value );
59
- return bytesWriter .result ();
58
+ try (BytesJsonWriter bytesWriter = jsonb .bufferedWriterAsBytes ()) {
59
+ toJson (bytesWriter , value );
60
+ return bytesWriter .result ();
61
+ }
60
62
}
61
63
62
64
@ Override
Original file line number Diff line number Diff line change 2
2
3
3
import io .avaje .jsonb .*;
4
4
import io .avaje .jsonb .spi .BufferedJsonWriter ;
5
+ import io .avaje .jsonb .spi .BytesJsonWriter ;
5
6
import io .avaje .jsonb .spi .PropertyNames ;
6
7
7
8
import java .io .IOException ;
@@ -194,9 +195,18 @@ private static final class DView<T> implements JsonView<T> {
194
195
195
196
@ Override
196
197
public String toJson (T value ) {
197
- BufferedJsonWriter bufferedJsonWriter = jsonb .bufferedWriter ();
198
- toJson (bufferedJsonWriter , value );
199
- return bufferedJsonWriter .result ();
198
+ try (BufferedJsonWriter bufferedJsonWriter = jsonb .bufferedWriter ()) {
199
+ toJson (bufferedJsonWriter , value );
200
+ return bufferedJsonWriter .result ();
201
+ }
202
+ }
203
+
204
+ @ Override
205
+ public byte [] toJsonBytes (T value ) {
206
+ try (BytesJsonWriter bytesWriter = jsonb .bufferedWriterAsBytes ()) {
207
+ toJson (bytesWriter , value );
208
+ return bytesWriter .result ();
209
+ }
200
210
}
201
211
202
212
@ Override
You can’t perform that action at this time.
0 commit comments