Package org.springframework.boot.json
Class JsonWriter.Member<T>
java.lang.Object
org.springframework.boot.json.JsonWriter.Member<T>
- Type Parameters:
T
- the member type
- Enclosing interface:
- JsonWriter<T>
A member that contributes JSON. Typically, a member will contribute a single
name/value pair based on an extracted value. They may also contribute more complex
JSON structures when configured with one of the
using(...)
methods.
The when(...)
methods may be used to filter a member (omit it entirely from
the JSON). The as(Function)
method can be used to adapt to a different
type.
-
Method Summary
Modifier and TypeMethodDescription<R> JsonWriter.Member<R>
Adapt the value by applying the givenFunction
.toString()
<E,
N, V> JsonWriter.Member<T> usingExtractedPairs
(BiConsumer<T, Consumer<E>> elements, Function<E, N> nameExtractor, Function<E, V> valueExtractor) Add JSON name/value pairs by extracting values from a series of elements.<E> JsonWriter.Member<T>
usingExtractedPairs
(BiConsumer<T, Consumer<E>> elements, JsonWriter.PairExtractor<E> extractor) Add JSON name/value pairs by extracting values from a series of elements.usingMembers
(Consumer<JsonWriter.Members<T>> members) Add JSON based on furtherJsonWriter.Members
configuration.<N,
V> JsonWriter.Member<T> usingPairs
(BiConsumer<T, BiConsumer<N, V>> pairs) Add JSON name/value pairs.Only include this member when the given predicate matches.Only include this member when it is notnull
and has atoString()
that is not zero length.Only include this member when the given predicate does not match.Only include this member when it is not empty (SeeObjectUtils.isEmpty(Object)
for details).Only include this member when its value is notnull
.whenNotNull
(Function<T, ?> extractor) Only include this member when an extracted value is notnull
.
-
Method Details
-
whenNotNull
Only include this member when its value is notnull
.- Returns:
- a
JsonWriter.Member
which may be configured further
-
whenNotNull
Only include this member when an extracted value is notnull
.- Parameters:
extractor
- an function used to extract the value to test- Returns:
- a
JsonWriter.Member
which may be configured further
-
whenHasLength
Only include this member when it is notnull
and has atoString()
that is not zero length.- Returns:
- a
JsonWriter.Member
which may be configured further - See Also:
-
whenNotEmpty
Only include this member when it is not empty (SeeObjectUtils.isEmpty(Object)
for details).- Returns:
- a
JsonWriter.Member
which may be configured further
-
whenNot
Only include this member when the given predicate does not match.- Parameters:
predicate
- the predicate to test- Returns:
- a
JsonWriter.Member
which may be configured further
-
when
Only include this member when the given predicate matches.- Parameters:
predicate
- the predicate to test- Returns:
- a
JsonWriter.Member
which may be configured further
-
as
Adapt the value by applying the givenFunction
.- Type Parameters:
R
- the result type- Parameters:
adapter
- aFunction
to adapt the value- Returns:
- a
JsonWriter.Member
which may be configured further
-
usingExtractedPairs
public <E> JsonWriter.Member<T> usingExtractedPairs(BiConsumer<T, Consumer<E>> elements, JsonWriter.PairExtractor<E> extractor) Add JSON name/value pairs by extracting values from a series of elements. Typically used with aIterable.forEach(Consumer)
call, for example:members.add(Event::getTags).usingExtractedPairs(Iterable::forEach, pairExtractor);
When used with a named member, the pairs will be added as a new JSON value object:
{ "name": { "p1": 1, "p2": 2 } }
When used with an unnamed member the pairs will be added to the existing JSON object:{ "p1": 1, "p2": 2 }
- Type Parameters:
E
- the element type- Parameters:
elements
- callback used to provide the elementsextractor
- aJsonWriter.PairExtractor
used to extract the name/value pair- Returns:
- a
JsonWriter.Member
which may be configured further - See Also:
-
usingExtractedPairs
public <E,N, JsonWriter.Member<T> usingExtractedPairsV> (BiConsumer<T, Consumer<E>> elements, Function<E, N> nameExtractor, Function<E, V> valueExtractor) Add JSON name/value pairs by extracting values from a series of elements. Typically used with aIterable.forEach(Consumer)
call, for example:members.add(Event::getTags).usingExtractedPairs(Iterable::forEach, Tag::getName, Tag::getValue);
When used with a named member, the pairs will be added as a new JSON value object:
{ "name": { "p1": 1, "p2": 2 } }
When used with an unnamed member the pairs will be added to the existing JSON object:{ "p1": 1, "p2": 2 }
- Type Parameters:
E
- the element typeN
- the name typeV
- the value type- Parameters:
elements
- callback used to provide the elementsnameExtractor
-Function
used to extract the namevalueExtractor
-Function
used to extract the value- Returns:
- a
JsonWriter.Member
which may be configured further - See Also:
-
usingPairs
Add JSON name/value pairs. Typically used with aMap.forEach(BiConsumer)
call, for example:members.add(Event::getLabels).usingPairs(Map::forEach);
When used with a named member, the pairs will be added as a new JSON value object:
{ "name": { "p1": 1, "p2": 2 } }
When used with an unnamed member the pairs will be added to the existing JSON object:{ "p1": 1, "p2": 2 }
- Type Parameters:
N
- the name typeV
- the value type- Parameters:
pairs
- callback used to provide the pairs- Returns:
- a
JsonWriter.Member
which may be configured further - See Also:
-
usingMembers
Add JSON based on furtherJsonWriter.Members
configuration. For example:members.add(User::getName).usingMembers((personMembers) -> { personMembers.add("first", Name::first); personMembers.add("last", Name::last); });
When used with a named member, the result will be added as a new JSON value object:
{ "name": { "first": "Jane", "last": "Doe" } }
When used with an unnamed member the result will be added to the existing JSON object:{ "first": "John", "last": "Doe" }
- Parameters:
members
- callback to configure the members- Returns:
- a
JsonWriter.Member
which may be configured further - See Also:
-
toString
-