Package com.aspectran.utils.json
Class JsonBuilder
java.lang.Object
com.aspectran.utils.json.JsonBuilder
A builder for creating JSON string.
ex)
String json = new JsonBuilder(); .object() .put("name", "value") .object("object") .put("name", "value") .endObject() .array("array") .put("value-1") .put("value-2") .endArray() .endObject() .toString();
In the above example, the value of the json variable is:
{"name": "value", "object": {"name": "value"}, "array": ["value-1", "value-2"]}
Note: object() & endObject() are a pair of methods, which means that they should show up at the same time. So does array() & endArray()
Created: 2024. 12. 14.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionapply
(StringifyContext stringifyContext) array()
Begins encoding a new array.Begins encoding a new array with the given name.endArray()
Ends encoding the current array.Ends encoding the current object.indentString
(String indentString) nullWritable
(boolean nullWritable) object()
Begins encoding a new object.Begins encoding a new object with the given name.prettyPrint
(boolean prettyPrint) Put a new value into an array.Put a new name-value pair in an object.void
setStringifyContext
(StringifyContext stringifyContext) toString()
-
Constructor Details
-
JsonBuilder
public JsonBuilder()
-
-
Method Details
-
setStringifyContext
-
apply
-
prettyPrint
-
indentString
-
nullWritable
-
object
Begins encoding a new object.- Returns:
- this builder
-
object
Begins encoding a new object with the given name.- Returns:
- this builder
-
endObject
Ends encoding the current object.- Returns:
- this builder
-
array
Begins encoding a new array.- Returns:
- this builder
-
array
Begins encoding a new array with the given name.- Returns:
- this builder
-
endArray
Ends encoding the current array.- Returns:
- this builder
-
put
Put a new value into an array. If it's not in an array, just put it as a single value.- Parameters:
value
- the new value- Returns:
- this builder
-
put
Put a new name-value pair in an object.- Parameters:
name
- the name of the entry to put in the objectvalue
- the value of the entry to put in the object- Returns:
- this builder
-
toString
-
toJsonString
-