org.apache.shindig.common
Class JsonSerializer

java.lang.Object
  extended by org.apache.shindig.common.JsonSerializer

public final class JsonSerializer
extends Object

Serializes a JSONObject. The methods here are designed to be substantially more CPU and memory efficient than those found in org.json or net.sf.json. In profiling, the performance of both of these libraries has been found to be woefully inadequate for large scale deployments. The append*() methods can be used to serialize directly into an Appendable, such as an output stream. This avoids unnecessary copies to intermediate objects. To reduce output size, null values in json arrays and objects will always be removed.


Method Summary
static void append(Appendable buf, Object value)
          Appends a value to the buffer.
static void appendArray(Appendable buf, Object[] array)
          Appends an array to the buffer.
static void appendCollection(Appendable buf, Collection<?> collection)
          Appends a Collection to the buffer.
static void appendJsonArray(Appendable buf, org.json.JSONArray array)
          Append a JSONArray to the buffer.
static void appendJsonObject(Appendable buf, org.json.JSONObject object)
          Appends a JSONObject to the buffer.
static void appendMap(Appendable buf, Map<String,?> map)
          Appends a Map to the buffer.
static void appendMultimap(Appendable buf, com.google.common.collect.Multimap<String,Object> map)
          Appends a Map to the buffer.
static void appendPojo(Appendable buf, Object pojo)
          Appends a java object using getters
static void appendString(Appendable buf, CharSequence string)
          Appends a string to the buffer.
static String serialize(Collection<?> collection)
          Serializes a Collection as a JSON array.
static String serialize(org.json.JSONArray array)
          Serializes a JSON array.
static String serialize(org.json.JSONObject object)
          Serialize a JSONObject.
static String serialize(Map<String,?> map)
          Serializes a Map as a JSON object.
static String serialize(Object object)
           
static String serialize(Object[] array)
          Serializes an array as a JSON array.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

serialize

public static String serialize(Object object)

serialize

public static String serialize(org.json.JSONObject object)
Serialize a JSONObject. Does not guard against cyclical references.


serialize

public static String serialize(Map<String,?> map)
Serializes a Map as a JSON object. Does not guard against cyclical references.


serialize

public static String serialize(Collection<?> collection)
Serializes a Collection as a JSON array. Does not guard against cyclical references.


serialize

public static String serialize(Object[] array)
Serializes an array as a JSON array. Does not guard against cyclical references


serialize

public static String serialize(org.json.JSONArray array)
Serializes a JSON array. Does not guard against cyclical references


append

public static void append(Appendable buf,
                          Object value)
                   throws IOException
Appends a value to the buffer.

Throws:
IOException - If Appendable.append(char) throws an exception.

appendPojo

public static void appendPojo(Appendable buf,
                              Object pojo)
                       throws IOException
Appends a java object using getters

Throws:
IOException - If Appendable.append(char) throws an exception.

appendArray

public static void appendArray(Appendable buf,
                               Object[] array)
                        throws IOException
Appends an array to the buffer.

Throws:
IOException - If Appendable.append(char) throws an exception.

appendJsonArray

public static void appendJsonArray(Appendable buf,
                                   org.json.JSONArray array)
                            throws IOException
Append a JSONArray to the buffer.

Throws:
IOException - If Appendable.append(char) throws an exception.

appendCollection

public static void appendCollection(Appendable buf,
                                    Collection<?> collection)
                             throws IOException
Appends a Collection to the buffer.

Throws:
IOException - If Appendable.append(char) throws an exception.

appendMap

public static void appendMap(Appendable buf,
                             Map<String,?> map)
                      throws IOException
Appends a Map to the buffer.

Throws:
IOException - If Appendable.append(char) throws an exception.

appendMultimap

public static void appendMultimap(Appendable buf,
                                  com.google.common.collect.Multimap<String,Object> map)
                           throws IOException
Appends a Map to the buffer.

Throws:
IOException - If Appendable.append(char) throws an exception.

appendJsonObject

public static void appendJsonObject(Appendable buf,
                                    org.json.JSONObject object)
                             throws IOException
Appends a JSONObject to the buffer.

Throws:
IOException - If Appendable.append(char) throws an exception.

appendString

public static void appendString(Appendable buf,
                                CharSequence string)
                         throws IOException
Appends a string to the buffer. The string will be JSON encoded and enclosed in quotes.

Throws:
IOException - If Appendable.append(char) throws an exception.


Copyright © 2007-2012. All Rights Reserved.