|
ref_send API 2.17 defensive programming in Java |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.waterken.syntax.json.JSONWriter
public final class JSONWriter
A SAX-like API for generating JSON text.
A client can only output a syntactically correct JSON text, or leave the
JSONWriter
in a detectable error state. The
implementation does not enforce the constraint that names within an
object SHOULD be unique.
For example, to output the JSON text:
{ "title" : "I Can Has Cheezburger?", "src" : { "@" : "http://www.example.com/image/481989943" }, "height" : 125, "width" : 100, "tags" : [ "lolcat", "food" ], "score" : 9.5 }
, write code:
final Writer text = … final JSONWriter top = JSONWriter.make(text); final JSONWriter.ObjectWriter o = top.startObject(); o.startMember("title").writeString("I Can Has Cheezburger?"); o.startMember("src").writeLink("http://www.example.com/image/481989943"); o.startMember("height").writeInt(125); o.startMember("width").writeInt(100); final JSONWriter.ArrayWriter tags = o.startMember("tags").startArray(); tags.startElement().writeString("lolcat"); tags.startElement().writeString("food"); tags.finish(); o.startMember("score").writeDouble(9.5); o.finish(); if (!top.isWritten()) { throw new NullPointerException(); } text.flush(); text.close();
An invalid sequence of calls to this API will cause a
NullPointerException
to be thrown. For example, calling
writeString() twice in succession will throw a NullPointerException
.
Nested Class Summary | |
---|---|
class |
JSONWriter.ArrayWriter
A JSON array writer. |
class |
JSONWriter.ObjectWriter
A JSON object writer. |
Field Summary | |
---|---|
static long |
maxMagnitude
maximum magnitude of a JavaScript number: 9007199254740992L |
static java.lang.ArithmeticException |
NaN
Signals a number that cannot be represented in JavaScript. |
Method Summary | |
---|---|
boolean |
isWritten()
|
static JSONWriter |
make(java.io.Writer out)
Constructs a JSON writer. |
JSONWriter.ArrayWriter |
startArray()
|
JSONWriter.ObjectWriter |
startObject()
|
void |
writeBoolean(boolean value)
|
void |
writeDouble(double value)
|
void |
writeFloat(float value)
|
void |
writeInt(int value)
|
void |
writeLink(java.lang.String URL)
|
void |
writeLong(long value)
|
void |
writeNull()
|
void |
writeString(java.lang.String value)
|
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final long maxMagnitude
public static final java.lang.ArithmeticException NaN
Method Detail |
---|
public static JSONWriter make(java.io.Writer out)
out
- UTF-8 output streampublic boolean isWritten()
true
if a single JSON value was successfully written,
else false
public JSONWriter.ObjectWriter startObject() throws java.io.IOException
java.io.IOException
public JSONWriter.ArrayWriter startArray() throws java.io.IOException
java.io.IOException
public void writeLink(java.lang.String URL) throws java.io.IOException
java.io.IOException
public void writeNull() throws java.io.IOException
java.io.IOException
public void writeBoolean(boolean value) throws java.io.IOException
java.io.IOException
public void writeInt(int value) throws java.io.IOException
java.io.IOException
public void writeLong(long value) throws java.lang.ArithmeticException, java.io.IOException
java.lang.ArithmeticException
java.io.IOException
public void writeFloat(float value) throws java.lang.ArithmeticException, java.io.IOException
java.lang.ArithmeticException
java.io.IOException
public void writeDouble(double value) throws java.lang.ArithmeticException, java.io.IOException
java.lang.ArithmeticException
java.io.IOException
public void writeString(java.lang.String value) throws java.io.IOException
java.io.IOException
|
ref_send API 2.17 defensive programming in Java |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Copyright 1998-2009 Waterken Inc. under the terms of the MIT X license.