程序包 com.sh.json.util

类 JSONUtils

java.lang.Object
com.sh.json.util.JSONUtils

public final class JSONUtils extends Object
Provides useful methods on java objects and JSON values.
  • 字段概要

    字段
    修饰符和类型
    字段
    说明
    static final String
    Constant for char "
    static final com.fasterxml.jackson.databind.ObjectMapper
     
    static final String
    Constant for char '
  • 方法概要

    修饰符和类型
    方法
    说明
    static String
    Transforms the string into a valid Java Identifier.
    The default strategy is JavaIdentifierTransformer.NOOP
    static String
    Transforms the string into a valid Java Identifier.
    The default strategy is JavaIdentifierTransformer.NOOP
    static String
    doubleToString(double d)
    Produce a string from a double.
    static String
    Returns the body of a function literal.
    static String
    Returns the params of a function literal.
    static Class
    Returns the inner-most component type of an Array.
    Returns the singleton MorpherRegistry.
    static Map
    Creates a Map with all the properties of the JSONObject.
    static Class
    Returns the JSON type.
    Values are Object, String, Boolean, Number(subclasses) & JSONFunction.
    static int
    Returns the hashcode of value.
    If null it will return JSONNull.getInstance().hashCode().
    If value is JSON, JSONFunction or String, value.hashCode is returned, otherwise the value is transformed to a String an its hashcode is returned.
    static boolean
    Returns true if the input has single-quotes or double-quotes at both sides.
    static boolean
    isArray(Class clazz)
    Tests if a Class represents an array or Collection.
    static boolean
    Tests if obj is an array or Collection.
    static boolean
    Tests if Class represents a Boolean or primitive boolean
    static boolean
    Tests if obj is a Boolean or primitive boolean
    static boolean
    isDouble(Class clazz)
    Tests if Class represents a primitive double or wrapper.
    static boolean
    Tests if obj is javaScript function.
    Obj must be a non-null String and match "^function[ ]?
    static boolean
    Tests if obj is javaScript function header.
    Obj must be a non-null String and match "^function[ ]?
    static boolean
    Returns trus if str represents a valid Java identifier.
    static boolean
    isJsonKeyword(String input, JsonConfig jsonConfig)
     
    static boolean
    Tests if the obj is a javaScript null.
    static boolean
    isNumber(Class clazz)
    Tests if Class represents a primitive number or wrapper.
    static boolean
    Tests if obj is a primitive number or wrapper.
    static boolean
    Tests if obj is not a boolean, number, string or array.
    static boolean
    isString(Class clazz)
    Tests if Class represents a String or a char
    static boolean
    Tests if obj is a String or a char
    static boolean
    mayBeJSON(String string)
    Tests if the String possibly represents a valid JSON String.
    Valid JSON strings are: "null" starts with "[" and ends with "]" starts with "{" and ends with "}"
    static org.apache.commons.beanutils.DynaBean
    newDynaBean(JSONObject jsonObject)
    Creates a new MorphDynaBean from a JSONObject.
    static org.apache.commons.beanutils.DynaBean
    newDynaBean(JSONObject jsonObject, JsonConfig jsonConfig)
    Creates a new MorphDynaBean from a JSONObject.
    static String
    Produce a string from a Number.
    static String
    quote(String string)
    Produce a string in double quotes with backslash sequences in all the right places.
    static String
    Strips any single-quotes or double-quotes from both sides of the string.
    static void
    Throw an exception if the object is an NaN or infinite number.
    static Number
    Transforms a Number into a valid javascript number.
    Float gets promoted to Double.
    Byte and Short get promoted to Integer.
    Long gets downgraded to Integer if possible.
    static String
    Make a JSON text of an Object value.
    static String
    valueToString(Object value, int indentFactor, int indent)
    Make a prettyprinted JSON text of an object value.

    从类继承的方法 java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 字段详细资料

    • OBJECT_MAPPER

      public static final com.fasterxml.jackson.databind.ObjectMapper OBJECT_MAPPER
    • DOUBLE_QUOTE

      public static final String DOUBLE_QUOTE
      Constant for char "
      另请参阅:
    • SINGLE_QUOTE

      public static final String SINGLE_QUOTE
      Constant for char '
      另请参阅:
  • 方法详细资料

    • convertToJavaIdentifier

      public static String convertToJavaIdentifier(String key)
      Transforms the string into a valid Java Identifier.
      The default strategy is JavaIdentifierTransformer.NOOP
      抛出:
      JSONException - if the string can not be transformed.
    • convertToJavaIdentifier

      public static String convertToJavaIdentifier(String key, JsonConfig jsonConfig)
      Transforms the string into a valid Java Identifier.
      The default strategy is JavaIdentifierTransformer.NOOP
      抛出:
      JSONException - if the string can not be transformed.
    • doubleToString

      public static String doubleToString(double d)
      Produce a string from a double. The string "null" will be returned if the number is not finite.
      参数:
      d - A double.
      返回:
      A String.
    • getFunctionBody

      public static String getFunctionBody(String function)
      Returns the body of a function literal.
    • getFunctionParams

      public static String getFunctionParams(String function)
      Returns the params of a function literal.
    • getInnerComponentType

      public static Class getInnerComponentType(Class type)
      Returns the inner-most component type of an Array.
    • getMorpherRegistry

      public static MorpherRegistry getMorpherRegistry()
      Returns the singleton MorpherRegistry.
    • getProperties

      public static Map getProperties(JSONObject jsonObject)
      Creates a Map with all the properties of the JSONObject.
    • getTypeClass

      public static Class getTypeClass(Object obj)
      Returns the JSON type.
      Values are Object, String, Boolean, Number(subclasses) & JSONFunction.
    • hashCode

      public static int hashCode(Object value)
      Returns the hashcode of value.
      If null it will return JSONNull.getInstance().hashCode().
      If value is JSON, JSONFunction or String, value.hashCode is returned, otherwise the value is transformed to a String an its hashcode is returned.
    • isArray

      public static boolean isArray(Class clazz)
      Tests if a Class represents an array or Collection.
    • isArray

      public static boolean isArray(Object obj)
      Tests if obj is an array or Collection.
    • isBoolean

      public static boolean isBoolean(Class clazz)
      Tests if Class represents a Boolean or primitive boolean
    • isBoolean

      public static boolean isBoolean(Object obj)
      Tests if obj is a Boolean or primitive boolean
    • isDouble

      public static boolean isDouble(Class clazz)
      Tests if Class represents a primitive double or wrapper.
    • isFunction

      public static boolean isFunction(Object obj)
      Tests if obj is javaScript function.
      Obj must be a non-null String and match "^function[ ]?\\(.*\\)[ ]?\\{.*\\}$"
    • isFunctionHeader

      public static boolean isFunctionHeader(Object obj)
      Tests if obj is javaScript function header.
      Obj must be a non-null String and match "^function[ ]?\\(.*\\)$"
    • isJavaIdentifier

      public static boolean isJavaIdentifier(String str)
      Returns trus if str represents a valid Java identifier.
    • isNull

      public static boolean isNull(Object obj)
      Tests if the obj is a javaScript null.
    • isNumber

      public static boolean isNumber(Class clazz)
      Tests if Class represents a primitive number or wrapper.
    • isNumber

      public static boolean isNumber(Object obj)
      Tests if obj is a primitive number or wrapper.
    • isObject

      public static boolean isObject(Object obj)
      Tests if obj is not a boolean, number, string or array.
    • isString

      public static boolean isString(Class clazz)
      Tests if Class represents a String or a char
    • isString

      public static boolean isString(Object obj)
      Tests if obj is a String or a char
    • mayBeJSON

      public static boolean mayBeJSON(String string)
      Tests if the String possibly represents a valid JSON String.
      Valid JSON strings are:
      • "null"
      • starts with "[" and ends with "]"
      • starts with "{" and ends with "}"
    • newDynaBean

      public static org.apache.commons.beanutils.DynaBean newDynaBean(JSONObject jsonObject)
      Creates a new MorphDynaBean from a JSONObject. The MorphDynaBean will have all the properties of the original JSONObject with the most accurate type. Values of properties are not copied.
    • newDynaBean

      public static org.apache.commons.beanutils.DynaBean newDynaBean(JSONObject jsonObject, JsonConfig jsonConfig)
      Creates a new MorphDynaBean from a JSONObject. The MorphDynaBean will have all the properties of the original JSONObject with the most accurate type. Values of properties are not copied.
    • numberToString

      public static String numberToString(Number n)
      Produce a string from a Number.
      参数:
      n - A Number
      返回:
      A String.
      抛出:
      JSONException - If n is a non-finite number.
    • quote

      public static String quote(String string)
      Produce a string in double quotes with backslash sequences in all the right places. A backslash will be inserted within </, allowing JSON text to be delivered in HTML. In JSON text, a string cannot contain a control character or an unescaped quote or backslash.
      CAUTION: if string represents a javascript function, translation of characters will not take place. This will produce a non-conformant JSON text.
      参数:
      string - A String
      返回:
      A String correctly formatted for insertion in a JSON text.
    • stripQuotes

      public static String stripQuotes(String input)
      Strips any single-quotes or double-quotes from both sides of the string.
    • hasQuotes

      public static boolean hasQuotes(String input)
      Returns true if the input has single-quotes or double-quotes at both sides.
    • isJsonKeyword

      public static boolean isJsonKeyword(String input, JsonConfig jsonConfig)
    • testValidity

      public static void testValidity(Object o)
      Throw an exception if the object is an NaN or infinite number.
      参数:
      o - The object to test.
      抛出:
      JSONException - If o is a non-finite number.
    • transformNumber

      public static Number transformNumber(Number input)
      Transforms a Number into a valid javascript number.
      Float gets promoted to Double.
      Byte and Short get promoted to Integer.
      Long gets downgraded to Integer if possible.
    • valueToString

      public static String valueToString(Object value)
      Make a JSON text of an Object value. If the object has an value.toJSONString() method, then that method will be used to produce the JSON text. The method is required to produce a strictly conforming text. If the object does not contain a toJSONString method (which is the most common case), then a text will be produced by the rules.

      Warning: This method assumes that the data structure is acyclical.

      参数:
      value - The value to be serialized.
      返回:
      a printable, displayable, transmittable representation of the object, beginning with { (left brace) and ending with } (right brace).
      抛出:
      JSONException - If the value is or contains an invalid number.
    • valueToString

      public static String valueToString(Object value, int indentFactor, int indent)
      Make a prettyprinted JSON text of an object value.

      Warning: This method assumes that the data structure is acyclical.

      参数:
      value - The value to be serialized.
      indentFactor - The number of spaces to add to each level of indentation.
      indent - The indentation of the top level.
      返回:
      a printable, displayable, transmittable representation of the object, beginning with { (left brace) and ending with } (right brace).
      抛出:
      JSONException - If the object contains an invalid number.