Package com.composum.sling.core.util
Class JsonUtil
- java.lang.Object
- 
- com.composum.sling.core.util.JsonUtil
 
- 
 public class JsonUtil extends Object The utility class to transform JCR object and Sling resources into JSON and update such objects using JSON data.
- 
- 
Nested Class SummaryNested Classes Modifier and Type Class Description static interfaceJsonUtil.ElementCallback<T>static classJsonUtil.JsonPropertythe structure for parsing property values from JSON using Gson
 - 
Field SummaryFields Modifier and Type Field Description protected static com.google.gson.GsonGSONstatic com.google.gson.GsonBuilderGSON_BUILDERthe declared builder for JSON POJO mapping
 - 
Constructor SummaryConstructors Constructor Description JsonUtil()
 - 
Method SummaryAll Methods Static Methods Concrete Methods Modifier and Type Method Description static org.apache.sling.api.resource.ResourcecreateResource(org.apache.sling.api.resource.ResourceResolver resolver, String path, Map<String,JsonUtil.JsonProperty> propertiesSet, javax.jcr.ValueFactory factory, MappingRules mapping)Creates the resource parsed from JSON.static voidexportChildOrderProperty(com.google.gson.stream.JsonWriter writer, org.apache.sling.api.resource.Resource resource)Writes the names of all children (except the 'jcr:content' child) into one array value named '_child_order_' as a hint to recalculate the original order of the children.static voidexportJson(@NotNull com.google.gson.stream.JsonWriter writer, @NotNull org.apache.sling.api.resource.Resource resource)Writes a resources JSON view to a writer using the default application rules for filtering.static voidexportJson(@NotNull com.google.gson.stream.JsonWriter writer, @NotNull org.apache.sling.api.resource.Resource resource, MappingRules mapping)static voidexportJson(com.google.gson.stream.JsonWriter writer, org.apache.sling.api.resource.Resource resource, MappingRules mapping, int depth)static voidexportProperties(@NotNull com.google.gson.stream.JsonWriter writer, @NotNull org.apache.sling.api.resource.Resource resource, MappingRules mapping)static StringgetValueString(Object value, int type, MappingRules mapping)Embeds the property type in the string value if the formats scope is 'value'.static StringgetValueTarget(@Nullable org.apache.sling.api.resource.Resource resource, @Nullable String name, @Nullable String value)static org.apache.sling.api.resource.ResourceimportJson(com.google.gson.stream.JsonReader reader, org.apache.sling.api.resource.ResourceResolver resolver, String path)static org.apache.sling.api.resource.ResourceimportJson(com.google.gson.stream.JsonReader reader, org.apache.sling.api.resource.ResourceResolver resolver, String path, MappingRules mapping)static booleanimportJsonProperty(javax.jcr.ValueFactory factory, org.apache.sling.api.resource.Resource resource, JsonUtil.JsonProperty property, MappingRules mapping)Changes the property specified by the JSON POJO of the given node if the change rule is appropriate to the current state of the node (changes are made only if not only 'extend' is specified as rule or if the property is new) and if the property filter of the rules accepts the property.static voidjsonMap(@NotNull com.google.gson.stream.JsonWriter writer, @Nullable Map<String,?> map)Transforms a Map object into a JSON object (stream).static Map<String,Object>jsonMap(com.google.gson.stream.JsonReader reader)Transforms a JSON object (stream) into a Map object.static voidjsonMapEntries(@NotNull com.google.gson.stream.JsonWriter writer, @Nullable Map<String,?> map)static voidjsonValue(@NotNull com.google.gson.stream.JsonWriter writer, @Nullable Object value)Transforms an object into a JSON object (stream).static ObjectjsonValue(com.google.gson.stream.JsonReader reader)Transforms a JSON object (stream) into an object.static javax.jcr.ValuemakeJcrValue(javax.jcr.Node node, int type, Object object, MappingRules mapping)Create a JCR value from string value for the designated JCR type.static ObjectmakeValueObject(int type, Object value)Create a JCR value from string value for the designated JCR type.static JsonUtil.JsonPropertyparseJsonProperty(com.google.gson.stream.JsonReader reader, String name)Parses a single property of the first element of an array and returns the result as a JSON POJO object.static StringparseJsonString(com.google.gson.stream.JsonReader reader, JsonUtil.JsonProperty property)The parser for a JSON string value with an optional type hint ('{type}...') as the values prefix.static booleansetJsonProperty(javax.jcr.Node node, JsonUtil.JsonProperty property, MappingRules mapping)Creates or updates one property at a JCR node.static voidwriteJsonArray(com.google.gson.stream.JsonWriter writer, String[] values)static <T> voidwriteJsonArray(com.google.gson.stream.JsonWriter writer, String name, Iterator<T> values, JsonUtil.ElementCallback<T> elementCallback)static voidwriteJsonArray(com.google.gson.stream.JsonWriter writer, Iterator<String> values)static <T> voidwriteJsonArray(com.google.gson.stream.JsonWriter writer, Iterator<T> values, JsonUtil.ElementCallback<T> elementCallback)static voidwriteJsonProperties(@NotNull org.apache.sling.api.resource.Resource resource, @NotNull com.google.gson.stream.JsonWriter writer, @NotNull StringFilter filter, @Nullable javax.jcr.Node node, @NotNull MappingRules mapping)Write all properties of an node accepted by the filter into an JSON array.static voidwriteJsonProperty(@NotNull org.apache.sling.api.resource.Resource resource, @NotNull com.google.gson.stream.JsonWriter writer, @Nullable String name, @Nullable Object value, @NotNull MappingRules mapping)Writes a resource property (without node - probably synthetic resource)static voidwriteJsonProperty(@NotNull org.apache.sling.api.resource.Resource resource, @NotNull com.google.gson.stream.JsonWriter writer, @Nullable javax.jcr.Property property, @NotNull MappingRules mapping)Writes a JCR property as an JSON object: { name: ..., value: ..., type: ..., multi: ...}.static voidwriteJsonValue(@NotNull org.apache.sling.api.resource.Resource resource, @NotNull com.google.gson.stream.JsonWriter writer, @NotNull String name, @NotNull Object value, @NotNull Integer type, @NotNull MappingRules mapping)Write a JCR value to the JSON writer.static voidwriteJsonValueMap(@NotNull org.apache.sling.api.resource.Resource resource, @NotNull com.google.gson.stream.JsonWriter writer, @NotNull StringFilter filter, @Nullable org.apache.sling.api.resource.ValueMap values, @NotNull MappingRules mapping)Write all properties of an node accepted by the filter into an JSON array.static voidwriteValue(com.google.gson.stream.JsonWriter writer, String name, boolean value)static voidwriteValue(com.google.gson.stream.JsonWriter writer, String name, double value)static voidwriteValue(com.google.gson.stream.JsonWriter writer, String name, long value)static voidwriteValue(com.google.gson.stream.JsonWriter writer, String name, Number value)static voidwriteValue(com.google.gson.stream.JsonWriter writer, String name, String value)
 
- 
- 
- 
Method Detail- 
jsonMappublic static Map<String,Object> jsonMap(com.google.gson.stream.JsonReader reader) throws IOException Transforms a JSON object (stream) into a Map object.- Throws:
- IOException
 
 - 
jsonValuepublic static Object jsonValue(com.google.gson.stream.JsonReader reader) throws IOException Transforms a JSON object (stream) into an object.- Throws:
- IOException
 
 - 
jsonMappublic static void jsonMap(@NotNull @NotNull com.google.gson.stream.JsonWriter writer, @Nullable @Nullable Map<String,?> map) throws IOExceptionTransforms a Map object into a JSON object (stream).- Throws:
- IOException
 
 - 
jsonMapEntriespublic static void jsonMapEntries(@NotNull @NotNull com.google.gson.stream.JsonWriter writer, @Nullable @Nullable Map<String,?> map) throws IOException- Throws:
- IOException
 
 - 
jsonValuepublic static void jsonValue(@NotNull @NotNull com.google.gson.stream.JsonWriter writer, @Nullable @Nullable Object value) throws IOExceptionTransforms an object into a JSON object (stream).- Throws:
- IOException
 
 - 
exportJsonpublic static void exportJson(@NotNull @NotNull com.google.gson.stream.JsonWriter writer, @NotNull @NotNull org.apache.sling.api.resource.Resource resource) throws javax.jcr.RepositoryException, IOExceptionWrites a resources JSON view to a writer using the default application rules for filtering.- Parameters:
- writer- the writer for the JSON transformation
- resource- the resource to transform
- Throws:
- javax.jcr.RepositoryException
- IOException
 
 - 
exportJsonpublic static void exportJson(@NotNull @NotNull com.google.gson.stream.JsonWriter writer, @NotNull @NotNull org.apache.sling.api.resource.Resource resource, MappingRules mapping) throws javax.jcr.RepositoryException, IOException- Parameters:
- writer- the writer for the JSON transformation
- resource- the resource to transform
- mapping- the mapping policy rule set
- Throws:
- javax.jcr.RepositoryException
- IOException
 
 - 
exportJsonpublic static void exportJson(com.google.gson.stream.JsonWriter writer, org.apache.sling.api.resource.Resource resource, MappingRules mapping, int depth) throws javax.jcr.RepositoryException, IOException- Parameters:
- writer- the writer for the JSON transformation
- resource- the resource to transform
- mapping- the mapping policy rule set
- depth- the max depth for the rendering
- Throws:
- javax.jcr.RepositoryException
- IOException
 
 - 
exportPropertiespublic static void exportProperties(@NotNull @NotNull com.google.gson.stream.JsonWriter writer, @NotNull @NotNull org.apache.sling.api.resource.Resource resource, MappingRules mapping) throws javax.jcr.RepositoryException, IOException- Parameters:
- writer- the writer for the JSON transformation
- resource- the resource to transform
- mapping- the mapping policy rule set
- Throws:
- javax.jcr.RepositoryException
- IOException
 
 - 
exportChildOrderPropertypublic static void exportChildOrderProperty(com.google.gson.stream.JsonWriter writer, org.apache.sling.api.resource.Resource resource) throws IOExceptionWrites the names of all children (except the 'jcr:content' child) into one array value named '_child_order_' as a hint to recalculate the original order of the children.- Throws:
- IOException
 
 - 
importJsonpublic static org.apache.sling.api.resource.Resource importJson(com.google.gson.stream.JsonReader reader, org.apache.sling.api.resource.ResourceResolver resolver, String path) throws javax.jcr.RepositoryException, IOException- Throws:
- javax.jcr.RepositoryException
- IOException
 
 - 
importJsonpublic static org.apache.sling.api.resource.Resource importJson(com.google.gson.stream.JsonReader reader, org.apache.sling.api.resource.ResourceResolver resolver, String path, MappingRules mapping) throws javax.jcr.RepositoryException, IOException- Throws:
- javax.jcr.RepositoryException
- IOException
 
 - 
createResourcepublic static org.apache.sling.api.resource.Resource createResource(org.apache.sling.api.resource.ResourceResolver resolver, String path, Map<String,JsonUtil.JsonProperty> propertiesSet, javax.jcr.ValueFactory factory, MappingRules mapping) throws javax.jcr.RepositoryExceptionCreates the resource parsed from JSON. Uses the 'jcr:primaryType' property from the propertySet to determine the right primary type for the new node. Uses the 'jcr:mixinTypes' property to set up the mixin types of the node before the other properties are set (it's important that all main type settings are done before the properties are set to avoid constraint violations).- Throws:
- javax.jcr.RepositoryException
 
 - 
parseJsonPropertypublic static JsonUtil.JsonProperty parseJsonProperty(com.google.gson.stream.JsonReader reader, String name) throws IOException Parses a single property of the first element of an array and returns the result as a JSON POJO object.- Parameters:
- reader- the reader with the JSON stream
- name- the already parsed name of the property
- Returns:
- a new JsonProperty object with the name, type, and value set
- Throws:
- IOException
 
 - 
parseJsonStringpublic static String parseJsonString(com.google.gson.stream.JsonReader reader, JsonUtil.JsonProperty property) throws IOException The parser for a JSON string value with an optional type hint ('{type}...') as the values prefix. Sets the value and type of the property POJO and returns the string value without the type. This is used for single property values and also for multiple values (in the array loop).- Throws:
- IOException
 
 - 
importJsonPropertypublic static boolean importJsonProperty(javax.jcr.ValueFactory factory, org.apache.sling.api.resource.Resource resource, JsonUtil.JsonProperty property, MappingRules mapping) throws javax.jcr.RepositoryExceptionChanges the property specified by the JSON POJO of the given node if the change rule is appropriate to the current state of the node (changes are made only if not only 'extend' is specified as rule or if the property is new) and if the property filter of the rules accepts the property. ConstraintViolationExceptions are catched if thrown and logged with 'info' level.- Parameters:
- factory- the ValueFactory to create the JCR value from the JSON value
- resource- the resource which stores the property
- property- the JSON POJO for the property
- mapping- the change rules for merging with existing values
- Throws:
- javax.jcr.RepositoryException
 
 - 
writeJsonPropertiespublic static void writeJsonProperties(@NotNull @NotNull org.apache.sling.api.resource.Resource resource, @NotNull @NotNull com.google.gson.stream.JsonWriter writer, @NotNull @NotNull StringFilter filter, @Nullable @Nullable javax.jcr.Node node, @NotNull @NotNull MappingRules mapping) throws javax.jcr.RepositoryException, IOExceptionWrite all properties of an node accepted by the filter into an JSON array.- Parameters:
- writer- the JSON writer object (with the JSON state)
- filter- the property name filter
- node- the JCR node to write
- Throws:
- javax.jcr.RepositoryException- error on accessing JCR
- IOException- error on write JSON
 
 - 
writeJsonValueMappublic static void writeJsonValueMap(@NotNull @NotNull org.apache.sling.api.resource.Resource resource, @NotNull @NotNull com.google.gson.stream.JsonWriter writer, @NotNull @NotNull StringFilter filter, @Nullable @Nullable org.apache.sling.api.resource.ValueMap values, @NotNull @NotNull MappingRules mapping) throws javax.jcr.RepositoryException, IOExceptionWrite all properties of an node accepted by the filter into an JSON array.- Parameters:
- writer- the JSON writer object (with the JSON state)
- filter- the property name filter
- values- the Sling ValueMap to write
- Throws:
- javax.jcr.RepositoryException- error on accessing JCR
- IOException- error on write JSON
 
 - 
writeJsonPropertypublic static void writeJsonProperty(@NotNull @NotNull org.apache.sling.api.resource.Resource resource, @NotNull @NotNull com.google.gson.stream.JsonWriter writer, @Nullable @Nullable javax.jcr.Property property, @NotNull @NotNull MappingRules mapping) throws javax.jcr.RepositoryException, IOExceptionWrites a JCR property as an JSON object: { name: ..., value: ..., type: ..., multi: ...}.- Parameters:
- writer- the JSON writer object (with the JSON state)
- property- the JCR property to write
- mapping- the format in the JSON output
- Throws:
- javax.jcr.RepositoryException- error on accessing JCR
- IOException- error on write JSON
 
 - 
writeJsonPropertypublic static void writeJsonProperty(@NotNull @NotNull org.apache.sling.api.resource.Resource resource, @NotNull @NotNull com.google.gson.stream.JsonWriter writer, @Nullable @Nullable String name, @Nullable @Nullable Object value, @NotNull @NotNull MappingRules mapping) throws javax.jcr.RepositoryException, IOExceptionWrites a resource property (without node - probably synthetic resource)- Throws:
- javax.jcr.RepositoryException
- IOException
 
 - 
getValueTargetpublic static String getValueTarget(@Nullable @Nullable org.apache.sling.api.resource.Resource resource, @Nullable @Nullable String name, @Nullable @Nullable String value) 
 - 
writeJsonValuepublic static void writeJsonValue(@NotNull @NotNull org.apache.sling.api.resource.Resource resource, @NotNull @NotNull com.google.gson.stream.JsonWriter writer, @NotNull @NotNull String name, @NotNull @NotNull Object value, @NotNull @NotNull Integer type, @NotNull @NotNull MappingRules mapping) throws javax.jcr.RepositoryException, IOExceptionWrite a JCR value to the JSON writer.- Parameters:
- writer- the JSON writer object (with the JSON state)
- name- the name of the value
- value- the value itself, should be a JCR property otherwise a java scalar object or array of scalars
- type- the type of the value
- Throws:
- javax.jcr.RepositoryException- error on accessing JCR
- IOException- error on write JSON
 
 - 
writeValuepublic static void writeValue(com.google.gson.stream.JsonWriter writer, String name, String value) throws IOException- Throws:
- IOException
 
 - 
writeValuepublic static void writeValue(com.google.gson.stream.JsonWriter writer, String name, boolean value) throws IOException- Throws:
- IOException
 
 - 
writeValuepublic static void writeValue(com.google.gson.stream.JsonWriter writer, String name, double value) throws IOException- Throws:
- IOException
 
 - 
writeValuepublic static void writeValue(com.google.gson.stream.JsonWriter writer, String name, long value) throws IOException- Throws:
- IOException
 
 - 
writeValuepublic static void writeValue(com.google.gson.stream.JsonWriter writer, String name, Number value) throws IOException- Throws:
- IOException
 
 - 
getValueStringpublic static String getValueString(Object value, int type, MappingRules mapping) Embeds the property type in the string value if the formats scope is 'value'.
 - 
writeJsonArraypublic static void writeJsonArray(com.google.gson.stream.JsonWriter writer, String[] values) throws IOException- Throws:
- IOException
 
 - 
writeJsonArraypublic static void writeJsonArray(com.google.gson.stream.JsonWriter writer, Iterator<String> values) throws IOException- Throws:
- IOException
 
 - 
writeJsonArraypublic static <T> void writeJsonArray(com.google.gson.stream.JsonWriter writer, Iterator<T> values, JsonUtil.ElementCallback<T> elementCallback) throws IOException- Throws:
- IOException
 
 - 
writeJsonArraypublic static <T> void writeJsonArray(com.google.gson.stream.JsonWriter writer, String name, Iterator<T> values, JsonUtil.ElementCallback<T> elementCallback) throws IOException- Throws:
- IOException
 
 - 
setJsonPropertypublic static boolean setJsonProperty(javax.jcr.Node node, JsonUtil.JsonProperty property, MappingRules mapping) throws javax.jcr.RepositoryExceptionCreates or updates one property at a JCR node.- Parameters:
- node- the node which holds the property
- property- the property object transformed from JSON
- Returns:
- trueif the property is set and available
- Throws:
- javax.jcr.RepositoryException- if storing was not possible (some reasons)
 
 - 
makeJcrValuepublic static javax.jcr.Value makeJcrValue(javax.jcr.Node node, int type, Object object, MappingRules mapping) throws PropertyValueFormatException, javax.jcr.RepositoryExceptionCreate a JCR value from string value for the designated JCR type.- Parameters:
- node- the node of the property
- type- the JCR type according to the types declared in PropertyType
- object- the value in the right type or a string representation of the value, for binary values a input stream can be used as parameter or a string with the base64 encoded data for the binary property
- Throws:
- PropertyValueFormatException
- javax.jcr.RepositoryException
 
 - 
makeValueObjectpublic static Object makeValueObject(int type, Object value) Create a JCR value from string value for the designated JCR type.- Parameters:
- type- the JCR type according to the types declared in PropertyType
- value- the value in the right type or a string representation of the value, for binary values a input stream can be used as parameter or a string with the base64 encoded data for the binary property
 
 
- 
 
-