Class Json
java.lang.Object
de.iip_ecosphere.platform.support.json.Json
Json interface. Requires an implementing plugin of type
Json or an active JsonProviderDescriptor.
The default instances, e.g., for toJsonDflt(Object) does not necessarily consider annotations. For
annotations, please create a configured instance, e.g., via createInstance(Class)
or createInstance4All().- Author:
- Holger Eichelberger, SSE
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceSpecifies the mapping of an enumeration for serialization/deserialization. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract JsonconfigureExceptFieldsFilter(String filterId, String... fieldNames) Enables a filter for the given id (as declared byFilter) that excepts the given fields.abstract JsonconfigureFor(Class<?> cls) Configures this instance forclsby considering the annotations incls.abstract JsonconfigureLazy(Set<Object> ignore) Configures this instance for lazy serialization ignoring given classes and members.abstract <T> TconvertValue(Object value, Class<T> cls) Convenience method for doing two-step conversion from given value, into instance of given value type, by writing value into temporary buffer and reading from the buffer into specified target type.static JsonArrayBuilderCreates a JSON array builder.protected abstract JsonArrayBuilderCreates a JSON array builder.<T> Json.EnumMapping<T> createEnumMapping(Class<T> type) Creates a mapping specification, with no mapping, for incremental creation.abstract <T> Json.EnumMapping<T> createEnumMapping(Class<T> type, Map<String, T> mapping) Creates a mapping specification, with mapping.createEnumValueMap(Class<? extends T> enm) Creates a default value mapping for the given enum.<T extends Enum<T>>
Json.EnumMapping<T> createEnumValueMapping(Class<T> type) Creates a mapping specification based oncreateEnumValueMap(Class).static JsonGeneratorcreateGenerator(Writer writer) Creates a generator instance forwriter.protected abstract JsonGeneratorcreateGeneratorImpl(Writer writer) Creates a generator instance forwriter.static JsonCreates a configurable instance, no annotations are considered by default.static JsoncreateInstance(Class<?> cls) Creates a pre-configured configurable instance by applyingconfigureFor(Class)so that usual annotations are considered.static JsoncreateInstance(Class<?>... cls) Creates a pre-configured configurable instance by applyingconfigureFor(Class)so that usual annotations are considered.static JsonCreates a configurable instance, all annotations are considered by default.protected abstract JsoncreateInstanceImpl(boolean considerAnnotations) Creates the actual instance.abstract <T> IOIterator<T> createIterator(InputStream stream, Class<T> cls) Creates an iterator over the information instreamassuming a heterogeneous collection ofcls.static JsonObjectcreateObject(byte[] data) Creates a JSON object for individual access from the byte arraydata.static JsonObjectcreateObject(Reader reader) Creates a JSON object for individual access fromreader.static JsonObjectcreateObject(String string) Creates a JSON object for individual access fromstring.static JsonObjectBuilderCreates a JSON object builder.protected abstract JsonObjectBuilderCreates a JSON object builder.protected abstract JsonObjectcreateObjectImpl(Reader reader) Creates a JSON object for individual access fromreader.abstract JsondeclareEnums(Json.EnumMapping<?>... mappings) Declares enums and their mappings.abstract JsondefineFields(String... fieldNames) Configures this instance so that Java field names map exactly to the given names.abstract JsondefineOptionals(Class<?> cls, String... fieldNames) Configures this instance for the givenfieldNamesas optional during deserialization.abstract JsonexceptFields(String... fieldNames) Configures this instance so that it excludes thefieldNamesto be excluded.abstract JsonfailOnUnknownProperties(boolean fail) Configures this instance to fail/fail not on unknown fields/properties.abstract JsonfilterAllExceptFields(String... fieldNames) Configures this instance to filter to all given field names.abstract <R> RReads an Object from a JSON string.static <R> RfromJsonDflt(Object json, Class<R> cls) Reads an Object from a JSON string on the default instance.abstract JsonConfigures this instance for IIP conventions.abstract <R> List<R> listFromJson(Object json, Class<R> cls) Reads a typed List from a JSON string.static <R> List<R> listFromJsonDflt(Object json, Class<R> cls) Reads a typed List from a JSON string.abstract <K,V> Map <K, V> mapFromJson(Object json, Class<K> keyCls, Class<K> valueCls) Reads a typed Map from a JSON string.static <K,V> Map <K, V> mapFromJsonDflt(Object json, Class<K> keyCls, Class<K> valueCls) Reads a typed Map from a JSON string.static JsonIteratorparse(byte[] data) Parses data into an interatively parsable structure.static JsonIteratorParses text into an interatively parsable structure.protected abstract JsonIteratorparseImpl(byte[] data) Parses data into an interatively parsable structure.protected abstract JsonIteratorParses text into an interatively parsable structure.abstract <T> TReads a value from a byte array.abstract <T> TReads a value from a string.static <T> TreadValueDflt(byte[] src, Class<T> cls) Reads a value from a byte array using the default instance.static <T> TreadValueDflt(String src, Class<T> cls) Reads a value from a string using the default instance.static voidsetPrototype(Json json) Manually sets the instance.abstract StringTurns anobjectto JSON.static StringtoJsonDflt(Object obj) Turns anobjectto JSON on the default instance.toJsonQuiet(Object obj) Turns anobjectto JSON without throwing exceptions.abstract byte[]writeValueAsBytes(Object value) Writes a given values into a byte array representation.static byte[]writeValueAsBytesDflt(Object value) Writes a given values into a byte array representation.abstract StringwriteValueAsString(Object value) Writes a given values into a string representation.static StringwriteValueAsStringDflt(Object value) Writes a given values into a string representation.
-
Field Details
-
prototype
-
-
Constructor Details
-
Json
public Json()
-
-
Method Details
-
createInstance
Creates a configurable instance, no annotations are considered by default.- Returns:
- the Json instance
-
createInstance4All
Creates a configurable instance, all annotations are considered by default.- Returns:
- the Json instance
-
createInstance
Creates a pre-configured configurable instance by applyingconfigureFor(Class)so that usual annotations are considered.- Parameters:
cls- the class to configure for- Returns:
- the Json instance
-
createInstance
Creates a pre-configured configurable instance by applyingconfigureFor(Class)so that usual annotations are considered.- Parameters:
cls- the classes to configure for- Returns:
- the Json instance
-
setPrototype
Manually sets the instance. Shall not be needed, but may be required in some tests.- Parameters:
json- the Json instance
-
createInstanceImpl
Creates the actual instance.- Parameters:
considerAnnotations- whether annotations shall be considered automatically- Returns:
- the instance
-
toJson
Turns anobjectto JSON.- Parameters:
obj- the object (may be null), must have getters/setters for all attributes and a no-arg constructor no-arg constructor- Returns:
- the JSON string or an empty string in case of problems/no address
- Throws:
IOException- in case of serious issues- See Also:
-
toJsonQuiet
Turns anobjectto JSON without throwing exceptions.- Parameters:
obj- the object (may be null), must have getters/setters for all attributes and a no-arg constructor no-arg constructor- Returns:
- the JSON string or an empty string in case of problems/no address
-
fromJson
Reads an Object from a JSON string.- Type Parameters:
R- the object type, must have getters/setters for all attributes and a no-arg constructor- Parameters:
json- the JSON value (usually a String)cls- the class of the type to read- Returns:
- the object or null if reading fails
- Throws:
IOException- See Also:
-
listFromJson
Reads a typed List from a JSON string.- Type Parameters:
R- the entity type- Parameters:
json- the JSON value (usually a String)cls- the class of the entity type to read- Returns:
- the list or null if reading fails
- See Also:
-
mapFromJson
Reads a typed Map from a JSON string.- Type Parameters:
K- the key typeV- the value type- Parameters:
json- the JSON value (usually a String)keyCls- the class of the key type to readvalueCls- the class of the value type to read- Returns:
- the map or null if reading fails
- See Also:
-
readValue
Reads a value from a string.- Type Parameters:
T- the result type- Parameters:
src- the value as stringcls- the value type- Throws:
IOException- if reading fails
-
readValueDflt
Reads a value from a string using the default instance.- Type Parameters:
T- the result type- Parameters:
src- the value as stringcls- the value type- Throws:
IOException- if reading fails
-
readValue
Reads a value from a byte array.- Type Parameters:
T- the result type- Parameters:
src- the value as byte arraycls- the value type- Throws:
IOException- if reading fails
-
readValueDflt
Reads a value from a byte array using the default instance.- Type Parameters:
T- the result type- Parameters:
src- the value as byte arraycls- the value type- Throws:
IOException- if reading fails
-
writeValueAsBytes
Writes a given values into a byte array representation.- Parameters:
value- the value to write- Returns:
- the byte array representation
- Throws:
IOException- if writing fails
-
writeValueAsString
Writes a given values into a string representation.- Parameters:
value- the value to write- Returns:
- the string representation
- Throws:
IOException- if writing fails
-
convertValue
Convenience method for doing two-step conversion from given value, into instance of given value type, by writing value into temporary buffer and reading from the buffer into specified target type.- Parameters:
value- the value to convertcls- the target type- Throws:
IllegalArgumentException- If conversion fails due to incompatible type; if so, root cause will contain underlying checked exception data binding functionality threw
-
toJsonDflt
Turns anobjectto JSON on the default instance.- Parameters:
obj- the object (may be null), must have getters/setters for all attributes and a no-arg constructor no-arg constructor- Returns:
- the JSON string or an empty string in case of problems/no address
- Throws:
IOException- See Also:
-
fromJsonDflt
Reads an Object from a JSON string on the default instance.- Type Parameters:
R- the object type, must have getters/setters for all attributes and a no-arg constructor- Parameters:
json- the JSON value (usually a String)cls- the class of the type to read- Returns:
- the object or null if reading fails
- Throws:
IOException- See Also:
-
listFromJsonDflt
Reads a typed List from a JSON string.- Type Parameters:
R- the entity type- Parameters:
json- the JSON value (usually a String)cls- the class of the entity type to read- Returns:
- the list or null if reading fails
- See Also:
-
mapFromJsonDflt
Reads a typed Map from a JSON string.- Type Parameters:
K- the key typeV- the value type- Parameters:
json- the JSON value (usually a String)keyCls- the class of the key type to readvalueCls- the class of the value type to read- Returns:
- the map or null if reading fails
- See Also:
-
writeValueAsBytesDflt
Writes a given values into a byte array representation.- Parameters:
value- the value to write- Returns:
- the byte array representation
- Throws:
IOException- if writing fails
-
writeValueAsStringDflt
Writes a given values into a string representation.- Parameters:
value- the value to write- Returns:
- the string representation
- Throws:
IOException- if writing fails
-
configureFor
Configures this instance forclsby considering the annotations incls.- Parameters:
cls- the class- Returns:
- this for chaining
-
handleIipDataClasses
Configures this instance for IIP conventions.- Returns:
- this for chaining
-
defineOptionals
Configures this instance for the givenfieldNamesas optional during deserialization.- Parameters:
cls- the cls the classfieldNamesare member offieldNames- the field names (names of Java fields)- Returns:
- this for chaining
-
defineFields
Configures this instance so that Java field names map exactly to the given names.- Parameters:
fieldNames- the field names (names of JSON/Java fields)- Returns:
- this for chaining
-
exceptFields
Configures this instance so that it excludes thefieldNamesto be excluded.- Parameters:
fieldNames- the field names- Returns:
- this for chaining
-
configureExceptFieldsFilter
Enables a filter for the given id (as declared byFilter) that excepts the given fields. Combination of annotation and configured filter in case that excepts cannot be setup directly on classes or the class object is not available/accessible for some reason. Similar tofilterAllExceptFields(String...), which is applied to all classes to be serialized.- Parameters:
filterId- the filter idfieldNames- the fields to except- Returns:
- this for chaining
-
filterAllExceptFields
Configures this instance to filter to all given field names.- Parameters:
fieldNames- the field names that shall be excluded- Returns:
- this for chaining
-
failOnUnknownProperties
Configures this instance to fail/fail not on unknown fields/properties.- Parameters:
fail- to fail or not to fail- Returns:
- this for chaining
-
createEnumMapping
Creates a mapping specification, with no mapping, for incremental creation.- Type Parameters:
T- the (enum) type- Parameters:
type- the type to map
-
createEnumMapping
Creates a mapping specification, with mapping.- Type Parameters:
T- the (enum) type- Parameters:
type- the type to mapmapping- the mapping of values
-
createEnumValueMapping
Creates a mapping specification based oncreateEnumValueMap(Class).- Type Parameters:
T- the enum type- Parameters:
type- the enum to map
-
declareEnums
Declares enums and their mappings.- Parameters:
mappings- the mappings- Returns:
- this for chaining
-
createEnumValueMap
Creates a default value mapping for the given enum.- Type Parameters:
T- the enum type- Parameters:
enm- the enum to map/take values from- Returns:
- the name-value map for
enum
-
configureLazy
Configures this instance for lazy serialization ignoring given classes and members.- Parameters:
ignore- , classes (also as return types) and (reflection) fields that shall be ignored- Returns:
- this for chaining
-
createIterator
public abstract <T> IOIterator<T> createIterator(InputStream stream, Class<T> cls) throws IOException Creates an iterator over the information instreamassuming a heterogeneous collection ofcls.- Type Parameters:
T- the element type- Parameters:
stream- the input stream to readcls- the element type- Returns:
- an iterator over the element types
- Throws:
IOException- if accessing the stream fails
-
createObject
Creates a JSON object for individual access fromstring.- Parameters:
string- the string- Returns:
- the JSON object
- Throws:
IOException- if the object cannot be read/constructed- See Also:
-
createObject
Creates a JSON object for individual access from the byte arraydata.- Parameters:
data- the byte array- Returns:
- the JSON object
- Throws:
IOException- if the object cannot be read/constructed- See Also:
-
createObject
Creates a JSON object for individual access fromreader.- Parameters:
reader- the reader- Returns:
- the JSON object
- Throws:
IOException- if the object cannot be read/constructed
-
createObjectImpl
Creates a JSON object for individual access fromreader.- Parameters:
reader- the reader- Returns:
- the JSON object
- Throws:
IOException- if the object cannot be read/constructed
-
createObjectBuilder
Creates a JSON object builder.- Returns:
- the JSON object builder
-
createObjectBuilderImpl
Creates a JSON object builder.- Returns:
- the JSON object builder
-
createArrayBuilder
Creates a JSON array builder.- Returns:
- the JSON array builder
-
createArrayBuilderImpl
Creates a JSON array builder.- Returns:
- the JSON array builder
-
parseImpl
Parses text into an interatively parsable structure.- Parameters:
text- the JSON text to parse- Returns:
- the iterator, may indicate invalid data
-
parseImpl
Parses data into an interatively parsable structure.- Parameters:
data- the JSON data to parse- Returns:
- the iterator, may indicate invalid data
-
parse
Parses text into an interatively parsable structure.- Parameters:
text- the JSON text to parse- Returns:
- the iterator, may indicate invalid data
-
parse
Parses data into an interatively parsable structure.- Parameters:
data- the JSON data to parse- Returns:
- the iterator, may indicate invalid data
-
createGeneratorImpl
Creates a generator instance forwriter.- Parameters:
writer- the writer- Returns:
- the generator
- Throws:
IOException- if the generator cannot be created
-
createGenerator
Creates a generator instance forwriter.- Parameters:
writer- the writer- Returns:
- the generator
- Throws:
IOException- if the generator cannot be created
-