Package org.springframework.boot.json
Interface JsonWriter<T>
- Type Parameters:
T- the type being written
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
Interface that can be used to write JSON output. Typically used to generate JSON when a
dependency on a fully marshalling library (such as Jackson or Gson) cannot be assumed.
For standard Java types, the standard() factory method may be used to obtain
an instance of this interface. It supports String, Number and
Boolean as well as Collection, Array, Map and
WritableJson types. Typical usage would be:
JsonWriter<Map<String,Object>> writer = JsonWriter.standard();
writer.write(Map.of("Hello", "World!"), out);
More complex mappings can be created using the of(Consumer) method with a
callback to configure the JSON members that should be written. Typical
usage would be:
JsonWriter<Person> writer = JsonWriter.of((members) -> {
members.add("first", Person::firstName);
members.add("last", Person::lastName);
members.add("dob", Person::dateOfBirth)
.whenNotNull()
.as(DateTimeFormatter.ISO_DATE::format);
});
writer.write(person, out);
The writeToString(Object) method can be used if you want to write the JSON
directly to a String. To write to other types of output, the
write(Object) method may be used to obtain a WritableJson instance.
- Since:
- 3.4.0
- Author:
- Phillip Webb, Moritz Halbritter
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic final classA member that contributes JSON.static final recordA path used to identify a specific JSON member.static final classCallback used to configure JSON members.static interfaceCallback interface that can beappliedtoJsonWriter.Membersto change names or filter members.static interfaceInterface that can be used to extract name/value pairs from an element.static interfaceCallback interface that can beappliedtoJsonWriter.Membersto process values before they are written. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> JsonWriter<T>of(Consumer<JsonWriter.Members<T>> members) Factory method to return aJsonWriterwith specificmember mapping.static <T> JsonWriter<T>standard()Factory method to return aJsonWriterfor standard Java types.default JsonWriter<T>Return a newJsonWriterinstance that appends a new line after the JSON has been written.default JsonWriter<T>withSuffix(String suffix) Return a newJsonWriterinstance that appends the given suffix after the JSON has been written.default WritableJsonProvide aWritableJsonimplementation that may be used to write the given instance to various outputs.voidwrite(T instance, Appendable out) Write the given instance to the providedAppendable.default StringwriteToString(T instance) Write the given instance to a JSON string.
-
Method Details
-
write
Write the given instance to the providedAppendable.- Parameters:
instance- the instance to write (may benullout- the output that should receive the JSON- Throws:
IOException- on IO error
-
writeToString
Write the given instance to a JSON string.- Parameters:
instance- the instance to write (may benull)- Returns:
- the JSON string
-
write
Provide aWritableJsonimplementation that may be used to write the given instance to various outputs.- Parameters:
instance- the instance to write (may benull)- Returns:
- a
WritableJsoninstance that may be used to write the JSON
-
withNewLineAtEnd
Return a newJsonWriterinstance that appends a new line after the JSON has been written.- Returns:
- a new
JsonWriterinstance that appends a new line after the JSON
-
withSuffix
Return a newJsonWriterinstance that appends the given suffix after the JSON has been written.- Parameters:
suffix- the suffix to write, if any- Returns:
- a new
JsonWriterinstance that appends a suffixafter the JSON
-
standard
Factory method to return aJsonWriterfor standard Java types. Seeclass-level javadocfor details.- Type Parameters:
T- the type to write- Returns:
- a
JsonWriterinstance
-
of
Factory method to return aJsonWriterwith specificmember mapping. Seeclass-level javadocandJsonWriter.Membersfor details.- Type Parameters:
T- the type to write- Parameters:
members- a consumer, which should configure the members- Returns:
- a
JsonWriterinstance - See Also:
-