接口 JsonWriter<T>
- 类型参数:
T
- the type being written
- 函数接口:
- 这是一个函数接口, 因此可用作 lambda 表达式或方法引用的赋值目标。
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.
- 从以下版本开始:
- 3.4.0
- 作者:
- Phillip Webb, Moritz Halbritter
-
嵌套类概要
嵌套类修饰符和类型接口说明static final class
A member that contributes JSON.static final record
A path used to identify a specific JSON member.static final class
Callback used to configure JSON members.static interface
Callback interface that can beapplied
toJsonWriter.Members
to change names or filter members.static interface
Interface that can be used to extract name/value pairs from an element.static interface
Callback interface that can beapplied
toJsonWriter.Members
to process values before they are written. -
方法概要
修饰符和类型方法说明static <T> JsonWriter
<T> of
(Consumer<JsonWriter.Members<T>> members) Factory method to return aJsonWriter
with specificmember mapping
.static <T> JsonWriter
<T> standard()
Factory method to return aJsonWriter
for standard Java types.default JsonWriter
<T> Return a newJsonWriter
instance that appends a new line after the JSON has been written.default JsonWriter
<T> withSuffix
(String suffix) Return a newJsonWriter
instance that appends the given suffix after the JSON has been written.default WritableJson
Provide aWritableJson
implementation that may be used to write the given instance to various outputs.void
write
(T instance, Appendable out) Write the given instance to the providedAppendable
.default String
writeToString
(T instance) Write the given instance to a JSON string.
-
方法详细资料
-
write
Write the given instance to the providedAppendable
.- 参数:
instance
- the instance to write (may benull
out
- the output that should receive the JSON- 抛出:
IOException
- on IO error
-
writeToString
Write the given instance to a JSON string.- 参数:
instance
- the instance to write (may benull
)- 返回:
- the JSON string
-
write
Provide aWritableJson
implementation that may be used to write the given instance to various outputs.- 参数:
instance
- the instance to write (may benull
)- 返回:
- a
WritableJson
instance that may be used to write the JSON
-
withNewLineAtEnd
Return a newJsonWriter
instance that appends a new line after the JSON has been written.- 返回:
- a new
JsonWriter
instance that appends a new line after the JSON
-
withSuffix
Return a newJsonWriter
instance that appends the given suffix after the JSON has been written.- 参数:
suffix
- the suffix to write, if any- 返回:
- a new
JsonWriter
instance that appends a suffixafter the JSON
-
standard
Factory method to return aJsonWriter
for standard Java types. Seeclass-level javadoc
for details.- 类型参数:
T
- the type to write- 返回:
- a
JsonWriter
instance
-
of
Factory method to return aJsonWriter
with specificmember mapping
. Seeclass-level javadoc
andJsonWriter.Members
for details.- 类型参数:
T
- the type to write- 参数:
members
- a consumer, which should configure the members- 返回:
- a
JsonWriter
instance - 另请参阅:
-