Class JSONUtils
java.lang.Object
net.sf.json.util.JSONUtils
Provides useful methods on java objects and JSON values.
- Version:
- 7
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringConstant for char "private static final Stringprivate static final Stringprivate static final Stringprivate static final Stringprivate static final Stringprivate static final net.sf.ezmorph.MorpherRegistrystatic final StringConstant for char ' -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringTransforms the string into a valid Java Identifier.
The default strategy is JavaIdentifierTransformer.NOOPstatic StringconvertToJavaIdentifier(String key, JsonConfig jsonConfig) Transforms the string into a valid Java Identifier.
The default strategy is JavaIdentifierTransformer.NOOPstatic StringdoubleToString(double d) Produce a string from a double.static StringgetFunctionBody(String function) Returns the body of a function literal.static StringgetFunctionParams(String function) Returns the params of a function literal.static ClassgetInnerComponentType(Class type) Returns the inner-most component type of an Array.static net.sf.ezmorph.MorpherRegistryReturns the singleton MorpherRegistry.static MapgetProperties(JSONObject jsonObject) Creates a Map with all the properties of the JSONObject.static ClassgetTypeClass(Object obj) Returns the JSON type.
Values are Object, String, Boolean, Number(subclasses) & JSONFunction.static intReturns 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 booleanReturns true if the input has single-quotes or double-quotes at both sides.static booleanTests if a Class represents an array or Collection.static booleanTests if obj is an array or Collection.private static booleanFinds out if n represents a BigIntegerprivate static booleanFinds out if n represents a BigIntegerstatic booleanTests if Class represents a Boolean or primitive booleanstatic booleanTests if obj is a Boolean or primitive booleanstatic booleanTests if Class represents a primitive double or wrapper.private static booleanFinds out if n represents a Double.private static booleanFinds out if n represents a Float.static booleanisFunction(Object obj) Tests if obj is javaScript function.
Obj must be a non-null String and match"^function[ ]?\\(.*\\)[ ]?\\{.*\\}$" static booleanisFunctionHeader(Object obj) Tests if obj is javaScript function header.
Obj must be a non-null String and match "^function[ ]?\\(.*\\)$"private static booleanFinds out if n represents an Integer.static booleanisJavaIdentifier(String str) Returns trus if str represents a valid Java identifier.static booleanisJsonKeyword(String input, JsonConfig jsonConfig) private static booleanFinds out if n represents a Long.static booleanTests if the obj is a javaScript null.static booleanTests if Class represents a primitive number or wrapper.static booleanTests if obj is a primitive number or wrapper.static booleanTests if obj is not a boolean, number, string or array.static booleanTests if Class represents a String or a charstatic booleanTests if obj is a String or a charstatic booleanTests 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.DynaBeannewDynaBean(JSONObject jsonObject) Creates a new MorphDynaBean from a JSONObject.static org.apache.commons.beanutils.DynaBeannewDynaBean(JSONObject jsonObject, JsonConfig jsonConfig) Creates a new MorphDynaBean from a JSONObject.static StringProduce a string from a Number.static StringProduce a string in double quotes with backslash sequences in all the right places.static StringMinimal escape form.static StringstripQuotes(String input) Strips any single-quotes or double-quotes from both sides of the string.static voidThrow an exception if the object is an NaN or infinite number.static NumbertransformNumber(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 StringvalueToCanonicalString(Object value) static StringvalueToString(Object value) Make a JSON text of an Object value.static StringvalueToString(Object value, int indentFactor, int indent) Make a prettyprinted JSON text of an object value.
-
Field Details
-
DOUBLE_QUOTE
-
SINGLE_QUOTE
-
FUNCTION_BODY_PATTERN
- See Also:
-
FUNCTION_HEADER_PATTERN
- See Also:
-
FUNCTION_PARAMS_PATTERN
- See Also:
-
FUNCTION_PATTERN
- See Also:
-
FUNCTION_PREFIX
- See Also:
-
morpherRegistry
private static final net.sf.ezmorph.MorpherRegistry morpherRegistry
-
-
Constructor Details
-
JSONUtils
private JSONUtils()
-
-
Method Details
-
convertToJavaIdentifier
Transforms the string into a valid Java Identifier.
The default strategy is JavaIdentifierTransformer.NOOP- Throws:
JSONException- if the string can not be transformed.
-
convertToJavaIdentifier
Transforms the string into a valid Java Identifier.
The default strategy is JavaIdentifierTransformer.NOOP- Throws:
JSONException- if the string can not be transformed.
-
doubleToString
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
-
getFunctionParams
-
getInnerComponentType
-
getMorpherRegistry
public static net.sf.ezmorph.MorpherRegistry getMorpherRegistry()Returns the singleton MorpherRegistry. -
getProperties
Creates a Map with all the properties of the JSONObject. -
getTypeClass
-
hashCode
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
Tests if a Class represents an array or Collection. -
isArray
Tests if obj is an array or Collection. -
isBoolean
Tests if Class represents a Boolean or primitive boolean -
isBoolean
Tests if obj is a Boolean or primitive boolean -
isDouble
Tests if Class represents a primitive double or wrapper. -
isFunction
Tests if obj is javaScript function.
Obj must be a non-null String and match"^function[ ]?\\(.*\\)[ ]?\\{.*\\}$" -
isFunctionHeader
Tests if obj is javaScript function header.
Obj must be a non-null String and match "^function[ ]?\\(.*\\)$" -
isJavaIdentifier
Returns trus if str represents a valid Java identifier. -
isNull
Tests if the obj is a javaScript null. -
isNumber
Tests if Class represents a primitive number or wrapper. -
isNumber
Tests if obj is a primitive number or wrapper. -
isObject
Tests if obj is not a boolean, number, string or array. -
isString
Tests if Class represents a String or a char -
isString
Tests if obj is a String or a char -
mayBeJSON
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
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
Produce a string from a Number.- Parameters:
n- A Number- Returns:
- A String.
- Throws:
JSONException- If n is a non-finite number.
-
quote
Produce a string in double quotes with backslash sequences in all the right places. A backslash will be inserted within invalid input: '<'/, 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
-
stripQuotes
-
hasQuotes
Returns true if the input has single-quotes or double-quotes at both sides. -
isJsonKeyword
-
testValidity
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
-
valueToString
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
-
valueToString
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
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
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
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
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
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
Finds out if n represents a Long.- Returns:
- true if n is instanceOf Long or the literal value can be evaluated as a Long.
-