org.pfsw.text.json
Class JsonUtil

java.lang.Object
  extended by org.pfsw.text.json.JsonUtil

public class JsonUtil
extends java.lang.Object

Convenience methods for JavaScript Object Notation (JSON) handling. JSON Web Site


Field Summary
static JsonUtil JU
          The singleton of this class (i.e.
 
Constructor Summary
protected JsonUtil()
           
 
Method Summary
 void appendJSONArray(java.lang.Appendable output, java.lang.Object... objects)
          Appends the given object array to the given output in a valid JSON string representation.
 void appendJSONConvertible(java.lang.Appendable output, org.pfsw.bif.text.IJSONConvertible jsonConvertible)
          Converts the given object to a valid JSON string representation and appends it to the given buffer.
 void appendJSONMap(java.lang.Appendable output, java.util.Map<java.lang.String,java.lang.Object> map)
          Adds the given map to the buffer as JSON representation.
 void appendJSONObject(java.lang.Appendable output, java.lang.Object object)
          Appends the given object to the output as a valid JSON string.
 void appendJsonPair(java.lang.Appendable output, java.lang.String name, java.lang.Object value)
          Appends the given name and value as JSON pair member to the given buffer.
 void appendJSONPair(java.lang.Appendable output, java.lang.String name, java.lang.Object value)
          Appends the given name and value as JSON pair member to the given buffer.
 void appendJsonPair(java.lang.Appendable output, java.lang.String name, java.lang.Object value, boolean compact)
          Appends the given name and value as JSON pair member to the given output.
 void appendJSONString(java.lang.Appendable output, java.lang.String str)
          Appends the given string to the buffer as a valid JSON string literal.
 java.lang.String arrayToJSON(java.lang.Object... objects)
          Converts the given object array to a valid JSON string representation.
 java.lang.String convertToJSON(org.pfsw.bif.text.IJSONConvertible jsonConvertible)
          Converts the given object to a valid JSON string representation.
static JsonUtil current()
          Returns the only instance this class supports (design pattern "Singleton")
 boolean isValidJsonType(java.lang.Class<?> type)
          Returns true if the given class one of the supported JSON object types.
 boolean isValidJsonTypeInstance(java.lang.Object object)
          Returns true if the given object is null or an instance of one of the supported JSON object types.
 java.lang.String mapToJSON(java.util.Map<java.lang.String,java.lang.Object> map)
          Converts the given map to a valid JSON string representation.
 java.lang.String objectToJSONValue(java.lang.Object object)
          Tries to convert the given object to a JSON string.
 java.lang.String toJSONStringLiteral(java.lang.String str)
          Returns the given string as JSON string literal (i.e. enclosed in quotes).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

JU

public static final JsonUtil JU
The singleton of this class (i.e. JsonUtil.current()) which can easily included by static import and the used like JU.toJSONStringLiteral(object).

Constructor Detail

JsonUtil

protected JsonUtil()
Method Detail

current

public static JsonUtil current()
Returns the only instance this class supports (design pattern "Singleton")


convertToJSON

public java.lang.String convertToJSON(org.pfsw.bif.text.IJSONConvertible jsonConvertible)
Converts the given object to a valid JSON string representation.

Parameters:
jsonConvertible - The object to convert.
Returns:
The JSON string representation

arrayToJSON

public java.lang.String arrayToJSON(java.lang.Object... objects)
Converts the given object array to a valid JSON string representation.

Parameters:
objects - The object array to convert.
Returns:
The JSON string representation

mapToJSON

public java.lang.String mapToJSON(java.util.Map<java.lang.String,java.lang.Object> map)
Converts the given map to a valid JSON string representation.

Parameters:
map - The map to convert.
Returns:
The JSON string representation

toJSONStringLiteral

public java.lang.String toJSONStringLiteral(java.lang.String str)
Returns the given string as JSON string literal (i.e. enclosed in quotes).

Parameters:
str - The string to make JSON compatible (might by null)
Returns:
The quoted string or "null" string without quotes for str being null.

appendJSONPair

public void appendJSONPair(java.lang.Appendable output,
                           java.lang.String name,
                           java.lang.Object value)
Appends the given name and value as JSON pair member to the given buffer. It produces a compact output, which means no extra spaces are inserted.

Parameters:
output - The buffer to append to.
name - The name of the pair.
value - The value of the pair.
Throws:
org.pfsw.bif.exceptions.IORuntimeException - if the given output object throws an IOException.

appendJsonPair

public void appendJsonPair(java.lang.Appendable output,
                           java.lang.String name,
                           java.lang.Object value)
Appends the given name and value as JSON pair member to the given buffer. For better readability extra spaces are inserted around the name/value separator.

Parameters:
output - The buffer to append to.
name - The name of the pair.
value - The value of the pair.
Throws:
org.pfsw.bif.exceptions.IORuntimeException - if the given output object throws an IOException.

appendJsonPair

public void appendJsonPair(java.lang.Appendable output,
                           java.lang.String name,
                           java.lang.Object value,
                           boolean compact)
Appends the given name and value as JSON pair member to the given output.

Parameters:
output - The buffer to append to.
name - The name of the pair.
value - The value of the pair.
compact - if true, no spaces are inserted between name and separator and separator and value.
Throws:
org.pfsw.bif.exceptions.IORuntimeException - if the given output object throws an IOException.

appendJSONString

public void appendJSONString(java.lang.Appendable output,
                             java.lang.String str)
Appends the given string to the buffer as a valid JSON string literal. That is, surrounded by quotes.

Throws:
org.pfsw.bif.exceptions.IORuntimeException - if the given output object throws an IOException.

appendJSONConvertible

public void appendJSONConvertible(java.lang.Appendable output,
                                  org.pfsw.bif.text.IJSONConvertible jsonConvertible)
Converts the given object to a valid JSON string representation and appends it to the given buffer.

Parameters:
jsonConvertible - The object to convert and append.
Throws:
org.pfsw.bif.exceptions.IORuntimeException - if the given output object throws an IOException.

appendJSONArray

public void appendJSONArray(java.lang.Appendable output,
                            java.lang.Object... objects)
Appends the given object array to the given output in a valid JSON string representation.

Parameters:
objects - The object array to append.
Throws:
org.pfsw.bif.exceptions.IORuntimeException - if the given output object throws an IOException.

appendJSONObject

public void appendJSONObject(java.lang.Appendable output,
                             java.lang.Object object)
Appends the given object to the output as a valid JSON string.

Throws:
org.pfsw.bif.exceptions.IORuntimeException - if the given output object throws an IOException.

appendJSONMap

public void appendJSONMap(java.lang.Appendable output,
                          java.util.Map<java.lang.String,java.lang.Object> map)
Adds the given map to the buffer as JSON representation.

Example:
{"key1":"value1","key2":"value2","key3":"value3"}

Throws:
org.pfsw.bif.exceptions.IORuntimeException - if the given output object throws an IOException.

objectToJSONValue

public java.lang.String objectToJSONValue(java.lang.Object object)
Tries to convert the given object to a JSON string. If the object is not any of the following then its toString() result will be returned.

Parameters:
object - Any object (even null).
Returns:
A JSON value representing the given object

isValidJsonTypeInstance

public boolean isValidJsonTypeInstance(java.lang.Object object)
Returns true if the given object is null or an instance of one of the supported JSON object types.

Valid types are:


isValidJsonType

public boolean isValidJsonType(java.lang.Class<?> type)
Returns true if the given class one of the supported JSON object types.

Valid types are: