Package io.smallrye.openapi.runtime.io
Interface JsonIO<V,A extends V,O extends V,AB,OB>
- Type Parameters:
V- the type for a JSON valueA- the type for a JSON arrayO- the type of a JSON objectAB- the type used to build a JSON arrayOB- the type used to build a JSON object
public interface JsonIO<V,A extends V,O extends V,AB,OB>
Abstraction layer around a library for reading and writing JSON. (E.g. Jakarta JSON-P or Jackson).
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionvoidAdd a JSON value to a JSON array builder.Cast a JSON value to a JSON arrayConvert a JSON boolean to aBoolean.Cast a JSON value to a JSON objectConvert a JSON string, number or boolean to aString.buildArray(AB array) Convert a JSON array builder into a JSON array.buildObject(OB object) Convert a JSON object builder into a JSON object.Create a JSON array builder which can be used to build a JSON array.Create a JSON object builder which can be used to build a JSON object.Get the list of JSON values contained by a JSON arrayConvert a JSON value into a Java object.<T> TConvert a basic JSON value into a Java object of the desired type.default VfromReader(Reader reader) Read a YAML document from aReader.fromReader(Reader reader, Format format) Read a JSON or YAML document from aReader.default VfromStream(InputStream stream, Format format) Read a JSON or YAML document from anInputStream.default VfromString(String value, Format format) Read a JSON or YAML document from aStringGet an JSON array property from a JSON objectGet an JSON array property from a JSON object and convert it into a list of Java objectsdefault BigDecimalgetBigDecimal(V object, String key) Get an number property from a JSON objectdefault BooleangetBoolean(V object, String key) Get an boolean property from a JSON objectdefault IntegerGet an integer property from a JSON objectgetJsonBigDecimal(O object, String key) Get an number property from a JSON objectgetJsonBoolean(O object, String key) Get an boolean property from a JSON objectgetJsonInt(O object, String key) Get an integer property from a JSON objectgetJsonString(O object, String key) Get a property from a JSON object as a string.Get an JSON object property from a JSON objectdefault StringGet a property from a JSON object as a string.Get a property from a JSON objectbooleanCheck whether a JSON object contains a particular keybooleanCheck whether a JSON value is an arraybooleanCheck whether a JSON value is a JSON boolean.booleanCheck whether a JSON value is a JSON null.booleanCheck whether a JSON value is a JSON objectbooleanCheck whether a JSON value is a JSON string.static <V,A extends V, O extends V, AB, OB>
JsonIO<V, A, O, AB, OB> newInstance(OpenApiConfig config) Returns the JSON value representingnull.default ObjectparseValue(String value) properties(O object) Get all the properties (keys and values) of a JSON object.voidSet a property on a JSON object buildervoidCopy all properties from a JSON object to a JSON object builder.Convert a Java object to JSON.toJson(Object object, JsonIO.PropertyMapper<V, OB> handler) Convert a Java object to JSON.Serialize a JSON value to a JSON or YAML string
-
Field Details
-
NOOP
-
-
Method Details
-
newInstance
-
parseValue
-
isArray
Check whether a JSON value is an array- Parameters:
value- the JSON value to check- Returns:
trueifvalueis a JSON array, otherwisefalse
-
asArray
Cast a JSON value to a JSON array- Parameters:
value- the JSON value, which must represent a JSON array- Returns:
- the JSON array
- Throws:
ClassCastException- ifvalueis not a JSON array
-
entries
Get the list of JSON values contained by a JSON array- Parameters:
array- the JSON array- Returns:
- the list of JSON values in the array
-
isObject
Check whether a JSON value is a JSON object- Parameters:
value- the JSON value to check- Returns:
trueifvalueis a JSON object, otherwisefalse
-
asObject
Cast a JSON value to a JSON object- Parameters:
value- the JSON value, which must represent a JSON object- Returns:
- the JSON object
- Throws:
ClassCastException- ifvalueis not a JSON object
-
hasKey
Check whether a JSON object contains a particular key- Parameters:
object- the JSON object to checkkey- the key to look for- Returns:
trueifobjectcontains keykey, otherwisefalse
-
properties
Get all the properties (keys and values) of a JSON object.This is a similar operation to
Map.entrySet().- Parameters:
object- the JSON object- Returns:
- the set of key-value pairs
-
isString
Check whether a JSON value is a JSON string.- Parameters:
value- the JSON value to check- Returns:
trueifvalueis a string, otherwisefalse
-
asString
Convert a JSON string, number or boolean to aString.- Parameters:
value- the JSON value, must be a string- Returns:
- the String value, or
nullif the JSON value does not represent a string, number or boolean
-
isBoolean
Check whether a JSON value is a JSON boolean.- Parameters:
value- the JSON value to check- Returns:
trueifvalueis a boolean, otherwisefalse
-
asBoolean
Convert a JSON boolean to aBoolean.- Parameters:
value- the JSON value, must be a boolean- Returns:
- the Boolean value, or
nullif the JSON value does not represent a boolean
-
isNull
Check whether a JSON value is a JSON null.- Parameters:
value- the JSON value to check- Returns:
trueifvalueis null, otherwisefalse
-
getString
Get a property from a JSON object as a string. The same conversions are performed asasString(Object).- Parameters:
object- the JSON objectkey- the property key- Returns:
- the property value, or
nullifobjectis not a JSON object, the property is not present, or the property value is not a string, number or boolean
-
getInt
Get an integer property from a JSON object- Parameters:
object- the JSON objectkey- the property key- Returns:
- the property value, or
nullifobjectis not a JSON object, the property is not present, or the property value is not an integer
-
getBoolean
Get an boolean property from a JSON object- Parameters:
object- the JSON objectkey- the property key- Returns:
- the property value, or
nullifobjectis not a JSON object, the property is not present, or the property value is not a boolean
-
getBigDecimal
Get an number property from a JSON object- Parameters:
object- the JSON objectkey- the property key- Returns:
- the property value, or
nullifobjectis not a JSON object, the property is not present, or the property value is not a number
-
getJsonInt
Get an integer property from a JSON object- Parameters:
object- the JSON objectkey- the property key- Returns:
- the property value, or
nullif the property is not present or is not an integer
-
getJsonString
Get a property from a JSON object as a string. The same conversions are performed asasString(Object).- Parameters:
object- the JSON objectkey- the property key- Returns:
- the property value, or
nullif the property is not present or is not a string, number or boolean
-
getJsonBoolean
Get an boolean property from a JSON object- Parameters:
object- the JSON objectkey- the property key- Returns:
- the property value, or
nullif the property is not present or is not a boolean
-
getJsonBigDecimal
Get an number property from a JSON object- Parameters:
object- the JSON objectkey- the property key- Returns:
- the property value, or
nullif the property is not present or is not a number
-
getValue
Get a property from a JSON object- Parameters:
object- the JSON objectkey- the property key- Returns:
- the property value, or
nullif the property is not present
-
getArray
Get an JSON array property from a JSON object- Parameters:
object- the JSON objectkey- the property key- Returns:
- an
Optionalcontaining the property value, or an emptyOptionalif the property is not present, or the property is not an array
-
getArray
Get an JSON array property from a JSON object and convert it into a list of Java objects- Type Parameters:
T- the type that each value in the JSON array should be converted into- Parameters:
object- the JSON objectkey- the property keyvalueMapper- a function to convert a value within the JSON array into a Java object- Returns:
- an
Optionalcontaining the list of Java objects, or an emptyOptionalif the property is not present, or the property is not an array
-
getObject
Get an JSON object property from a JSON object- Parameters:
object- the JSON objectkey- the property key- Returns:
- an
Optionalcontaining the property value, or an emptyOptionalif the property is not present, or the property is not an object
-
toJson
Convert a Java object to JSON. SeetoJson(Object, Object, PropertyMapper)for the list of supported types- Parameters:
object- the JSON object- Returns:
- an
Optionalcontaining the JSON value, or an emptyOptionalifobjectis not one of the supported types
-
toJson
-
fromJson
Convert a JSON value into a Java object.- Parameters:
object- the JSON value- Returns:
- the Java object, which may be
nullifobjectisnullor represents a JSON null value
-
fromJson
Convert a basic JSON value into a Java object of the desired type. This method cannot be used to convert JSON arrays or objects.The supported values for
desiredTypeare:StringIntegerBigIntegerLongBigDecimalBoolean
- Type Parameters:
T- the desired Java type- Parameters:
object- the JSON objectdesiredType- the desired Java type- Returns:
- the JSON object, or
nullifobjectcannot be converted to the desired type
-
toJson
Convert a Java object to JSON.The following types are supported:
V(the JSON value type)StringBigDecimalBigIntegerBooleanDoubleFloatShortIntegerLongCharacterEnumListwhere each item is a supported typeMapwhere each key is aStringand each value is a supported type
- Parameters:
object- the JSON objectdefaultValue- the default value to return ifvaluecannot be converted to JSONpropertyMapper- mapper object to alter the default mapping of the object and its properties to JSON- Returns:
- the JSON value, or
defaultValueifvaluecannot be converted to JSON
-
fromString
Read a JSON or YAML document from aString- Parameters:
value- the JSON or YAML documentformat- the format- Returns:
- the root JSON value from the document
-
fromStream
Read a JSON or YAML document from anInputStream. The stream is read using thedefault charset.- Parameters:
stream- the input stream to read the JSON or YAML document fromformat- the format- Returns:
- the root JSON value from the document
-
fromReader
Read a YAML document from aReader.- Parameters:
reader- the reader to read the YAML document from- Returns:
- the root JSON value from the document
- Throws:
IOException
-
fromReader
Read a JSON or YAML document from aReader.- Parameters:
reader- the reader to read the JSON or YAML document fromformat- the format- Returns:
- the root JSON value from the document
- Throws:
IOException
-
toString
Serialize a JSON value to a JSON or YAML string- Parameters:
object- the JSON valueformat- the desired format- Returns:
- the serialization of
object
-
createArray
AB createArray()Create a JSON array builder which can be used to build a JSON array. Example:AB builder = jsonIO().createArray(); jsonIO().add(builder, value1); jsonIO().add(builder, value2); A array = jsonIO().buildArray(builder);- Returns:
- the JSON array builder
-
add
Add a JSON value to a JSON array builder.- Parameters:
array- the array buildervalue- the value to add- See Also:
-
buildArray
Convert a JSON array builder into a JSON array.- Parameters:
array- the JSON array builder- Returns:
- the JSON array
- See Also:
-
createObject
OB createObject()Create a JSON object builder which can be used to build a JSON object. Example:OB builder = jsonIO().createObject(); jsonIO().set(builder, "key1", value1); jsonIO().set(builder, "key2", value2); O object = jsonIO().buildObject(builder);- Returns:
- the JSON object builder
-
set
Set a property on a JSON object builder- Parameters:
object- the JSON object builderkey- the property keyvalue- the property value- See Also:
-
setAll
Copy all properties from a JSON object to a JSON object builder.- Parameters:
object- the JSON object buildervalueSource- the JSON object- See Also:
-
buildObject
Convert a JSON object builder into a JSON object.- Parameters:
object- the JSON object builder- Returns:
- the JSON object
- See Also:
-
nullValue
V nullValue()Returns the JSON value representingnull.
-