java.lang.Object
com.github.wnameless.json.flattener.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
  • Field Details

  • Constructor Details

    • 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 Details

    • 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
    • withFlattenMode

      public JsonFlattener withFlattenMode(FlattenMode flattenMode)
      A fluent setter to setup a mode of the JsonFlattener.
      Parameters:
      flattenMode - a FlattenMode
      Returns:
      this JsonFlattener
    • withStringEscapePolicy

      public JsonFlattener withStringEscapePolicy(CharSequenceTranslatorFactory policy)
      A fluent setter to setup the JSON string escape policy.
      Parameters:
      policy - any CharSequenceTranslatorFactory or a StringEscapePolicy
      Returns:
      this JsonFlattener
    • 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
    • withPrintMode

      public JsonFlattener withPrintMode(PrintMode printMode)
      A fluent setter to setup a print mode of the JsonFlattener. The default print mode is minimal.
      Parameters:
      printMode - a PrintMode
      Returns:
      this JsonFlattener
    • withKeyTransformer

      public JsonFlattener withKeyTransformer(KeyTransformer keyTrans)
      A fluent setter to setup a KeyTransformer of the JsonFlattener.
      Parameters:
      keyTrans - a KeyTransformer
      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
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object