Skip to content

Commit aed4546

Browse files
committed
Extract WritableJson from JsonWriter
Make `WritableJson` a top level class rather than a nested class inside `WritableJson`. Closes gh-42595
1 parent b169439 commit aed4546

File tree

8 files changed

+329
-281
lines changed

8 files changed

+329
-281
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/json/JsonValueWriter.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.util.function.BiConsumer;
2626
import java.util.function.Consumer;
2727

28-
import org.springframework.boot.json.JsonWriter.WritableJson;
2928
import org.springframework.util.Assert;
3029
import org.springframework.util.ObjectUtils;
3130
import org.springframework.util.function.ThrowingConsumer;

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/json/JsonWriter.java

Lines changed: 0 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,7 @@
1616

1717
package org.springframework.boot.json;
1818

19-
import java.io.ByteArrayOutputStream;
2019
import java.io.IOException;
21-
import java.io.OutputStream;
22-
import java.io.OutputStreamWriter;
23-
import java.io.UncheckedIOException;
24-
import java.io.Writer;
25-
import java.nio.charset.Charset;
26-
import java.nio.charset.StandardCharsets;
2720
import java.util.ArrayList;
2821
import java.util.Collection;
2922
import java.util.List;
@@ -37,7 +30,6 @@
3730

3831
import org.springframework.boot.json.JsonValueWriter.Series;
3932
import org.springframework.boot.json.JsonWriter.Member.Extractor;
40-
import org.springframework.core.io.WritableResource;
4133
import org.springframework.util.Assert;
4234
import org.springframework.util.ObjectUtils;
4335
import org.springframework.util.StringUtils;
@@ -159,148 +151,6 @@ static <T> JsonWriter<T> of(Consumer<Members<T>> members) {
159151
return (instance, out) -> initializedMembers.write(instance, new JsonValueWriter(out));
160152
}
161153

162-
/**
163-
* JSON content that can be written out.
164-
*/
165-
@FunctionalInterface
166-
interface WritableJson {
167-
168-
/**
169-
* Write the JSON to the provided {@link Appendable}.
170-
* @param out the {@link Appendable} to receive the JSON
171-
* @throws IOException on IO error
172-
*/
173-
void to(Appendable out) throws IOException;
174-
175-
/**
176-
* Write the JSON to a {@link String}.
177-
* @return the JSON string
178-
*/
179-
default String toJsonString() {
180-
try {
181-
StringBuilder stringBuilder = new StringBuilder();
182-
to(stringBuilder);
183-
return stringBuilder.toString();
184-
}
185-
catch (IOException ex) {
186-
throw new UncheckedIOException(ex);
187-
}
188-
}
189-
190-
/**
191-
* Write the JSON to a UTF-8 encoded byte array.
192-
* @return the JSON bytes
193-
*/
194-
default byte[] toByteArray() {
195-
return toByteArray(StandardCharsets.UTF_8);
196-
}
197-
198-
/**
199-
* Write the JSON to a byte array.
200-
* @param charset the charset
201-
* @return the JSON bytes
202-
*/
203-
default byte[] toByteArray(Charset charset) {
204-
Assert.notNull(charset, "'charset' must not be null");
205-
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
206-
toWriter(new OutputStreamWriter(out, charset));
207-
return out.toByteArray();
208-
}
209-
catch (IOException ex) {
210-
throw new UncheckedIOException(ex);
211-
}
212-
}
213-
214-
/**
215-
* Write the JSON to the provided {@link WritableResource} using
216-
* {@link StandardCharsets#UTF_8 UTF8} encoding.
217-
* @param out the {@link OutputStream} to receive the JSON
218-
* @throws IOException on IO error
219-
*/
220-
default void toResource(WritableResource out) throws IOException {
221-
Assert.notNull(out, "'out' must not be null");
222-
try (OutputStream outputStream = out.getOutputStream()) {
223-
toOutputStream(outputStream);
224-
}
225-
}
226-
227-
/**
228-
* Write the JSON to the provided {@link WritableResource} using the given
229-
* {@link Charset}.
230-
* @param out the {@link OutputStream} to receive the JSON
231-
* @param charset the charset to use
232-
* @throws IOException on IO error
233-
*/
234-
default void toResource(WritableResource out, Charset charset) throws IOException {
235-
Assert.notNull(out, "'out' must not be null");
236-
Assert.notNull(charset, "'charset' must not be null");
237-
try (OutputStream outputStream = out.getOutputStream()) {
238-
toOutputStream(outputStream, charset);
239-
}
240-
}
241-
242-
/**
243-
* Write the JSON to the provided {@link OutputStream} using
244-
* {@link StandardCharsets#UTF_8 UTF8} encoding. The output stream will not be
245-
* closed.
246-
* @param out the {@link OutputStream} to receive the JSON
247-
* @throws IOException on IO error
248-
* @see #toOutputStream(OutputStream, Charset)
249-
*/
250-
default void toOutputStream(OutputStream out) throws IOException {
251-
toOutputStream(out, StandardCharsets.UTF_8);
252-
}
253-
254-
/**
255-
* Write the JSON to the provided {@link OutputStream} using the given
256-
* {@link Charset}. The output stream will not be closed.
257-
* @param out the {@link OutputStream} to receive the JSON
258-
* @param charset the charset to use
259-
* @throws IOException on IO error
260-
*/
261-
default void toOutputStream(OutputStream out, Charset charset) throws IOException {
262-
Assert.notNull(out, "'out' must not be null");
263-
Assert.notNull(charset, "'charset' must not be null");
264-
toWriter(new OutputStreamWriter(out, charset));
265-
}
266-
267-
/**
268-
* Write the JSON to the provided {@link Writer}. The writer will be flushed but
269-
* not closed.
270-
* @param out the {@link Writer} to receive the JSON
271-
* @throws IOException on IO error
272-
* @see #toOutputStream(OutputStream, Charset)
273-
*/
274-
default void toWriter(Writer out) throws IOException {
275-
Assert.notNull(out, "'out' must not be null");
276-
to(out);
277-
out.flush();
278-
}
279-
280-
/**
281-
* Factory method used to create a {@link WritableJson} with a sensible
282-
* {@link Object#toString()} that delegate to {@link WritableJson#toJsonString()}.
283-
* @param writableJson the source {@link WritableJson}
284-
* @return a new {@link WritableJson} with a sensible {@link Object#toString()}.
285-
*/
286-
static WritableJson of(WritableJson writableJson) {
287-
return new WritableJson() {
288-
289-
@Override
290-
public void to(Appendable out) throws IOException {
291-
writableJson.to(out);
292-
}
293-
294-
@Override
295-
public String toString() {
296-
return toJsonString();
297-
}
298-
299-
};
300-
}
301-
302-
}
303-
304154
/**
305155
* Callback used to configure JSON members. Individual members can be declared using
306156
* the various {@code add(...)} methods. Typically, members are declared with a
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.json;
18+
19+
import java.io.ByteArrayOutputStream;
20+
import java.io.IOException;
21+
import java.io.OutputStream;
22+
import java.io.OutputStreamWriter;
23+
import java.io.UncheckedIOException;
24+
import java.io.Writer;
25+
import java.nio.charset.Charset;
26+
import java.nio.charset.StandardCharsets;
27+
28+
import org.springframework.core.io.WritableResource;
29+
import org.springframework.util.Assert;
30+
31+
/**
32+
* JSON content that can be written out.
33+
*
34+
* @author Phillip Webb
35+
* @author Moritz Halbritter
36+
* @since 3.4.0
37+
* @see JsonWriter
38+
*/
39+
@FunctionalInterface
40+
public interface WritableJson {
41+
42+
/**
43+
* Write the JSON to the provided {@link Appendable}.
44+
* @param out the {@link Appendable} to receive the JSON
45+
* @throws IOException on IO error
46+
*/
47+
void to(Appendable out) throws IOException;
48+
49+
/**
50+
* Write the JSON to a {@link String}.
51+
* @return the JSON string
52+
*/
53+
default String toJsonString() {
54+
try {
55+
StringBuilder stringBuilder = new StringBuilder();
56+
to(stringBuilder);
57+
return stringBuilder.toString();
58+
}
59+
catch (IOException ex) {
60+
throw new UncheckedIOException(ex);
61+
}
62+
}
63+
64+
/**
65+
* Write the JSON to a UTF-8 encoded byte array.
66+
* @return the JSON bytes
67+
*/
68+
default byte[] toByteArray() {
69+
return toByteArray(StandardCharsets.UTF_8);
70+
}
71+
72+
/**
73+
* Write the JSON to a byte array.
74+
* @param charset the charset
75+
* @return the JSON bytes
76+
*/
77+
default byte[] toByteArray(Charset charset) {
78+
Assert.notNull(charset, "'charset' must not be null");
79+
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
80+
toWriter(new OutputStreamWriter(out, charset));
81+
return out.toByteArray();
82+
}
83+
catch (IOException ex) {
84+
throw new UncheckedIOException(ex);
85+
}
86+
}
87+
88+
/**
89+
* Write the JSON to the provided {@link WritableResource} using
90+
* {@link StandardCharsets#UTF_8 UTF8} encoding.
91+
* @param out the {@link OutputStream} to receive the JSON
92+
* @throws IOException on IO error
93+
*/
94+
default void toResource(WritableResource out) throws IOException {
95+
Assert.notNull(out, "'out' must not be null");
96+
try (OutputStream outputStream = out.getOutputStream()) {
97+
toOutputStream(outputStream);
98+
}
99+
}
100+
101+
/**
102+
* Write the JSON to the provided {@link WritableResource} using the given
103+
* {@link Charset}.
104+
* @param out the {@link OutputStream} to receive the JSON
105+
* @param charset the charset to use
106+
* @throws IOException on IO error
107+
*/
108+
default void toResource(WritableResource out, Charset charset) throws IOException {
109+
Assert.notNull(out, "'out' must not be null");
110+
Assert.notNull(charset, "'charset' must not be null");
111+
try (OutputStream outputStream = out.getOutputStream()) {
112+
toOutputStream(outputStream, charset);
113+
}
114+
}
115+
116+
/**
117+
* Write the JSON to the provided {@link OutputStream} using
118+
* {@link StandardCharsets#UTF_8 UTF8} encoding. The output stream will not be closed.
119+
* @param out the {@link OutputStream} to receive the JSON
120+
* @throws IOException on IO error
121+
* @see #toOutputStream(OutputStream, Charset)
122+
*/
123+
default void toOutputStream(OutputStream out) throws IOException {
124+
toOutputStream(out, StandardCharsets.UTF_8);
125+
}
126+
127+
/**
128+
* Write the JSON to the provided {@link OutputStream} using the given
129+
* {@link Charset}. The output stream will not be closed.
130+
* @param out the {@link OutputStream} to receive the JSON
131+
* @param charset the charset to use
132+
* @throws IOException on IO error
133+
*/
134+
default void toOutputStream(OutputStream out, Charset charset) throws IOException {
135+
Assert.notNull(out, "'out' must not be null");
136+
Assert.notNull(charset, "'charset' must not be null");
137+
toWriter(new OutputStreamWriter(out, charset));
138+
}
139+
140+
/**
141+
* Write the JSON to the provided {@link Writer}. The writer will be flushed but not
142+
* closed.
143+
* @param out the {@link Writer} to receive the JSON
144+
* @throws IOException on IO error
145+
* @see #toOutputStream(OutputStream, Charset)
146+
*/
147+
default void toWriter(Writer out) throws IOException {
148+
Assert.notNull(out, "'out' must not be null");
149+
to(out);
150+
out.flush();
151+
}
152+
153+
/**
154+
* Factory method used to create a {@link WritableJson} with a sensible
155+
* {@link Object#toString()} that delegate to {@link WritableJson#toJsonString()}.
156+
* @param writableJson the source {@link WritableJson}
157+
* @return a new {@link WritableJson} with a sensible {@link Object#toString()}.
158+
*/
159+
static WritableJson of(WritableJson writableJson) {
160+
return new WritableJson() {
161+
162+
@Override
163+
public void to(Appendable out) throws IOException {
164+
writableJson.to(out);
165+
}
166+
167+
@Override
168+
public String toString() {
169+
return toJsonString();
170+
}
171+
172+
};
173+
}
174+
175+
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/GraylogExtendedLogFormatStructuredLogFormatter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
import org.springframework.boot.json.JsonWriter;
3535
import org.springframework.boot.json.JsonWriter.Members;
36-
import org.springframework.boot.json.JsonWriter.WritableJson;
36+
import org.springframework.boot.json.WritableJson;
3737
import org.springframework.boot.logging.structured.CommonStructuredLogFormat;
3838
import org.springframework.boot.logging.structured.GraylogExtendedLogFormatService;
3939
import org.springframework.boot.logging.structured.JsonWriterStructuredLogFormatter;

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/StructuredMessage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import org.apache.logging.log4j.message.Message;
2222
import org.apache.logging.log4j.util.MultiFormatStringBuilderFormattable;
2323

24-
import org.springframework.boot.json.JsonWriter.WritableJson;
24+
import org.springframework.boot.json.WritableJson;
2525

2626
/**
2727
* Helper used to adapt {@link Message} for structured writing.

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/GraylogExtendedLogFormatStructuredLogFormatter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
import org.springframework.boot.json.JsonWriter;
3535
import org.springframework.boot.json.JsonWriter.Members;
36-
import org.springframework.boot.json.JsonWriter.WritableJson;
36+
import org.springframework.boot.json.WritableJson;
3737
import org.springframework.boot.logging.structured.CommonStructuredLogFormat;
3838
import org.springframework.boot.logging.structured.GraylogExtendedLogFormatService;
3939
import org.springframework.boot.logging.structured.JsonWriterStructuredLogFormatter;

0 commit comments

Comments
 (0)