public interface Json
Any object supported by the concrete JSON parser implementation can be serialized and deserialized.
A set of methods are specifically provided to deal with collections in a simple way, serializing and deserializing to
and from a JSON array. See for example toJsonArray(Class, Collection) or
fromJsonArray(JsonReader, Class).
The JsonWriter interface is used to provide the JSON serialization result, allowing to obtain the JSON data
in a number of ways, for example as a String, as a byte array or writing it into a provided writer.
At the opposite, the JsonReader interface is used to represent the JSON data source which has to be
deserialized into a Java object, and provides a number of static builder methods to obtain the JSON data from
different sources, for example a String, an array of bytes or an InputStream.
This API makes available a specific set of convenience methods to directly support the Holon platform
PropertyBox data class. A PropertyBox is serialized as a generic JSON object, using the property
names as the object attribute names. Any VirtualProperty is ignored by default. In the deserialization phase,
it is necessary to provide the Property set to use to create a PropertyBox instance form a JSON
object. For this reason, methods like fromJson(JsonReader, Iterable) allow to provide a Property set
for PropertyBox deserialization.
The JsonProvider interface can be used to provide a Json API implementation using the default Java service
extension feature. The default Json API implementation can be obtained using tge get() or require()
methods. See JsonProvider JavaDocs and methods documentation for further information.
The Holon platform JSON module provides two standard implementations of this API by default: one based on the Jackson library and one based on the Gson library. See the Holon platform documentation for details.
| Modifier and Type | Interface and Description |
|---|---|
static class |
Json.JsonException
Base Json exception class.
|
static class |
Json.JsonReadException
Exception thrown for JSON deserialization errors.
|
static class |
Json.JsonWriteException
Exception thrown for JSON serialization errors.
|
| Modifier and Type | Field and Description |
|---|---|
static String |
CONTEXT_KEY
Default
Context resource reference |
| Modifier and Type | Method and Description |
|---|---|
<T> T |
fromJson(JsonReader reader,
Class<T> type)
Deserializes the specified JSON source into an object of the specified type.
|
<P extends com.holonplatform.core.property.Property> |
fromJson(JsonReader reader,
Iterable<P> propertySet)
Deserializes the specified JSON data source into a
PropertyBox, using given propertySet as
PropertyBox property set. |
default com.holonplatform.core.property.PropertyBox |
fromJson(JsonReader reader,
com.holonplatform.core.property.Property... propertySet)
Deserializes the specified JSON data source into a
PropertyBox, using given propertySet as
PropertyBox property set. |
default <T> T |
fromJson(String json,
Class<T> type)
Deserializes the specified JSON string into an object of the specified type.
|
default <P extends com.holonplatform.core.property.Property> |
fromJson(String json,
Iterable<P> propertySet)
Deserializes the specified JSON string into a
PropertyBox, using given propertySet as
PropertyBox property set. |
default com.holonplatform.core.property.PropertyBox |
fromJson(String json,
com.holonplatform.core.property.Property... propertySet)
Deserializes the specified JSON string into a
PropertyBox, using given propertySet as
PropertyBox property set. |
<T> List<T> |
fromJsonArray(JsonReader reader,
Class<T> type)
Deserializes the specified JSON array data source into a
List of objects of the specified type. |
<P extends com.holonplatform.core.property.Property> |
fromJsonArray(JsonReader reader,
Iterable<P> propertySet)
Deserializes the specified JSON array data source into a list of
PropertyBox, using given
propertySet as PropertyBox property set. |
default List<com.holonplatform.core.property.PropertyBox> |
fromJsonArray(JsonReader reader,
com.holonplatform.core.property.Property... propertySet)
Deserializes the specified JSON array data source into a list of
PropertyBox, using given
propertySet as PropertyBox property set. |
default <T> List<T> |
fromJsonArray(String json,
Class<T> type)
Deserializes the specified JSON array string into a
List of objects of the specified type. |
default <P extends com.holonplatform.core.property.Property> |
fromJsonArray(String json,
Iterable<P> propertySet)
Deserializes the specified JSON array string into a list of
PropertyBox, using given
propertySet as PropertyBox property set. |
default List<com.holonplatform.core.property.PropertyBox> |
fromJsonArray(String json,
com.holonplatform.core.property.Property... propertySet)
Deserializes the specified JSON array string into a list of
PropertyBox, using given
propertySet as PropertyBox property set. |
static Optional<Json> |
get()
Try to obtain a
Json implementation, either from Context, if available using
CONTEXT_KEY, or relying on registered JsonProviders and using the one with higher priority. |
static Optional<Json> |
get(ClassLoader classLoader)
Try to obtain a
Json implementation using given Classloader, either from Context, if available
using CONTEXT_KEY, or relying on registered JsonProviders and using the one with higher
priority. |
static Json |
require()
Requires a
Json implementation, either from Context, if available using CONTEXT_KEY, or
relying on registered JsonProviders and using the one with higher priority. |
JsonWriter |
toJson(Object value)
Serialize given
value to JSON. |
<T> JsonWriter |
toJsonArray(Class<T> type,
Collection<T> values)
Serialize given collection of values as a JSON array.
|
default <T> JsonWriter |
toJsonArray(Class<T> type,
T... values)
Serialize given array of values as a JSON array.
|
default <T> String |
toJsonArrayString(Class<T> type,
Collection<T> values)
Serialize given collection of values as a JSON array string.
|
default <T> String |
toJsonArrayString(Class<T> type,
T... values)
Serialize given array of values as a JSON array string.
|
default String |
toJsonString(Object value)
Serialize given
value to a JSON string. |
static final String CONTEXT_KEY
Context resource referenceJsonWriter toJson(Object value)
value to JSON.value - Value to serializeJsonWriter from which to obtain the serialized JSON data.default String toJsonString(Object value)
value to a JSON string.value - Value to serializenull if given value was nullJson.JsonWriteException - If a JSON serialization error occured<T> JsonWriter toJsonArray(Class<T> type, Collection<T> values)
T - Values typetype - Value typevalues - Values collectionJsonWriter from which to obtain the serialized JSON data.default <T> JsonWriter toJsonArray(Class<T> type, T... values)
T - Values typetype - Value typevalues - Values to serializeJsonWriter from which to obtain the serialized JSON data.default <T> String toJsonArrayString(Class<T> type, Collection<T> values)
T - Values typetype - Value typevalues - Values to serializeJson.JsonWriteException - If a JSON serialization error occureddefault <T> String toJsonArrayString(Class<T> type, T... values)
T - Values typetype - Value typevalues - Values to serializeJson.JsonWriteException - If a JSON serialization error occured<T> T fromJson(JsonReader reader, Class<T> type)
T - desired object typereader - JSON data source (not null)type - the type of the desired object (not null)Json.JsonReadException - If a JSON deserialization error occureddefault <T> T fromJson(String json, Class<T> type)
T - desired object typejson - JSON stringtype - the type of the desired object (not null)Json.JsonReadException - If a JSON deserialization error occured<T> List<T> fromJsonArray(JsonReader reader, Class<T> type)
List of objects of the specified type.T - desired object typereader - JSON data source (not null)type - the type of the desired objects (not null)Json.JsonReadException - If a JSON deserialization error occureddefault <T> List<T> fromJsonArray(String json, Class<T> type)
List of objects of the specified type.T - desired object typejson - JSON array stringtype - the type of the desired objects (not null)Json.JsonReadException - If a JSON deserialization error occured<P extends com.holonplatform.core.property.Property> com.holonplatform.core.property.PropertyBox fromJson(JsonReader reader, Iterable<P> propertySet)
PropertyBox, using given propertySet as
PropertyBox property set.P - Actual property typereader - JSON data source (not null)propertySet - Property set to use to build the deserialized PropertyBox (not null)PropertyBoxJson.JsonReadException - If a JSON deserialization error occureddefault com.holonplatform.core.property.PropertyBox fromJson(JsonReader reader, com.holonplatform.core.property.Property... propertySet)
PropertyBox, using given propertySet as
PropertyBox property set.reader - JSON data source (not null)propertySet - Property set to use to build the deserialized PropertyBox (not null)PropertyBoxJson.JsonReadException - If a JSON deserialization error occureddefault <P extends com.holonplatform.core.property.Property> com.holonplatform.core.property.PropertyBox fromJson(String json, Iterable<P> propertySet)
PropertyBox, using given propertySet as
PropertyBox property set.P - Actual property typejson - JSON stringpropertySet - Property set to use to build the deserialized PropertyBox (not null)PropertyBoxJson.JsonReadException - If a JSON deserialization error occureddefault com.holonplatform.core.property.PropertyBox fromJson(String json, com.holonplatform.core.property.Property... propertySet)
PropertyBox, using given propertySet as
PropertyBox property set.json - JSON stringpropertySet - Property set to use to build the deserialized PropertyBox (not null)PropertyBoxJson.JsonReadException - If a JSON deserialization error occured<P extends com.holonplatform.core.property.Property> List<com.holonplatform.core.property.PropertyBox> fromJsonArray(JsonReader reader, Iterable<P> propertySet)
PropertyBox, using given
propertySet as PropertyBox property set.P - Actual property typereader - JSON data source (not null)propertySet - Property set to use to build the deserialized PropertyBoxs (not null)List of PropertyBoxJson.JsonReadException - If a JSON deserialization error occureddefault List<com.holonplatform.core.property.PropertyBox> fromJsonArray(JsonReader reader, com.holonplatform.core.property.Property... propertySet)
PropertyBox, using given
propertySet as PropertyBox property set.reader - JSON data source (not null)propertySet - Property set to use to build the deserialized PropertyBoxs (not null)List of PropertyBoxJson.JsonReadException - If a JSON deserialization error occureddefault <P extends com.holonplatform.core.property.Property> List<com.holonplatform.core.property.PropertyBox> fromJsonArray(String json, Iterable<P> propertySet)
PropertyBox, using given
propertySet as PropertyBox property set.P - Actual property typejson - JSON stringpropertySet - Property set to use to build the deserialized PropertyBoxs (not null)List of PropertyBoxJson.JsonReadException - If a JSON deserialization error occureddefault List<com.holonplatform.core.property.PropertyBox> fromJsonArray(String json, com.holonplatform.core.property.Property... propertySet)
PropertyBox, using given
propertySet as PropertyBox property set.json - JSON stringpropertySet - Property set to use to build the deserialized PropertyBoxs (not null)List of PropertyBoxJson.JsonReadException - If a JSON deserialization error occuredstatic Json require()
Json implementation, either from Context, if available using CONTEXT_KEY, or
relying on registered JsonProviders and using the one with higher priority.
If not available using get(), an IllegalStateException is thrown.
Json implementationIllegalStateException - If a Json implementation is not availablestatic Optional<Json> get()
Json implementation, either from Context, if available using
CONTEXT_KEY, or relying on registered JsonProviders and using the one with higher priority.Json implementation, if availablestatic Optional<Json> get(ClassLoader classLoader)
Json implementation using given Classloader, either from Context, if available
using CONTEXT_KEY, or relying on registered JsonProviders and using the one with higher
priority.classLoader - ClassLoader to useJson implementation, if availableCopyright © 2019 The Holon Platform. All rights reserved.