http://www.json.org/
After discussing the pros and cons of (A) creating a Map of parameters and serialising that to a JSON string; and (B) programmatically calling methods on a ResponseWriter-esque helper object to generate the JSON string; we've chosen (B). Here is the suggestion for how to implement that idea.
Say we want to output a string that creates a javascript map, with a nested map, as well as String, integer and boolean values:
{a:
{b:true,c:'hi'}
,d:25}
Re-written with more white-space, for greater clarity:
{
a:
{
b:true,
c:'hi'
}
,
d:25
}
We could make use of a utility object, that provides an API similar to StringBuilder, but javascript variable oriented, instead of char oriented:
JSONBuilder builder = new JSONBuilder();
builder.beginMap().
beginMap("a").
entry("b", true).
entry("c", "hi").
endMap().
entry("d", 25).
endMap();
String json = builder.toString();
Or, slightly more succinctly:
String json = JSONBuilder.create().
beginMap().
beginMap("a").
entry("b", true).
entry("c", "hi").
endMap().
entry("d", 25).
endMap().toString();
All the methods in JSONBuilder return the reference to itself, to simplify further operations on it. JSONBuilder.create() just makes a new instance. Every JSONBuilder internally uses a StringBuilder, whose toString() is returned by JSONBuilder.toString().
beginMap() just appends: "{"
beginMap(String key) appends: key + "
{"
endMap() appends: "}
"
There would be versions of entry(String key, X value) that take: int, long, float, double, boolean, String. They basically append: key + ":" + value. The String one adds quotes and does JSON string escaping, like how the link above describes.
beginMap(String key) and entry(String key, X value) need to use some internal state for managing when to append a comma.
http://www.json.org/
After discussing the pros and cons of (A) creating a Map of parameters and serialising that to a JSON string; and (B) programmatically calling methods on a ResponseWriter-esque helper object to generate the JSON string; we've chosen (B). Here is the suggestion for how to implement that idea.
Say we want to output a string that creates a javascript map, with a nested map, as well as String, integer and boolean values:
{a:
{b:true,c:'hi'},d:25}
Re-written with more white-space, for greater clarity:
{
{ b:true, c:'hi' }a:
,
d:25
}
We could make use of a utility object, that provides an API similar to StringBuilder, but javascript variable oriented, instead of char oriented:
JSONBuilder builder = new JSONBuilder();
builder.beginMap().
beginMap("a").
entry("b", true).
entry("c", "hi").
endMap().
entry("d", 25).
endMap();
String json = builder.toString();
Or, slightly more succinctly:
String json = JSONBuilder.create().
beginMap().
beginMap("a").
entry("b", true).
entry("c", "hi").
endMap().
entry("d", 25).
endMap().toString();
All the methods in JSONBuilder return the reference to itself, to simplify further operations on it. JSONBuilder.create() just makes a new instance. Every JSONBuilder internally uses a StringBuilder, whose toString() is returned by JSONBuilder.toString().
beginMap() just appends: "{"
{" endMap() appends: "}beginMap(String key) appends: key + "
"
There would be versions of entry(String key, X value) that take: int, long, float, double, boolean, String. They basically append: key + ":" + value. The String one adds quotes and does JSON string escaping, like how the link above describes.
beginMap(String key) and entry(String key, X value) need to use some internal state for managing when to append a comma.