Class JsonUtil


  • public final class JsonUtil
    extends java.lang.Object
    Json utilities based on Gson (com.google.gson).
    Since:
    2.0.0
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static com.google.gson.Gson getParser()
      Gets the singleton instance of the parser.
      static void registerTypeAdapter​(java.lang.Class<?> matchingClass, java.lang.Object adapter, boolean withSubclasses)
      Registers a new type adapter.
      static java.lang.String toJson​(java.lang.Object obj)
      Formats the provided object as a json string.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • getParser

        public static com.google.gson.Gson getParser()
        Gets the singleton instance of the parser.

        If not created yet, a default instance is created.

        Caution: to use the Gson object in return, the invoker must also import the Gson library from Google (see the manifest of the NOTICE document for the version to be used).

        Returns:
        a not null instance.
        Since:
        2.0.0
      • registerTypeAdapter

        public static void registerTypeAdapter​(java.lang.Class<?> matchingClass,
                                               java.lang.Object adapter,
                                               boolean withSubclasses)
        Registers a new type adapter.
        Parameters:
        matchingClass - The type to be registered.
        adapter - The type adapter to be registered (should implement JsonSerializer and/or JsonDeserializer).
        withSubclasses - Apply this adapter to subclasses of matchingClass also.
        Since:
        2.0.0
      • toJson

        public static java.lang.String toJson​(java.lang.Object obj)
        Formats the provided object as a json string.
        Parameters:
        obj - The object to format.
        Returns:
        A not empty String.
        Since:
        2.0.0