Class JsonUtil


  • public class JsonUtil
    extends java.lang.Object
    Helper utility to work with JSON values in Java types. Includes the JSON dialect used by Protocol Buffers.
    • Constructor Summary

      Constructors 
      Constructor Description
      JsonUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.util.List<java.util.Map<java.lang.String,​?>> checkObjectList​(java.util.List<?> rawList)
      Casts a list of unchecked JSON values to a list of checked objects in Java type.
      static java.util.List<java.lang.String> checkStringList​(java.util.List<?> rawList)
      Casts a list of unchecked JSON values to a list of String.
      private static boolean durationIsValid​(long seconds, int nanos)
      Returns true if the given number of seconds and nanos is a valid Duration.
      static java.lang.Boolean getBoolean​(java.util.Map<java.lang.String,​?> obj, java.lang.String key)
      Gets a boolean from an object for the given key.
      static java.util.List<?> getList​(java.util.Map<java.lang.String,​?> obj, java.lang.String key)
      Gets a list from an object for the given key.
      static java.util.List<java.util.Map<java.lang.String,​?>> getListOfObjects​(java.util.Map<java.lang.String,​?> obj, java.lang.String key)
      Gets a list from an object for the given key, and verifies all entries are objects.
      static java.util.List<java.lang.String> getListOfStrings​(java.util.Map<java.lang.String,​?> obj, java.lang.String key)
      Gets a list from an object for the given key, and verifies all entries are strings.
      static java.lang.Double getNumberAsDouble​(java.util.Map<java.lang.String,​?> obj, java.lang.String key)
      Gets a number from an object for the given key.
      static java.lang.Float getNumberAsFloat​(java.util.Map<java.lang.String,​?> obj, java.lang.String key)
      Gets a number from an object for the given key.
      static java.lang.Integer getNumberAsInteger​(java.util.Map<java.lang.String,​?> obj, java.lang.String key)
      Gets a number from an object for the given key, casted to an integer.
      static java.lang.Long getNumberAsLong​(java.util.Map<java.lang.String,​?> obj, java.lang.String key)
      Gets a number from an object for the given key, casted to an long.
      static java.util.Map<java.lang.String,​?> getObject​(java.util.Map<java.lang.String,​?> obj, java.lang.String key)
      Gets an object from an object for the given key.
      static java.lang.String getString​(java.util.Map<java.lang.String,​?> obj, java.lang.String key)
      Gets a string from an object for the given key.
      static java.lang.Long getStringAsDuration​(java.util.Map<java.lang.String,​?> obj, java.lang.String key)
      Gets a string from an object for the given key, parsed as a duration (defined by protobuf).
      private static long normalizedDuration​(long seconds, int nanos)
      Copy of Durations.normalizedDuration(long, int).
      private static long parseDuration​(java.lang.String value)
      Parse from a string to produce a duration.
      private static int parseNanos​(java.lang.String value)
      Copy of Timestamps.parseNanos(java.lang.String).
      private static long saturatedAdd​(long a, long b)
      Returns the sum of a and b unless it would overflow or underflow in which case Long.MAX_VALUE or Long.MIN_VALUE is returned, respectively.
      • Methods inherited from class java.lang.Object

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

      • NANOS_PER_SECOND

        private static final long NANOS_PER_SECOND
    • Constructor Detail

      • JsonUtil

        public JsonUtil()
    • Method Detail

      • getList

        @Nullable
        public static java.util.List<?> getList​(java.util.Map<java.lang.String,​?> obj,
                                                java.lang.String key)
        Gets a list from an object for the given key. If the key is not present, this returns null. If the value is not a List, throws an exception.
      • getListOfObjects

        @Nullable
        public static java.util.List<java.util.Map<java.lang.String,​?>> getListOfObjects​(java.util.Map<java.lang.String,​?> obj,
                                                                                               java.lang.String key)
        Gets a list from an object for the given key, and verifies all entries are objects. If the key is not present, this returns null. If the value is not a List or an entry is not an object, throws an exception.
      • getListOfStrings

        @Nullable
        public static java.util.List<java.lang.String> getListOfStrings​(java.util.Map<java.lang.String,​?> obj,
                                                                        java.lang.String key)
        Gets a list from an object for the given key, and verifies all entries are strings. If the key is not present, this returns null. If the value is not a List or an entry is not a string, throws an exception.
      • getObject

        @Nullable
        public static java.util.Map<java.lang.String,​?> getObject​(java.util.Map<java.lang.String,​?> obj,
                                                                        java.lang.String key)
        Gets an object from an object for the given key. If the key is not present, this returns null. If the value is not a Map, throws an exception.
      • getNumberAsDouble

        @Nullable
        public static java.lang.Double getNumberAsDouble​(java.util.Map<java.lang.String,​?> obj,
                                                         java.lang.String key)
        Gets a number from an object for the given key. If the key is not present, this returns null. If the value does not represent a double, throws an exception.
      • getNumberAsFloat

        @Nullable
        public static java.lang.Float getNumberAsFloat​(java.util.Map<java.lang.String,​?> obj,
                                                       java.lang.String key)
        Gets a number from an object for the given key. If the key is not present, this returns null. If the value does not represent a float, throws an exception.
      • getNumberAsInteger

        @Nullable
        public static java.lang.Integer getNumberAsInteger​(java.util.Map<java.lang.String,​?> obj,
                                                           java.lang.String key)
        Gets a number from an object for the given key, casted to an integer. If the key is not present, this returns null. If the value does not represent an integer, throws an exception.
      • getNumberAsLong

        public static java.lang.Long getNumberAsLong​(java.util.Map<java.lang.String,​?> obj,
                                                     java.lang.String key)
        Gets a number from an object for the given key, casted to an long. If the key is not present, this returns null. If the value does not represent a long integer, throws an exception.
      • getString

        @Nullable
        public static java.lang.String getString​(java.util.Map<java.lang.String,​?> obj,
                                                 java.lang.String key)
        Gets a string from an object for the given key. If the key is not present, this returns null. If the value is not a String, throws an exception.
      • getStringAsDuration

        public static java.lang.Long getStringAsDuration​(java.util.Map<java.lang.String,​?> obj,
                                                         java.lang.String key)
        Gets a string from an object for the given key, parsed as a duration (defined by protobuf). If the key is not present, this returns null. If the value is not a String or not properly formatted, throws an exception.
      • getBoolean

        @Nullable
        public static java.lang.Boolean getBoolean​(java.util.Map<java.lang.String,​?> obj,
                                                   java.lang.String key)
        Gets a boolean from an object for the given key. If the key is not present, this returns null. If the value is not a Boolean, throws an exception.
      • checkObjectList

        public static java.util.List<java.util.Map<java.lang.String,​?>> checkObjectList​(java.util.List<?> rawList)
        Casts a list of unchecked JSON values to a list of checked objects in Java type. If the given list contains a value that is not a Map, throws an exception.
      • checkStringList

        public static java.util.List<java.lang.String> checkStringList​(java.util.List<?> rawList)
        Casts a list of unchecked JSON values to a list of String. If the given list contains a value that is not a String, throws an exception.
      • parseDuration

        private static long parseDuration​(java.lang.String value)
                                   throws java.text.ParseException
        Parse from a string to produce a duration. Copy of Durations.parse(java.lang.String).
        Returns:
        A Duration parsed from the string.
        Throws:
        java.text.ParseException - if parsing fails.
      • parseNanos

        private static int parseNanos​(java.lang.String value)
                               throws java.text.ParseException
        Copy of Timestamps.parseNanos(java.lang.String).
        Throws:
        java.text.ParseException
      • normalizedDuration

        private static long normalizedDuration​(long seconds,
                                               int nanos)
        Copy of Durations.normalizedDuration(long, int).
      • durationIsValid

        private static boolean durationIsValid​(long seconds,
                                               int nanos)
        Returns true if the given number of seconds and nanos is a valid Duration. The seconds value must be in the range [-315,576,000,000, +315,576,000,000]. The nanos value must be in the range [-999,999,999, +999,999,999].

        Note: Durations less than one second are represented with a 0 seconds field and a positive or negative nanos field. For durations of one second or more, a non-zero value for the nanos field must be of the same sign as the seconds field.

        Copy of com.google.protobuf.util.Duration#isValid.

      • saturatedAdd

        private static long saturatedAdd​(long a,
                                         long b)
        Returns the sum of a and b unless it would overflow or underflow in which case Long.MAX_VALUE or Long.MIN_VALUE is returned, respectively.

        Copy of LongMath.saturatedAdd(long, long).