org.apache.geode.pdx

## Class JSONFormatter

- java.lang.Object
- - org.apache.geode.pdx.JSONFormatter

-     public class JSONFormatter
      extends java.lang.Object

  JSONFormatter has a static method `fromJSON(String)` to convert a JSON document into a `PdxInstance` and a static method `toJSON(PdxInstance)` to convert a `PdxInstance` into a JSON Document.

  Using these methods an applications may convert a JSON document into a PdxInstance for storing in the cache. Indexes can then be defined on the PdxInstances so that queries can be performed using OQL. Queries will return PdxInstances and these can be turned back back into JSON documents using JSONFormatter.

  JSONFormatter treats values in a json document as number(byte, short, int, long..), string, array, object, 'true', 'false' or 'null'. These correspond to the following java types:

  JSON | Java
  object | `PdxInstance`
  arrays | `LinkedList`
  BigDecimal | `BigDecimal`
  BigInterger | `BigInteger`
  Double | double
  float | float
  boolean | boolean
  Integer | byte, short or int
  null | null

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static java.lang.String` | `JSON_CLASSNAME`
    `static java.lang.String` | `SORT_JSON_FIELD_NAMES_PROPERTY` By setting "gemfire.sort-json-field-names" to true, enables serialization of JSON field in JSON document to be sorted.

  - ### Method Summary

    All Methods Static Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `static PdxInstance` | `fromJSON(byte[] jsonByteArray)` Converts a JSON document into a PdxInstance
    `static PdxInstance` | `fromJSON(java.lang.String jsonString)` Converts a JSON document into a PdxInstance
    `static java.lang.String` | `toJSON(PdxInstance pdxInstance)` Converts a PdxInstance into a JSON document
    `static byte[]` | `toJSONByteArray(PdxInstance pdxInstance)` Converts a PdxInstance into a JSON document in byte-array form

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### JSON_CLASSNAME

          public static final java.lang.String JSON_CLASSNAME

      See Also:
        Constant Field Values

    - #### SORT_JSON_FIELD_NAMES_PROPERTY

          public static final java.lang.String SORT_JSON_FIELD_NAMES_PROPERTY

      By setting "gemfire.sort-json-field-names" to true, enables serialization of JSON field in JSON document to be sorted. That can help to reduce the number of pdx typeId generation if different JSON documents have same fields in the different order.
      See Also:
        Constant Field Values

  - ### Method Detail

    - #### fromJSON

          public static PdxInstance fromJSON(java.lang.String jsonString)

      Converts a JSON document into a PdxInstance
      Returns:
        the PdxInstance.
      Throws:
        `JSONFormatterException` - if unable to parse the JSON document

    - #### fromJSON

          public static PdxInstance fromJSON(byte[] jsonByteArray)

      Converts a JSON document into a PdxInstance
      Returns:
        the PdxInstance.
      Throws:
        `JSONFormatterException` - if unable to parse the JSON document

    - #### toJSON

          public static java.lang.String toJSON(PdxInstance pdxInstance)

      Converts a PdxInstance into a JSON document
      Returns:
        the JSON string.
      Throws:
        `JSONFormatterException` - if unable to create the JSON document

    - #### toJSONByteArray

          public static byte[] toJSONByteArray(PdxInstance pdxInstance)

      Converts a PdxInstance into a JSON document in byte-array form
      Returns:
        the JSON byte array.
      Throws:
        `JSONFormatterException` - if unable to create the JSON document