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.- 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 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.static JsonCreates a configurable instance.protected abstract JsonCreates 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(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.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 voidsetPrototype(Json json) Manually sets the instance.abstract StringTurns anobjectto JSON.static StringtoJsonDflt(Object obj) Turns anobjectto JSON on the default instance.abstract byte[]writeValueAsBytes(Object value) Writes a given values into a byte array representation.
-
Field Details
-
prototype
-
-
Constructor Details
-
Json
public Json()
-
-
Method Details
-
createInstance
Creates a configurable instance.- 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.- 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- See Also:
-
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:
-
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
-
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
-
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
-
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:
-
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
-
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.- Parameters:
type- the type to map
-
createEnumMapping
Creates a mapping specification, with mapping.- Parameters:
type- the type to mapmapping- the mapping of values
-
declareEnums
Declares enums and their mappings.- Parameters:
mappings- the mappings- Returns:
- this for chaining
-
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 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
-