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.
        Parameters:
        json - a JsonValueBase
      • JsonFlattener

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

        public JsonFlattener​(Reader jsonReader)
                      throws IOException
        Creates a JSON flattener.
        Parameters:
        jsonReader - the JSON reader
        Throws:
        IOException - if 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
      • 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