org.qtunes.core.util
Class JSONSerializer

java.lang.Object
  extended by org.qtunes.core.util.JSONSerializer
Direct Known Subclasses:
ControlJSONSerializer

public class JSONSerializer
extends java.lang.Object

Class to read/write objects as JSON. Can be overridden to support custom serialization. For writing this should involve overriding the getCustomWrite(java.lang.Object) method like so:

 public Object getCustomWrite(Object o) {
     if (o instanceof Widget) {
         Map m = new HashMap();
         m.put("type", "widget");
         m.put("name", widget.getName());
         return m;
     } else {
         return super.getCustomWrite(o);
     }
 }
 
and for reading the getCustomRead(java.util.Map) methods:
 public Object getCustomRead(Map m) {
     if ("widget".equals(m.get("type"))) {
         return Widget.getByName((String)m.get("name"));
     } else {
         return super.getCustomRead();
     }
 }
 


Constructor Summary
JSONSerializer()
           
 
Method Summary
protected  java.lang.Object getCustomRead(java.util.List list)
          Subclasses may override this method to return a more specific type of object than the List specified.
protected  java.lang.Object getCustomRead(java.util.Map map)
          Subclasses may override this method to return a more specific type of object than the Map specified.
 java.lang.Object getCustomWrite(java.lang.Object object)
          Get an object that can be serialized to represent the specified Object.
 java.lang.Object read(java.io.Reader reader)
          Parse a JSON serialized object from the Reader and return the Object it represents
 java.lang.Object read(java.lang.String s)
          Parse a JSON serialized String and return the Object it represents
 java.lang.Object readAsProbableString(java.lang.String s)
           
protected  java.lang.Object readCustomToken(int c, java.io.Reader reader)
           
 void setLaxKeyQuoting(boolean lax)
           
 java.lang.String write(java.lang.Object object)
          Serialize the object and return the serialized version as a String
 void write(java.lang.Object o, java.lang.Appendable sb)
          Serialize the object and write the serialized version to the specified Appendable
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JSONSerializer

public JSONSerializer()
Method Detail

setLaxKeyQuoting

public void setLaxKeyQuoting(boolean lax)

write

public java.lang.String write(java.lang.Object object)
Serialize the object and return the serialized version as a String

Parameters:
object - the Object to serialize

write

public void write(java.lang.Object o,
                  java.lang.Appendable sb)
           throws java.io.IOException
Serialize the object and write the serialized version to the specified Appendable

Parameters:
o - the Object to serialize
Throws:
java.io.IOException

getCustomWrite

public java.lang.Object getCustomWrite(java.lang.Object object)
                                throws java.lang.IllegalArgumentException
Get an object that can be serialized to represent the specified Object. This method is called when there is no default way to serialize the parameter - by default it throws an Exception saying the object cannot be serialized. It can be overridden by subclasses.

Parameters:
object - an unserializable Object
Returns:
an Object which can be used in it's place
Throws:
java.lang.IllegalArgumentException

readAsProbableString

public java.lang.Object readAsProbableString(java.lang.String s)
                                      throws java.io.IOException
Throws:
java.io.IOException

read

public java.lang.Object read(java.lang.String s)
                      throws java.io.IOException
Parse a JSON serialized String and return the Object it represents

Throws:
java.io.IOException

read

public java.lang.Object read(java.io.Reader reader)
                      throws java.io.IOException
Parse a JSON serialized object from the Reader and return the Object it represents

Throws:
java.io.IOException

getCustomRead

protected java.lang.Object getCustomRead(java.util.Map map)
Subclasses may override this method to return a more specific type of object than the Map specified. Roughly the opposite of getCustomWrite(java.lang.Object).

Parameters:
map - a generic Map
Returns:
the Object that the Map represents.

getCustomRead

protected java.lang.Object getCustomRead(java.util.List list)
Subclasses may override this method to return a more specific type of object than the List specified. Roughly the opposite of getCustomWrite(java.lang.Object).

Parameters:
list - a generic List
Returns:
the Object that the Map represents.

readCustomToken

protected java.lang.Object readCustomToken(int c,
                                           java.io.Reader reader)