Package uk.ac.starlink.ttools.taplint
Class JsonWriter
java.lang.Object
uk.ac.starlink.ttools.taplint.JsonWriter
Outputs an object as JSON.
Classes List, Map, Number and Boolean are recognised.
Arrays are not.
If there is a non-string where a string is required, it's just forced
to a string by calling its toString method.
- Since:
- 23 Oct 2016
- Author:
- Mark Taylor
-
Constructor Summary
ConstructorsConstructorDescriptionConstructor with default properties.JsonWriter
(int indent, boolean spacer) Custom constructor. -
Method Summary
Modifier and TypeMethodDescriptiongetIndent
(int level) Returns prepended whitespace.Serialises a key-value pair to JSON.Converts an item to JSON.void
toJson
(StringBuffer sbuf, Object item, int level, boolean isPositioned) Recursive method which does the work for conversion.
-
Constructor Details
-
JsonWriter
public JsonWriter()Constructor with default properties. -
JsonWriter
public JsonWriter(int indent, boolean spacer) Custom constructor.- Parameters:
indent
- number of characters indent per levelspacer
- whether to put spaces inside brackets
-
-
Method Details
-
toJson
Converts an item to JSON.- Parameters:
item
- suitable object- Returns:
- JSON representation
-
toJson
Recursive method which does the work for conversion. If possible, call this method withisPositioned=false
.- Parameters:
sbuf
- string buffer to append result toitem
- object to convertlevel
- current indentation levelisPositioned
- true if output should be direct to sbuf, false if it needs a newline plus indentation first
-
getIndent
Returns prepended whitespace.- Parameters:
level
- indentation level- Returns:
- string to prepend
-
jsonPair
Serialises a key-value pair to JSON.- Parameters:
key
- key stringvalue
- value objectlevel
- indentation levelisPositioned
- true if output should be direct to sbuf, false if it needs a newline plus indentation first- Returns:
- pair representation
-