Package org.kordamp.json.util
Class JSONUtils
- java.lang.Object
-
- org.kordamp.json.util.JSONUtils
-
public final class JSONUtils extends java.lang.Object
Provides useful methods on java objects and JSON values.- Version:
- 7
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
DOUBLE_QUOTE
Constant for char "private static java.lang.String
FUNCTION_BODY_PATTERN
private static java.lang.String
FUNCTION_HEADER_PATTERN
private static java.lang.String
FUNCTION_PARAMS_PATTERN
private static java.lang.String
FUNCTION_PATTERN
private static java.lang.String
FUNCTION_PREFIX
private static org.kordamp.ezmorph.MorpherRegistry
morpherRegistry
static java.lang.String
SINGLE_QUOTE
Constant for char '
-
Constructor Summary
Constructors Modifier Constructor Description private
JSONUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.String
convertToJavaIdentifier(java.lang.String key)
Transforms the string into a valid Java Identifier.
The default strategy is JavaIdentifierTransformer.NOOPstatic java.lang.String
convertToJavaIdentifier(java.lang.String key, JsonConfig jsonConfig)
Transforms the string into a valid Java Identifier.
The default strategy is JavaIdentifierTransformer.NOOPstatic java.lang.String
doubleToString(double d)
Produce a string from a double.static java.lang.String
getFunctionBody(java.lang.String function)
Returns the body of a function literal.static java.lang.String
getFunctionParams(java.lang.String function)
Returns the params of a function literal.static java.lang.Class
getInnerComponentType(java.lang.Class type)
Returns the inner-most component type of an Array.static org.kordamp.ezmorph.MorpherRegistry
getMorpherRegistry()
Returns the singleton MorpherRegistry.static java.util.Map
getProperties(JSONObject jsonObject)
Creates a Map with all the properties of the JSONObject.static java.lang.Class
getTypeClass(java.lang.Object obj)
Returns the JSON type.
Values are Object, String, Boolean, Number(subclasses) & JSONFunction.static int
hashCode(java.lang.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.static boolean
hasQuotes(java.lang.String input)
Returns true if the input has single-quotes or double-quotes at both sides.static boolean
isArray(java.lang.Class clazz)
Tests if a Class represents an array or Collection.static boolean
isArray(java.lang.Object obj)
Tests if obj is an array or Collection.private static boolean
isBigDecimal(java.lang.Number n)
Finds out if n represents a BigIntegerprivate static boolean
isBigInteger(java.lang.Number n)
Finds out if n represents a BigIntegerstatic boolean
isBlank(java.lang.String str)
Determines whether a given string isnull
, empty, or only contains whitespace.static boolean
isBoolean(java.lang.Class clazz)
Tests if Class represents a Boolean or primitive booleanstatic boolean
isBoolean(java.lang.Object obj)
Tests if obj is a Boolean or primitive booleanstatic boolean
isDouble(java.lang.Class clazz)
Tests if Class represents a primitive double or wrapper.private static boolean
isDouble(java.lang.Number n)
Finds out if n represents a Double.private static boolean
isFloat(java.lang.Number n)
Finds out if n represents a Float.static boolean
isFunction(java.lang.Object obj)
Tests if obj is javaScript function.
Obj must be a non-null String and match"^function[ ]?\\(.*\\)[ ]?\\{.*\\}$" static boolean
isFunctionHeader(java.lang.Object obj)
Tests if obj is javaScript function header.
Obj must be a non-null String and match "^function[ ]?\\(.*\\)$"private static boolean
isInteger(java.lang.Number n)
Finds out if n represents an Integer.static boolean
isJavaIdentifier(java.lang.String str)
Returns trus if str represents a valid Java identifier.static boolean
isJsonKeyword(java.lang.String input, JsonConfig jsonConfig)
private static boolean
isLong(java.lang.Number n)
Finds out if n represents a Long.static boolean
isNull(java.lang.Object obj)
Tests if the obj is a javaScript null.static boolean
isNumber(java.lang.Class clazz)
Tests if Class represents a primitive number or wrapper.static boolean
isNumber(java.lang.Object obj)
Tests if obj is a primitive number or wrapper.static boolean
isObject(java.lang.Object obj)
Tests if obj is not a boolean, number, string or array.static boolean
isString(java.lang.Class clazz)
Tests if Class represents a String or a charstatic boolean
isString(java.lang.Object obj)
Tests if obj is a String or a charstatic boolean
mayBeJSON(java.lang.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 java.lang.String
numberToString(java.lang.Number n)
Produce a string from a Number.static java.lang.String
quote(java.lang.String string)
Produce a string in double quotes with backslash sequences in all the right places.static java.lang.String
quoteCanonical(java.lang.String s)
Minimal escape form.static java.lang.String
stripQuotes(java.lang.String input)
Strips any single-quotes or double-quotes from both sides of the string.static void
testValidity(java.lang.Object o)
Throw an exception if the object is an NaN or infinite number.static java.lang.Number
transformNumber(java.lang.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.static java.lang.String
valueToCanonicalString(java.lang.Object value)
static java.lang.String
valueToString(java.lang.Object value)
Make a JSON text of an Object value.static java.lang.String
valueToString(java.lang.Object value, int indentFactor, int indent)
Make a prettyprinted JSON text of an object value.
-
-
-
Field Detail
-
DOUBLE_QUOTE
public static final java.lang.String DOUBLE_QUOTE
Constant for char "- See Also:
- Constant Field Values
-
SINGLE_QUOTE
public static final java.lang.String SINGLE_QUOTE
Constant for char '- See Also:
- Constant Field Values
-
FUNCTION_BODY_PATTERN
private static final java.lang.String FUNCTION_BODY_PATTERN
- See Also:
- Constant Field Values
-
FUNCTION_HEADER_PATTERN
private static final java.lang.String FUNCTION_HEADER_PATTERN
- See Also:
- Constant Field Values
-
FUNCTION_PARAMS_PATTERN
private static final java.lang.String FUNCTION_PARAMS_PATTERN
- See Also:
- Constant Field Values
-
FUNCTION_PATTERN
private static final java.lang.String FUNCTION_PATTERN
- See Also:
- Constant Field Values
-
FUNCTION_PREFIX
private static final java.lang.String FUNCTION_PREFIX
- See Also:
- Constant Field Values
-
morpherRegistry
private static final org.kordamp.ezmorph.MorpherRegistry morpherRegistry
-
-
Method Detail
-
convertToJavaIdentifier
public static java.lang.String convertToJavaIdentifier(java.lang.String key)
Transforms the string into a valid Java Identifier.
The default strategy is JavaIdentifierTransformer.NOOP- Throws:
JSONException
- if the string can not be transformed.
-
convertToJavaIdentifier
public static java.lang.String convertToJavaIdentifier(java.lang.String key, JsonConfig jsonConfig)
Transforms the string into a valid Java Identifier.
The default strategy is JavaIdentifierTransformer.NOOP- Throws:
JSONException
- if the string can not be transformed.
-
doubleToString
public static java.lang.String doubleToString(double d)
Produce a string from a double. The string "null" will be returned if the number is not finite.- Parameters:
d
- A double.- Returns:
- A String.
-
getFunctionBody
public static java.lang.String getFunctionBody(java.lang.String function)
Returns the body of a function literal.
-
getFunctionParams
public static java.lang.String getFunctionParams(java.lang.String function)
Returns the params of a function literal.
-
getInnerComponentType
public static java.lang.Class getInnerComponentType(java.lang.Class type)
Returns the inner-most component type of an Array.
-
getMorpherRegistry
public static org.kordamp.ezmorph.MorpherRegistry getMorpherRegistry()
Returns the singleton MorpherRegistry.
-
getProperties
public static java.util.Map getProperties(JSONObject jsonObject)
Creates a Map with all the properties of the JSONObject.
-
getTypeClass
public static java.lang.Class getTypeClass(java.lang.Object obj)
Returns the JSON type.
Values are Object, String, Boolean, Number(subclasses) & JSONFunction.
-
hashCode
public static int hashCode(java.lang.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(java.lang.Class clazz)
Tests if a Class represents an array or Collection.
-
isArray
public static boolean isArray(java.lang.Object obj)
Tests if obj is an array or Collection.
-
isBoolean
public static boolean isBoolean(java.lang.Class clazz)
Tests if Class represents a Boolean or primitive boolean
-
isBoolean
public static boolean isBoolean(java.lang.Object obj)
Tests if obj is a Boolean or primitive boolean
-
isDouble
public static boolean isDouble(java.lang.Class clazz)
Tests if Class represents a primitive double or wrapper.
-
isFunction
public static boolean isFunction(java.lang.Object obj)
Tests if obj is javaScript function.
Obj must be a non-null String and match"^function[ ]?\\(.*\\)[ ]?\\{.*\\}$"
-
isFunctionHeader
public static boolean isFunctionHeader(java.lang.Object obj)
Tests if obj is javaScript function header.
Obj must be a non-null String and match "^function[ ]?\\(.*\\)$"
-
isJavaIdentifier
public static boolean isJavaIdentifier(java.lang.String str)
Returns trus if str represents a valid Java identifier.
-
isNull
public static boolean isNull(java.lang.Object obj)
Tests if the obj is a javaScript null.
-
isNumber
public static boolean isNumber(java.lang.Class clazz)
Tests if Class represents a primitive number or wrapper.
-
isNumber
public static boolean isNumber(java.lang.Object obj)
Tests if obj is a primitive number or wrapper.
-
isObject
public static boolean isObject(java.lang.Object obj)
Tests if obj is not a boolean, number, string or array.
-
isString
public static boolean isString(java.lang.Class clazz)
Tests if Class represents a String or a char
-
isString
public static boolean isString(java.lang.Object obj)
Tests if obj is a String or a char
-
isBlank
public static boolean isBlank(java.lang.String str)
Determines whether a given string is
null
, empty, or only contains whitespace. If it contains anything other than whitespace then the string is not considered to be blank and the method returnsfalse
.- Parameters:
str
- The string to test.- Returns:
true
if the string isnull
, or blank.
-
mayBeJSON
public static boolean mayBeJSON(java.lang.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 java.lang.String numberToString(java.lang.Number n)
Produce a string from a Number.- Parameters:
n
- A Number- Returns:
- A String.
- Throws:
JSONException
- If n is a non-finite number.
-
quote
public static java.lang.String quote(java.lang.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.- Parameters:
string
- A String- Returns:
- A String correctly formatted for insertion in a JSON text.
-
quoteCanonical
public static java.lang.String quoteCanonical(java.lang.String s)
Minimal escape form.
-
stripQuotes
public static java.lang.String stripQuotes(java.lang.String input)
Strips any single-quotes or double-quotes from both sides of the string.
-
hasQuotes
public static boolean hasQuotes(java.lang.String input)
Returns true if the input has single-quotes or double-quotes at both sides.
-
isJsonKeyword
public static boolean isJsonKeyword(java.lang.String input, JsonConfig jsonConfig)
-
testValidity
public static void testValidity(java.lang.Object o)
Throw an exception if the object is an NaN or infinite number.- Parameters:
o
- The object to test.- Throws:
JSONException
- If o is a non-finite number.
-
transformNumber
public static java.lang.Number transformNumber(java.lang.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 java.lang.String valueToString(java.lang.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.- Parameters:
value
- The value to be serialized.- Returns:
- a printable, displayable, transmittable representation of the
object, beginning with
{
(left brace) and ending with}
(right brace). - Throws:
JSONException
- If the value is or contains an invalid number.
-
valueToCanonicalString
public static java.lang.String valueToCanonicalString(java.lang.Object value)
-
valueToString
public static java.lang.String valueToString(java.lang.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.- Parameters:
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.- Returns:
- a printable, displayable, transmittable representation of the
object, beginning with
{
(left brace) and ending with}
(right brace). - Throws:
JSONException
- If the object contains an invalid number.
-
isBigDecimal
private static boolean isBigDecimal(java.lang.Number n)
Finds out if n represents a BigInteger- Returns:
- true if n is instanceOf BigInteger or the literal value can be evaluated as a BigInteger
-
isBigInteger
private static boolean isBigInteger(java.lang.Number n)
Finds out if n represents a BigInteger- Returns:
- true if n is instanceOf BigInteger or the literal value can be evaluated as a BigInteger
-
isDouble
private static boolean isDouble(java.lang.Number n)
Finds out if n represents a Double.- Returns:
- true if n is instanceOf Double or the literal value can be evaluated as a Double.
-
isFloat
private static boolean isFloat(java.lang.Number n)
Finds out if n represents a Float.- Returns:
- true if n is instanceOf Float or the literal value can be evaluated as a Float.
-
isInteger
private static boolean isInteger(java.lang.Number n)
Finds out if n represents an Integer.- Returns:
- true if n is instanceOf Integer or the literal value can be evaluated as an Integer.
-
isLong
private static boolean isLong(java.lang.Number n)
Finds out if n represents a Long.- Returns:
- true if n is instanceOf Long or the literal value can be evaluated as a Long.
-
-