Class JsonValue

  • All Implemented Interfaces:

    
    public class JsonValue
    extends JsonField<Nothing>
                        

    A class representing an arbitrary JSON value.

    It is immutable and assignable to any JsonField, regardless of its expected type (i.e. its generic type argument).

    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      final <R extends Any> R convert(TypeReference<R> type)
      final <R extends Any> R convert(Class<R> type)
      final <R extends Any> R accept(JsonValue.Visitor<R> visitor) Returns the result of calling the visitor method corresponding to this value's variant.
      final static JsonValue from(Object value) Converts the given value to a JsonValue.
      final static JsonValue fromJsonNode(JsonNode node) Returns a JsonValue converted from the given Jackson JsonNode.
      • Methods inherited from class com.openai.core.JsonField

        accept, asArray, asBoolean, asKnown, asNumber, asObject, asString, asStringOrThrow, asUnknown, isMissing, isNull
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

    • Method Detail

      • convert

         final <R extends Any> R convert(TypeReference<R> type)
      • from

         final static JsonValue from(Object value)

        Converts the given value to a JsonValue.

        This method works best on primitive types, List values, Map values, and nested combinations of these. For example:

        // Create primitive JSON values
        JsonValue nullValue = JsonValue.from(null);
        JsonValue booleanValue = JsonValue.from(true);
        JsonValue numberValue = JsonValue.from(42);
        JsonValue stringValue = JsonValue.from("Hello World!");
        
        // Create a JSON array value equivalent to `["Hello", "World"]`
        JsonValue arrayValue = JsonValue.from(List.of("Hello", "World"));
        
        // Create a JSON object value equivalent to `{ "a": 1, "b": 2 }`
        JsonValue objectValue = JsonValue.from(Map.of(
          "a", 1,
          "b", 2
        ));
        
        // Create an arbitrarily nested JSON equivalent to:
        // {
        //   "a": [1, 2],
        //   "b": [3, 4]
        // }
        JsonValue complexValue = JsonValue.from(Map.of(
          "a", List.of(1, 2),
          "b", List.of(3, 4)
        ));