Package uk.ac.starlink.ttools.taplint
Class JsonWriter
- java.lang.Object
-
- uk.ac.starlink.ttools.taplint.JsonWriter
-
public class JsonWriter extends java.lang.ObjectOutputs 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
Constructors Constructor Description JsonWriter()Constructor with default properties.JsonWriter(int indent, boolean spacer)Custom constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.StringgetIndent(int level)Returns prepended whitespace.java.lang.StringjsonPair(java.lang.String key, java.lang.Object value, int level, boolean isPositioned)Serialises a key-value pair to JSON.java.lang.StringtoJson(java.lang.Object item)Converts an item to JSON.voidtoJson(java.lang.StringBuffer sbuf, java.lang.Object item, int level, boolean isPositioned)Recursive method which does the work for conversion.
-
-
-
Method Detail
-
toJson
public java.lang.String toJson(java.lang.Object item)
Converts an item to JSON.- Parameters:
item- suitable object- Returns:
- JSON representation
-
toJson
public void toJson(java.lang.StringBuffer sbuf, java.lang.Object item, int level, boolean isPositioned)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
public java.lang.String getIndent(int level)
Returns prepended whitespace.- Parameters:
level- indentation level- Returns:
- string to prepend
-
jsonPair
public java.lang.String jsonPair(java.lang.String key, java.lang.Object value, int level, boolean isPositioned)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
-
-