Class JsonWriter.Member<T>

java.lang.Object
org.springframework.boot.json.JsonWriter.Member<T>
Type Parameters:
T - the member type
Enclosing interface:
JsonWriter<T>

public static final class JsonWriter.Member<T> extends Object
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 Details

    • whenNotNull

      public JsonWriter.Member<T> whenNotNull()
      Only include this member when its value is not null.
      Returns:
      a JsonWriter.Member which may be configured further
    • whenNotNull

      public JsonWriter.Member<T> whenNotNull(Function<T,?> extractor)
      Only include this member when an extracted value is not null.
      Parameters:
      extractor - an function used to extract the value to test
      Returns:
      a JsonWriter.Member which may be configured further
    • whenHasLength

      public JsonWriter.Member<T> whenHasLength()
      Only include this member when it is not null and has a toString() that is not zero length.
      Returns:
      a JsonWriter.Member which may be configured further
      See Also:
      • StringUtils.hasLength(CharSequence)
    • whenNotEmpty

      public JsonWriter.Member<T> whenNotEmpty()
      Only include this member when it is not empty (See ObjectUtils.isEmpty(Object) for details).
      Returns:
      a JsonWriter.Member which may be configured further
    • whenNot

      public JsonWriter.Member<T> whenNot(Predicate<T> predicate)
      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

      public JsonWriter.Member<T> when(Predicate<T> predicate)
      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

      public <R> JsonWriter.Member<R> as(Function<T,R> adapter)
      Adapt the value by applying the given Function.
      Type Parameters:
      R - the result type
      Parameters:
      adapter - a Function 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 a Iterable.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 elements
      extractor - a JsonWriter.PairExtractor used to extract the name/value pair
      Returns:
      a JsonWriter.Member which may be configured further
      See Also:
    • usingExtractedPairs

      public <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. Typically used with a Iterable.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 type
      N - the name type
      V - the value type
      Parameters:
      elements - callback used to provide the elements
      nameExtractor - Function used to extract the name
      valueExtractor - Function used to extract the value
      Returns:
      a JsonWriter.Member which may be configured further
      See Also:
    • usingPairs

      public <N, V> JsonWriter.Member<T> usingPairs(BiConsumer<T,BiConsumer<N,V>> pairs)
      Add JSON name/value pairs. Typically used with a Map.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 type
      V - the value type
      Parameters:
      pairs - callback used to provide the pairs
      Returns:
      a JsonWriter.Member which may be configured further
      See Also:
    • usingMembers

      public JsonWriter.Member<T> usingMembers(Consumer<JsonWriter.Members<T>> members)
      Add JSON based on further JsonWriter.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

      public String toString()
      Overrides:
      toString in class Object