de.lmu.ifi.dbs.elki.application.jsmap
Class JSONBuffer

java.lang.Object
  extended by de.lmu.ifi.dbs.elki.application.jsmap.JSONBuffer

public class JSONBuffer
extends Object

Helper class to produce JSON output. Try hard to ensure validity of the output.


Nested Class Summary
static class JSONBuffer.JSONException
          Class to represent JSON encoding exceptions.
(package private) static class JSONBuffer.ops
          Operations on the stack.
 
Field Summary
(package private)  StringBuffer buffer
          The actual buffer we serialize to
(package private)  Stack<JSONBuffer.ops> stack
          Operations stack for detecting errors
 
Constructor Summary
JSONBuffer(StringBuffer buffer)
          Constructor.
 
Method Summary
private  void addQuotedString(Object cont)
          Add a string in quotes.
private  void addSeparator()
          Add a separator
 JSONBuffer append(double cont)
          Append double
 JSONBuffer append(double[] cont)
          Append double array
 JSONBuffer append(int cont)
          Append integer
 JSONBuffer append(int[] cont)
          Append integer array
 JSONBuffer appendKeyArray(Object key)
          Append a key an start a new array
 JSONBuffer appendKeyHash(Object key)
          Append a key an start a new hash
 JSONBuffer appendKeyValue(Object key, Object val)
          Append a key-value pair as string.
 JSONBuffer appendNewline()
          Append a newline, for human readability
 JSONBuffer appendString(Object cont)
          Append a string in quotes
 JSONBuffer closeArray()
          Close an array context.
 JSONBuffer closeHash()
          Close an hash context.
static String jsonEscapeString(String orig)
          String escaping for JSON
private  void removeSeparator()
          Remove a separator character if present
 JSONBuffer startArray()
          Start an array context.
 JSONBuffer startHash()
          Start an hash context.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

buffer

final StringBuffer buffer
The actual buffer we serialize to


stack

final Stack<JSONBuffer.ops> stack
Operations stack for detecting errors

Constructor Detail

JSONBuffer

public JSONBuffer(StringBuffer buffer)
Constructor.

Parameters:
buffer - Buffer to serialize to
Method Detail

jsonEscapeString

public static String jsonEscapeString(String orig)
String escaping for JSON

Parameters:
orig - Original string
Returns:
JSON safe string

appendString

public JSONBuffer appendString(Object cont)
Append a string in quotes

Parameters:
cont - Contents
Returns:
Buffer for chaining

append

public JSONBuffer append(double cont)
Append double

Parameters:
cont - Contents
Returns:
Buffer for chaining

append

public JSONBuffer append(int cont)
Append integer

Parameters:
cont - Contents
Returns:
Buffer for chaining

append

public JSONBuffer append(double[] cont)
Append double array

Parameters:
cont - Contents
Returns:
Buffer for chaining

append

public JSONBuffer append(int[] cont)
Append integer array

Parameters:
cont - Contents
Returns:
Buffer for chaining

addQuotedString

private void addQuotedString(Object cont)
Add a string in quotes.

Parameters:
cont - Object to put as string

appendKeyValue

public JSONBuffer appendKeyValue(Object key,
                                 Object val)
Append a key-value pair as string.

Parameters:
key - Key to append
val - Value to append
Returns:
Buffer for chaining

appendKeyHash

public JSONBuffer appendKeyHash(Object key)
Append a key an start a new hash

Parameters:
key - Key to append
Returns:
Buffer for chaining

appendKeyArray

public JSONBuffer appendKeyArray(Object key)
Append a key an start a new array

Parameters:
key - Key to append
Returns:
Buffer for chaining

startArray

public JSONBuffer startArray()
Start an array context. Must only be called on an empty or array context.

Returns:
Buffer for chaining

startHash

public JSONBuffer startHash()
Start an hash context. Must only be called on an empty or array context.

Returns:
Buffer for chaining

closeArray

public JSONBuffer closeArray()
Close an array context.

Returns:
Buffer for chaining

closeHash

public JSONBuffer closeHash()
Close an hash context.

Returns:
Buffer for chaining

removeSeparator

private void removeSeparator()
Remove a separator character if present


addSeparator

private void addSeparator()
Add a separator


appendNewline

public JSONBuffer appendNewline()
Append a newline, for human readability

Returns:
Buffer for chaining

Release 0.4.0 (2011-09-20_1324)