Record Class JsonContent
- Record Components:
value- the wrapped value; aString,Map,List, any POJO Jackson serializes to a string, object or array, ornull
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
FieldsModifier and TypeFieldDescriptionstatic final JsonContentShared instance representing an explicit JSONnull. -
Constructor Summary
ConstructorsConstructorDescriptionJsonContent(@Nullable Object value) Creates an instance of aJsonContentrecord class. -
Method Summary
Modifier and TypeMethodDescription<T> @Nullable TConverts the wrapped value into the given type using Jackson — the JSON view, as opposed to theinstanceofchecks the other accessors perform.asList()Reads the wrapped value as a list.asMap()Reads the wrapped value as a map.@Nullable StringasText()Reads the wrapped value as text.final booleanIndicates whether some other object is "equal to" this one.final inthashCode()Returns a hash code value for this object.booleanisNull()static JsonContentA JSON object whose keys keep the order they are written in:JsonContent.object("question", "...", "inspect", "message").static JsonContentWraps 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 StringtoString()Returns a string representation of this record class.@Nullable Objectvalue()Returns the value of thevaluerecord component.
-
Field Details
-
NULL
Shared instance representing an explicit JSONnull.
-
-
Constructor Details
-
JsonContent
Creates an instance of aJsonContentrecord class.- Parameters:
value- the value for thevaluerecord component
-
-
Method Details
-
of
Wraps an arbitrary value. Also the Jackson entry point when reading aJsonContentback from a response.- Parameters:
value- the value to wrap; may benull- Returns:
- the wrapped value, never
null
-
object
A JSON object whose keys keep the order they are written in:JsonContent.object("question", "...", "inspect", "message").Use it instead of
Map.offor 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.ofiterates 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-emptyString, each value anything aJsonContentcan wrap, ornullfor a JSONnull- 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:
truewhen this instance wraps a JSONnull
-
asText
Reads the wrapped value as text. This is the Java view: a POJO that serializes to a JSON string still answersnullhere, because the value is not aString. Useas(Class, JsonMapper)for the Jackson view.- Returns:
- the wrapped value as text, or
nullwhen it is not aString
-
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 aMap. Note also that an empty result does not distinguish "not an object" from "an empty object" — useas(Class, JsonMapper)when that matters.- Returns:
- the wrapped value as a map, or an empty map when it is not a
Map
-
asList
Reads the wrapped value as a list. This is the Java view, with the same caveats asasMap().- Returns:
- the wrapped value as a list, or an empty list when it is not a
List
-
as
Converts the wrapped value into the given type using Jackson — the JSON view, as opposed to theinstanceofchecks 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 aMapthrows rather than yielding its fields.- Type Parameters:
T- the target type- Parameters:
type- the target typejsonMapper- the mapper used for the conversion- Returns:
- the converted value, or
nullwhen this instance wraps a JSONnull
-
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
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. -
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. -
equals
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 withObjects::equals(Object,Object). -
value
Returns the value of thevaluerecord component.- Returns:
- the value of the
valuerecord component
-