Class 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.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected static com.google.gson.Gson GSON  
      static com.google.gson.GsonBuilder GSON_BUILDER
      the declared builder for JSON POJO mapping
    • Constructor Summary

      Constructors 
      Constructor Description
      JsonUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      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)
      Creates the resource parsed from JSON.
      static void exportChildOrderProperty​(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 void exportJson​(@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 void exportJson​(@NotNull com.google.gson.stream.JsonWriter writer, @NotNull org.apache.sling.api.resource.Resource resource, MappingRules mapping)  
      static void exportJson​(com.google.gson.stream.JsonWriter writer, org.apache.sling.api.resource.Resource resource, MappingRules mapping, int depth)  
      static void exportProperties​(@NotNull com.google.gson.stream.JsonWriter writer, @NotNull org.apache.sling.api.resource.Resource resource, MappingRules mapping)  
      static String getValueString​(Object value, int type, MappingRules mapping)
      Embeds the property type in the string value if the formats scope is 'value'.
      static String getValueTarget​(@Nullable org.apache.sling.api.resource.Resource resource, @Nullable String name, @Nullable String value)  
      static org.apache.sling.api.resource.Resource importJson​(com.google.gson.stream.JsonReader reader, org.apache.sling.api.resource.ResourceResolver resolver, String path)  
      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)  
      static boolean importJsonProperty​(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 void jsonMap​(@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 void jsonMapEntries​(@NotNull com.google.gson.stream.JsonWriter writer, @Nullable Map<String,​?> map)  
      static void jsonValue​(@NotNull com.google.gson.stream.JsonWriter writer, @Nullable Object value)
      Transforms an object into a JSON object (stream).
      static Object jsonValue​(com.google.gson.stream.JsonReader reader)
      Transforms a JSON object (stream) into an object.
      static javax.jcr.Value makeJcrValue​(javax.jcr.Node node, int type, Object object, MappingRules mapping)
      Create a JCR value from string value for the designated JCR type.
      static Object makeValueObject​(int type, Object value)
      Create a JCR value from string value for the designated JCR type.
      static JsonUtil.JsonProperty parseJsonProperty​(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 String parseJsonString​(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 boolean setJsonProperty​(javax.jcr.Node node, JsonUtil.JsonProperty property, MappingRules mapping)
      Creates or updates one property at a JCR node.
      static void writeJsonArray​(com.google.gson.stream.JsonWriter writer, String[] values)  
      static <T> void writeJsonArray​(com.google.gson.stream.JsonWriter writer, String name, Iterator<T> values, JsonUtil.ElementCallback<T> elementCallback)  
      static void writeJsonArray​(com.google.gson.stream.JsonWriter writer, Iterator<String> values)  
      static <T> void writeJsonArray​(com.google.gson.stream.JsonWriter writer, Iterator<T> values, JsonUtil.ElementCallback<T> elementCallback)  
      static void writeJsonProperties​(@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 void writeJsonProperty​(@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 void writeJsonProperty​(@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 void writeJsonValue​(@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 void writeJsonValueMap​(@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 void writeValue​(com.google.gson.stream.JsonWriter writer, String name, boolean value)  
      static void writeValue​(com.google.gson.stream.JsonWriter writer, String name, double value)  
      static void writeValue​(com.google.gson.stream.JsonWriter writer, String name, long value)  
      static void writeValue​(com.google.gson.stream.JsonWriter writer, String name, Number value)  
      static void writeValue​(com.google.gson.stream.JsonWriter writer, String name, String value)  
    • Field Detail

      • GSON_BUILDER

        public static final com.google.gson.GsonBuilder GSON_BUILDER
        the declared builder for JSON POJO mapping
      • GSON

        protected static final com.google.gson.Gson GSON
    • Constructor Detail

      • JsonUtil

        public JsonUtil()
    • Method Detail

      • jsonMap

        public static Map<String,​Object> jsonMap​(com.google.gson.stream.JsonReader reader)
                                                throws IOException
        Transforms a JSON object (stream) into a Map object.
        Throws:
        IOException
      • jsonValue

        public static Object jsonValue​(com.google.gson.stream.JsonReader reader)
                                throws IOException
        Transforms a JSON object (stream) into an object.
        Throws:
        IOException
      • jsonMap

        public static void jsonMap​(@NotNull
                                   @NotNull com.google.gson.stream.JsonWriter writer,
                                   @Nullable
                                   @Nullable Map<String,​?> map)
                            throws IOException
        Transforms a Map object into a JSON object (stream).
        Throws:
        IOException
      • jsonMapEntries

        public static void jsonMapEntries​(@NotNull
                                          @NotNull com.google.gson.stream.JsonWriter writer,
                                          @Nullable
                                          @Nullable Map<String,​?> map)
                                   throws IOException
        Throws:
        IOException
      • jsonValue

        public static void jsonValue​(@NotNull
                                     @NotNull com.google.gson.stream.JsonWriter writer,
                                     @Nullable
                                     @Nullable Object value)
                              throws IOException
        Transforms an object into a JSON object (stream).
        Throws:
        IOException
      • exportJson

        public static void exportJson​(@NotNull
                                      @NotNull com.google.gson.stream.JsonWriter writer,
                                      @NotNull
                                      @NotNull org.apache.sling.api.resource.Resource resource)
                               throws javax.jcr.RepositoryException,
                                      IOException
        Writes 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
      • exportJson

        public 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
      • exportJson

        public 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
      • exportProperties

        public 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
      • exportChildOrderProperty

        public static void exportChildOrderProperty​(com.google.gson.stream.JsonWriter writer,
                                                    org.apache.sling.api.resource.Resource resource)
                                             throws IOException
        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.
        Throws:
        IOException
      • importJson

        public 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
      • importJson

        public 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
      • createResource

        public 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.RepositoryException
        Creates 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
      • parseJsonProperty

        public 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
      • parseJsonString

        public 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
      • importJsonProperty

        public static boolean importJsonProperty​(javax.jcr.ValueFactory factory,
                                                 org.apache.sling.api.resource.Resource resource,
                                                 JsonUtil.JsonProperty property,
                                                 MappingRules mapping)
                                          throws javax.jcr.RepositoryException
        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. 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
      • writeJsonProperties

        public 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,
                                               IOException
        Write 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
      • writeJsonValueMap

        public 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,
                                             IOException
        Write 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
      • writeJsonProperty

        public 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,
                                             IOException
        Writes 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
      • writeJsonProperty

        public 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,
                                             IOException
        Writes a resource property (without node - probably synthetic resource)
        Throws:
        javax.jcr.RepositoryException
        IOException
      • getValueTarget

        public static String getValueTarget​(@Nullable
                                            @Nullable org.apache.sling.api.resource.Resource resource,
                                            @Nullable
                                            @Nullable String name,
                                            @Nullable
                                            @Nullable String value)
      • writeJsonValue

        public 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,
                                          IOException
        Write 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
      • writeValue

        public static void writeValue​(com.google.gson.stream.JsonWriter writer,
                                      String name,
                                      boolean value)
                               throws IOException
        Throws:
        IOException
      • writeValue

        public static void writeValue​(com.google.gson.stream.JsonWriter writer,
                                      String name,
                                      double value)
                               throws IOException
        Throws:
        IOException
      • writeValue

        public static void writeValue​(com.google.gson.stream.JsonWriter writer,
                                      String name,
                                      long value)
                               throws IOException
        Throws:
        IOException
      • getValueString

        public static String getValueString​(Object value,
                                            int type,
                                            MappingRules mapping)
        Embeds the property type in the string value if the formats scope is 'value'.
      • writeJsonArray

        public static void writeJsonArray​(com.google.gson.stream.JsonWriter writer,
                                          String[] values)
                                   throws IOException
        Throws:
        IOException
      • setJsonProperty

        public static boolean setJsonProperty​(javax.jcr.Node node,
                                              JsonUtil.JsonProperty property,
                                              MappingRules mapping)
                                       throws javax.jcr.RepositoryException
        Creates or updates one property at a JCR node.
        Parameters:
        node - the node which holds the property
        property - the property object transformed from JSON
        Returns:
        true if the property is set and available
        Throws:
        javax.jcr.RepositoryException - if storing was not possible (some reasons)
      • makeJcrValue

        public static javax.jcr.Value makeJcrValue​(javax.jcr.Node node,
                                                   int type,
                                                   Object object,
                                                   MappingRules mapping)
                                            throws PropertyValueFormatException,
                                                   javax.jcr.RepositoryException
        Create 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
      • makeValueObject

        public 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