Record Class JsonContent

java.lang.Object
java.lang.Record
org.springframework.ai.systemone.api.JsonContent
Record Components:
value - the wrapped value; a String, Map, List, any POJO Jackson serializes to a string, object or array, or null

public record JsonContent(@Nullable Object value) extends Record
The polymorphic JSON payload that the System One API accepts wherever the documentation refers to an EntryType: state, every instructions value and every criteria description may be a string, a JSON object, a JSON array or null.

The wrapped value is serialized transparently (via JsonValue), so a JsonContent holding a String writes as a bare JSON string and one holding a Map writes as a JSON object. Anything Jackson can serialize may be wrapped — an enum, a record, a java.time value or a type carrying its own JsonValue — provided it produces one of the three shapes above.

A bare scalar is not an EntryType. The API accepts only a string, an object, an array or null, and answers 422 for anything else, so a Number, Boolean or a @JsonValue type that serializes to one of those is rejected on the wire even though it wraps without complaint here. Whether a given POJO is valid is a question about the JSON it produces, which only Jackson can answer, so it is not checked at construction.

Note that asText(), asMap() and asList() inspect the Java value rather than the JSON it would serialize to; see their documentation.

Since:
2.1.0
Author:
Christian Tzolov
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final JsonContent
    Shared instance representing an explicit JSON null.
  • Constructor Summary

    Constructors
    Constructor
    Description
    JsonContent(@Nullable Object value)
    Creates an instance of a JsonContent record class.
  • Method Summary

    Modifier and Type
    Method
    Description
    <T> @Nullable T
    as(Class<T> type, tools.jackson.databind.json.JsonMapper jsonMapper)
    Converts the wrapped value into the given type using Jackson — the JSON view, as opposed to the instanceof checks the other accessors perform.
    Reads the wrapped value as a list.
    Reads the wrapped value as a map.
    @Nullable String
    Reads the wrapped value as text.
    final boolean
    Indicates whether some other object is "equal to" this one.
    final int
    Returns a hash code value for this object.
    boolean
     
    object(@Nullable Object... keysAndValues)
    A JSON object whose keys keep the order they are written in: JsonContent.object("question", "...", "inspect", "message").
    of(@Nullable Object value)
    Wraps an arbitrary value.
    Renders the wrapped value as a short human readable string, used when the client has to describe a criterion in a log or a feedback message.
    final String
    Returns a string representation of this record class.
    @Nullable Object
    Returns the value of the value record component.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • NULL

      public static final JsonContent NULL
      Shared instance representing an explicit JSON null.
  • Constructor Details

    • JsonContent

      public JsonContent(@Nullable Object value)
      Creates an instance of a JsonContent record class.
      Parameters:
      value - the value for the value record component
  • Method Details

    • of

      public static JsonContent of(@Nullable Object value)
      Wraps an arbitrary value. Also the Jackson entry point when reading a JsonContent back from a response.
      Parameters:
      value - the value to wrap; may be null
      Returns:
      the wrapped value, never null
    • object

      public static JsonContent object(@Nullable Object... keysAndValues)
      A JSON object whose keys keep the order they are written in: JsonContent.object("question", "...", "inspect", "message").

      Use it instead of Map.of for a state, structured instructions or a criteria description with more than one key. The model reads them in the order they are sent, and a different order can change an answer; Map.of iterates in an order that changes with every JVM run, so the same code would send a different request each time.

      Parameters:
      keysAndValues - alternating keys and values; each key a non-empty String, each value anything a JsonContent can wrap, or null for a JSON null
      Returns:
      the object, backed by an unmodifiable insertion-ordered map
      Throws:
      IllegalArgumentException - when the arguments are not key-value pairs, a key is not a non-empty string, or a key repeats
    • isNull

      public boolean isNull()
      Returns:
      true when this instance wraps a JSON null
    • asText

      public @Nullable String asText()
      Reads the wrapped value as text. This is the Java view: a POJO that serializes to a JSON string still answers null here, because the value is not a String. Use as(Class, JsonMapper) for the Jackson view.
      Returns:
      the wrapped value as text, or null when it is not a String
    • asMap

      public Map<String,Object> asMap()
      Reads the wrapped value as a map. This is the Java view: a POJO that serializes to a JSON object still answers an empty map here, because the value is not a Map. Note also that an empty result does not distinguish "not an object" from "an empty object" — use as(Class, JsonMapper) when that matters.
      Returns:
      the wrapped value as a map, or an empty map when it is not a Map
    • asList

      public List<Object> asList()
      Reads the wrapped value as a list. This is the Java view, with the same caveats as asMap().
      Returns:
      the wrapped value as a list, or an empty list when it is not a List
    • as

      public <T> @Nullable T as(Class<T> type, tools.jackson.databind.json.JsonMapper jsonMapper)
      Converts the wrapped value into the given type using Jackson — the JSON view, as opposed to the instanceof checks the other accessors perform.

      Because the conversion goes through Jackson it honours the value's own JsonValue, so a type that serializes to a string is a string as far as this method is concerned and converting it to a Map throws rather than yielding its fields.

      Type Parameters:
      T - the target type
      Parameters:
      type - the target type
      jsonMapper - the mapper used for the conversion
      Returns:
      the converted value, or null when this instance wraps a JSON null
    • toDisplayString

      public String toDisplayString()
      Renders the wrapped value as a short human readable string, used when the client has to describe a criterion in a log or a feedback message.

      A string renders bare, without quotes, because that is what reads well when a rubric level or a criterion description is quoted back. Everything else renders as the JSON it serializes to, so what a model is told about a value matches what was sent rather than Java's toString() syntax.

      The mapper used here is not the one the transport serializes the request with, so a value that depends on application-configured Jackson modules can render slightly differently from what goes on the wire. A value that cannot be serialized at all falls back to toString(): this method is used in exception messages and logs and must never throw.

      Returns:
      a display string, never null
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • value

      public @Nullable Object value()
      Returns the value of the value record component.
      Returns:
      the value of the value record component