com.h3c.util.json

## Class JSON

- java.lang.Object
- - com.h3c.util.json.JSON

-     public class JSON
      extends Object

  A utility for encoding and decoding POJOs to and from their corresponding JSON representations. This utility merely relies on the underlying `JsonFactory`'s to provide the needed codecs.

- - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `static Set<JsonFactory>` | `factories()` List all registered factories (unmodifiable)
    `static <T> JsonFactory` | `factory(Class<T> factoryClazz)` Retrieve the JSON factory instance by the given class.
    `static <T> T` | `fromJson(String json, Class<T> pojo)` Convert the given JSON string to the given typed POJO.
    `static <T> List<T>` | `fromJsonList(String json, Class<T> pojo)` Convert the given JSON string to a typed list of POJO's.
    `static void` | `registerFactory(JsonFactory factory)` Register the given factory with this utility.
    `static <T> String` | `toJson(T pojo)` Convert a POJO to its JSON representation with compact formatting.
    `static <T> String` | `toJson(T pojo, boolean prettyPrint)` Convert a POJO to its JSON representation.
    `static <T> String` | `toJsonList(Collection<T> pojos, Class<T> pojo)` Convert a collection of POJO's into its JSON representation with compact formatting.
    `static <T> String` | `toJsonList(Collection<T> pojos, Class<T> pojo, boolean prettyPrint)` Convert a collection of POJO's into its JSON representation.
    `static void` | `unregisterFactory(JsonFactory factory)` Unregister the given factory with this utility.

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

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

- - ### Method Detail

    - #### registerFactory

          public static void registerFactory(JsonFactory factory)

      Register the given factory with this utility.
      Parameters:
        `factory` - JsonFactory to register

    - #### unregisterFactory

          public static void unregisterFactory(JsonFactory factory)

      Unregister the given factory with this utility. It is the responsibility of the factory to do any clean up necessary.
      Parameters:
        `factory` - JsonFactory to unregister

    - #### fromJson

          public static <T> T fromJson(String json,
                       Class<T> pojo)

      Convert the given JSON string to the given typed POJO.
      Parameters:
        `json` - JSON string representing an instance of the given POJO class
        `pojo` - POJO class of the given JSON string
      Returns:
        POJO instance represented by the JSON string

    - #### fromJsonList

          public static <T> List<T> fromJsonList(String json,
                                 Class<T> pojo)

      Convert the given JSON string to a typed list of POJO's.
      Parameters:
        `json` - JSON string representing a typed list of POJO's
        `pojo` - POJO class of the given JSON string
      Returns:
        typed list of POJO's represented by the JSON string

    - #### toJson

          public static <T> String toJson(T pojo,
                          boolean prettyPrint)

      Convert a POJO to its JSON representation.
      Parameters:
        `pojo` - POJO instance to convert
        `prettyPrint` - human-readable formatted JSON if true, single-line JSON otherwise
      Returns:
        JSON string representation of POJO

    - #### toJson

          public static <T> String toJson(T pojo)

      Convert a POJO to its JSON representation with compact formatting.
      Parameters:
        `pojo` - POJO instance to convert
      Returns:
        JSON string representation of POJO

    - #### toJsonList

          public static <T> String toJsonList(Collection<T> pojos,
                              Class<T> pojo,
                              boolean prettyPrint)

      Convert a collection of POJO's into its JSON representation.
      Parameters:
        `pojos` - collection of POJO's
        `pojo` - class of POJO
        `prettyPrint` - human-readable formatted JSON if true, single-line JSON otherwise
      Returns:
        JSON string representation of the list of POJO's

    - #### toJsonList

          public static <T> String toJsonList(Collection<T> pojos,
                              Class<T> pojo)

      Convert a collection of POJO's into its JSON representation with compact formatting.
      Parameters:
        `pojos` - collection of POJO's
        `pojo` - class of POJO
      Returns:
        JSON string representation of the list of POJO's

    - #### factories

          public static Set<JsonFactory> factories()

      List all registered factories (unmodifiable)
      Returns:
        set of registered factories

    - #### factory

          public static <T> JsonFactory factory(Class<T> factoryClazz)

      Retrieve the JSON factory instance by the given class.
      Parameters:
        `factoryClazz` - class of json factory to retrieve
      Returns:
        the JSON factory instance by the given class

Copyright © 2014. All Rights Reserved.