|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.restfb.json.JsonWriter
public class JsonWriter
JsonWriter provides a quick and convenient way of producing JSON text. The texts produced strictly conform to JSON syntax rules. No whitespace is added, so the results are ready for transmission or storage. Each instance of JsonWriter can produce one JSON text.
A JsonWriter instance provides a value
method for appending
values to the text, and a key
method for adding keys before
values in objects. There are array
and endArray
methods that make and bound array values, and object
and
endObject
methods which make and bound object values. All of
these methods return the JsonWriter instance, permitting a cascade style. For
example,
new JsonWriter(myWriter).object().key("JSON").value("Hello, World!").endObject();which writes
{"JSON":"Hello, World!"}
The first method called must be array
or object
.
There are no methods for adding commas or colons. JsonWriter adds them for
you. Objects and arrays can be nested up to 20 levels deep.
This can sometimes be easier than using a JsonObject to build a string.
Constructor Summary | |
---|---|
JsonWriter(Writer w)
Make a fresh JsonWriter. |
Method Summary | |
---|---|
JsonWriter |
array()
Begin appending a new array. |
JsonWriter |
endArray()
End an array. |
JsonWriter |
endObject()
End an object. |
JsonWriter |
key(String s)
Append a key. |
JsonWriter |
object()
Begin appending a new object. |
JsonWriter |
value(boolean b)
Append either the value true or the value false . |
JsonWriter |
value(double d)
Append a double value. |
JsonWriter |
value(long l)
Append a long value. |
JsonWriter |
value(Object o)
Append an object value. |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public JsonWriter(Writer w)
Method Detail |
---|
public JsonWriter array()
endArray
will be appended to this array. The
endArray
method must be called to mark the array's end.
JsonException
- If the nesting is too deep, or if the object is started in the
wrong place (for example as a key or after the end of the
outermost array or object).public JsonWriter endArray()
array
.
JsonException
- If incorrectly nested.public JsonWriter endObject()
object
.
JsonException
- If incorrectly nested.public JsonWriter key(String s)
s
- A key string.
JsonException
- If the key is out of place. For example, keys do not belong in
arrays or if the key is null.public JsonWriter object()
endObject
will be appended to this object. The
endObject
method must be called to mark the object's end.
JsonException
- If the nesting is too deep, or if the object is started in the
wrong place (for example as a key or after the end of the
outermost array or object).public JsonWriter value(boolean b)
true
or the value false
.
b
- A boolean.
JsonException
public JsonWriter value(double d)
d
- A double.
JsonException
- If the number is not finite.public JsonWriter value(long l)
l
- A long.
JsonException
public JsonWriter value(Object o)
o
- The object to append. It can be null, or a Boolean, Number,
String, JsonObject, or JsonArray, or an object with a
toJSONString() method.
JsonException
- If the value is out of sequence.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |