Class JsonFlattener


  • public final class JsonFlattener
    extends Object
    JsonFlattener flattens any JSON nested objects or arrays into a flattened JSON string or a Map<Stirng, Object>. The String key will represents the corresponding position of value in the original nested objects or arrays and the Object value are either String, Boolean, Long, Double or null.

    For example:
    A nested JSON
    { "a" : { "b" : 1, "c": null, "d": [false, true] }, "e": "f", "g":2.3 }

    can be turned into a flattened JSON
    { "a.b": 1, "a.c": null, "a.d[0]": false, "a.d[1]": true, "e": "f", "g":2.3 }

    or into a Map
    {
      a.b=1,
      a.c=null,
      a.d[0]=false,
      a.d[1]=true,
      e=f,
      g=2.3
    }
    Author:
    Wei-Ming Wu
    • Constructor Detail

      • JsonFlattener

        public JsonFlattener​(com.github.wnameless.json.base.JsonValueBase<?> json)
        Creates a JSON flattener by given JsonValueBase.
        Parameters:
        json - a JsonValueBase
      • JsonFlattener

        public JsonFlattener​(com.github.wnameless.json.base.JsonCore<?> jsonCore,
                             com.github.wnameless.json.base.JsonValueBase<?> json)
        Creates a JSON flattener by given JsonCore and JsonValueBase.
        Parameters:
        jsonCore - a JsonCore
        json - a JSON string
      • JsonFlattener

        public JsonFlattener​(String json)
        Creates a JSON flattener by given JSON string.
        Parameters:
        json - a JSON string
      • JsonFlattener

        public JsonFlattener​(com.github.wnameless.json.base.JsonCore<?> jsonCore,
                             String json)
        Creates a JSON flattener by given JsonCore and JSON string.
        Parameters:
        jsonCore - a JsonCore
        json - a JSON string
      • JsonFlattener

        public JsonFlattener​(Reader jsonReader)
                      throws IOException
        Creates a JSON flattener by given JSON string reader.
        Parameters:
        jsonReader - a JSON reader
        Throws:
        IOException - if the jsonReader cannot be read
      • JsonFlattener

        public JsonFlattener​(com.github.wnameless.json.base.JsonCore<?> jsonCore,
                             Reader jsonReader)
                      throws IOException
        Creates a JSON flattener by given JsonCore and JSON string reader.
        Parameters:
        jsonCore - a JsonCore
        jsonReader - a JSON string
        Throws:
        IOException - if the jsonReader cannot be read
    • Method Detail

      • flatten

        public static String flatten​(com.github.wnameless.json.base.JsonValueBase<?> jsonVal)
        Returns a flattened JSON string.
        Parameters:
        jsonVal - a JSON data which wraps by JsonValueBase
        Returns:
        a flattened JSON string
      • flatten

        public static String flatten​(String json)
        Returns a flattened JSON string.
        Parameters:
        json - the JSON string
        Returns:
        a flattened JSON string
      • flattenAsMap

        public static Map<String,​Object> flattenAsMap​(com.github.wnameless.json.base.JsonValueBase<?> jsonVal)
        Returns a flattened JSON as Map.
        Parameters:
        jsonVal - a JSON data which wraps by JsonValueBase
        Returns:
        a flattened JSON as Map
      • flattenAsMap

        public static Map<String,​Object> flattenAsMap​(String json)
        Returns a flattened JSON as Map.
        Parameters:
        json - the JSON string
        Returns:
        a flattened JSON as Map
      • withSeparator

        public JsonFlattener withSeparator​(char separator)
        A fluent setter to setup the separator within a key in the flattened JSON. The default separator is a dot(.).
        Parameters:
        separator - any character
        Returns:
        this JsonFlattener
      • withLeftAndRightBrackets

        public JsonFlattener withLeftAndRightBrackets​(char leftBracket,
                                                      char rightBracket)
        A fluent setter to setup the left and right brackets within a key in the flattened JSON. The default left and right brackets are left square bracket([) and right square bracket(]).
        Parameters:
        leftBracket - any character
        rightBracket - any character
        Returns:
        this JsonFlattener
      • ignoreReservedCharacters

        public JsonFlattener ignoreReservedCharacters()
        After this option is enable, all reserved characters used in keys will stop to be checked and escaped.

        Example:

        Input JSON: {"matrix":{"agent.smith":"1999"}}
        Flatten with option disable: {"matrix[\"agent.smith\"]":"1999"}
        Flatten with option enable: {"matrix.agent.smith":"1999"}

        JsonUnflattener may cause unpredictable results with the JSON produced by a JsonFlattener with this option enable.
        Returns:
        this JsonFlattener
      • flatten

        public String flatten()
        Returns a flattened JSON string.
        Returns:
        a flattened JSON string
      • flattenAsMap

        public Map<String,​Object> flattenAsMap()
        Returns a flattened JSON as Map.
        Returns:
        a flattened JSON as Map
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object