net.sf.json
Class JSONUtils

java.lang.Object
  extended by net.sf.json.JSONUtils

public final class JSONUtils
extends java.lang.Object

Provides useful methods on java objects.

Version:
4
Author:
Andres Almiray

Method Summary
static java.lang.String doubleToString(double d)
          Produce a string from a double.
static int[] getDimensions(JSONArray jsonArray)
          Returns the number of dimensions suited for a java array.
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)
           
static net.sf.ezmorph.MorpherRegistry getMorpherRegistry()
           
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.
static boolean isArray(java.lang.Object obj)
          Tests if obj is an array or Collection.
static boolean isBoolean(java.lang.Object obj)
          Tests if obj is a Boolean or primitive boolean
static boolean isFunction(java.lang.Object obj)
          Tests if obj is javaScript function.
static boolean isFunctionHeader(java.lang.Object obj)
          Tests if obj is javaScript function header.
static boolean isNull(java.lang.Object obj)
          Tests if the obj is a javaScript null.
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.Object obj)
          Tests if obj is a String or a char
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 void testValidity(java.lang.Object o)
          Throw an exception if the object is an NaN or infinite number.
static java.lang.Object[] toObject(char[] array)
          Converts an array of primitive chars to objects.
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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

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.

getDimensions

public static int[] getDimensions(JSONArray jsonArray)
Returns the number of dimensions suited for a java array.


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)

getMorpherRegistry

public static net.sf.ezmorph.MorpherRegistry getMorpherRegistry()

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.


isArray

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


isBoolean

public static boolean isBoolean(java.lang.Object obj)
Tests if obj is a Boolean or primitive boolean


isFunction

public static boolean isFunction(java.lang.Object obj)
Tests if obj is javaScript function.
Obj must ba 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 ba a non-null String and match "^function[ ]?\\(.\*\)$"


isNull

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


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.Object obj)
Tests if obj is a String or a char


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 CAUTION: if string represents a javascript function, translation of characters will not take place. This will produce a non-conformant JSON text.

Parameters:
string - A String
Returns:
A String correctly formatted for insertion in a JSON text.

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.

toObject

public static java.lang.Object[] toObject(char[] array)
Converts an array of primitive chars to objects.

This method is not in ArrayUtils. (commons-lang 2.1)

This method returns null for a null input array.

Parameters:
array - a char array
Returns:
a Character array, null if null array input

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.

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.


Copyright © 2006 null. All Rights Reserved.