Package com.aspectran.core.util.json
Class JsonWriter
- java.lang.Object
-
- com.aspectran.core.util.json.JsonWriter
-
- All Implemented Interfaces:
java.io.Flushable
- Direct Known Subclasses:
ContentsJsonWriter
public class JsonWriter extends java.lang.Object implements java.io.FlushableConverts an object to a JSON formatted string.If pretty-printing is enabled, the JsonWriter will add newlines and indentation to the written data. Pretty-printing is disabled by default.
Created: 2008. 06. 12 PM 8:20:54
- Author:
- Juho Jeong
-
-
Constructor Summary
Constructors Constructor Description JsonWriter(java.io.Writer writer)Instantiates a new JsonWriter.JsonWriter(java.io.Writer writer, boolean prettyPrint)Instantiates a new JsonWriter.JsonWriter(java.io.Writer writer, java.lang.String indentString)Instantiates a new JsonWriter.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes the writer.JsonWritercloseCurlyBracket()Close the open curly bracket.JsonWritercloseSquareBracket()Close the open square bracket.voidflush()protected voidindent()Write a tab character to a character stream.protected voidnextLine()Write a new line character to a character stream.JsonWriteropenCurlyBracket()Open a single curly bracket.JsonWriteropenSquareBracket()Open a single square bracket.static java.lang.Stringstringify(java.lang.Object object)Converts an object to a JSON formatted string.static java.lang.Stringstringify(java.lang.Object object, boolean prettyPrint)Converts an object to a JSON formatted string.static java.lang.Stringstringify(java.lang.Object object, java.lang.String indentString)Converts an object to a JSON formatted string.JsonWriterwrite(java.lang.Object object)Write an object to a character stream.JsonWriterwriteComma()Write a comma character to a character stream.JsonWriterwriteName(java.lang.String name)Writes a key name to a character stream.JsonWriterwriteNull()Write a string "null" to a character stream.JsonWriterwriteValue(java.lang.Boolean value)Writes aBooleanobject to a character stream.JsonWriterwriteValue(java.lang.Number value)Writes aNumberobject to a character stream.JsonWriterwriteValue(java.lang.String value)Writes a string to a character stream.
-
-
-
Constructor Detail
-
JsonWriter
public JsonWriter(java.io.Writer writer)
Instantiates a new JsonWriter. Pretty-printing is disabled by default.- Parameters:
writer- the character-output stream
-
JsonWriter
public JsonWriter(java.io.Writer writer, boolean prettyPrint)Instantiates a new JsonWriter. If pretty-printing is enabled, includes spaces, tabs and new-lines to make the format more readable. The default indentation string is a tab character.- Parameters:
writer- the character-output streamprettyPrint- enables or disables pretty-printing
-
JsonWriter
public JsonWriter(java.io.Writer writer, java.lang.String indentString)Instantiates a new JsonWriter. If pretty-printing is enabled, includes spaces, tabs and new-lines to make the format more readable.- Parameters:
writer- the character-output streamindentString- the string that should be used for indentation when pretty-printing is enabled
-
-
Method Detail
-
write
public JsonWriter write(java.lang.Object object) throws java.io.IOException
Write an object to a character stream.- Parameters:
object- the object to write to a character-output stream.- Returns:
- this JsonWriter
- Throws:
java.io.IOException- if an I/O error has occurred.
-
indent
protected void indent() throws java.io.IOExceptionWrite a tab character to a character stream.- Throws:
java.io.IOException- if an I/O error has occurred
-
writeName
public JsonWriter writeName(java.lang.String name) throws java.io.IOException
Writes a key name to a character stream.- Parameters:
name- the string to write to a character-output stream- Returns:
- this JsonWriter
- Throws:
java.io.IOException- if an I/O error has occurred
-
writeValue
public JsonWriter writeValue(java.lang.String value) throws java.io.IOException
Writes a string to a character stream. Ifvalueis null, write a null string ("").- Parameters:
value- the string to write to a character-output stream- Returns:
- this JsonWriter
- Throws:
java.io.IOException- if an I/O error has occurred
-
writeValue
public JsonWriter writeValue(java.lang.Boolean value) throws java.io.IOException
Writes aBooleanobject to a character stream.- Parameters:
value- aBooleanobject to write to a character-output stream- Returns:
- this JsonWriter
- Throws:
java.io.IOException- if an I/O error has occurred
-
writeValue
public JsonWriter writeValue(java.lang.Number value) throws java.io.IOException
Writes aNumberobject to a character stream.- Parameters:
value- aNumberobject to write to a character-output stream- Returns:
- this JsonWriter
- Throws:
java.io.IOException- if an I/O error has occurred
-
writeNull
public JsonWriter writeNull() throws java.io.IOException
Write a string "null" to a character stream.- Returns:
- this JsonWriter
- Throws:
java.io.IOException- if an I/O error has occurred
-
writeComma
public JsonWriter writeComma() throws java.io.IOException
Write a comma character to a character stream.- Returns:
- this JsonWriter
- Throws:
java.io.IOException- if an I/O error has occurred
-
nextLine
protected void nextLine() throws java.io.IOExceptionWrite a new line character to a character stream.- Throws:
java.io.IOException- if an I/O error has occurred
-
openCurlyBracket
public JsonWriter openCurlyBracket() throws java.io.IOException
Open a single curly bracket.- Returns:
- this JsonWriter
- Throws:
java.io.IOException- if an I/O error has occurred
-
closeCurlyBracket
public JsonWriter closeCurlyBracket() throws java.io.IOException
Close the open curly bracket.- Returns:
- this JsonWriter
- Throws:
java.io.IOException- if an I/O error has occurred
-
openSquareBracket
public JsonWriter openSquareBracket() throws java.io.IOException
Open a single square bracket.- Returns:
- this JsonWriter
- Throws:
java.io.IOException- if an I/O error has occurred
-
closeSquareBracket
public JsonWriter closeSquareBracket() throws java.io.IOException
Close the open square bracket.- Returns:
- this JsonWriter
- Throws:
java.io.IOException- if an I/O error has occurred
-
flush
public void flush() throws java.io.IOException- Specified by:
flushin interfacejava.io.Flushable- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOExceptionCloses the writer.- Throws:
java.io.IOException- if an I/O error has occurred
-
stringify
public static java.lang.String stringify(java.lang.Object object) throws java.io.IOExceptionConverts an object to a JSON formatted string. Pretty-printing is disabled by default.- Parameters:
object- an object to convert to a JSON formatted string- Returns:
- the JSON formatted string
- Throws:
java.io.IOException- if an I/O error has occurred
-
stringify
public static java.lang.String stringify(java.lang.Object object, boolean prettyPrint) throws java.io.IOExceptionConverts an object to a JSON formatted string. If pretty-printing is enabled, includes spaces, tabs and new-lines to make the format more readable. The default indentation string is a tab character.- Parameters:
object- an object to convert to a JSON formatted stringprettyPrint- enables or disables pretty-printing- Returns:
- the JSON formatted string
- Throws:
java.io.IOException- if an I/O error has occurred
-
stringify
public static java.lang.String stringify(java.lang.Object object, java.lang.String indentString) throws java.io.IOExceptionConverts an object to a JSON formatted string. If pretty-printing is enabled, includes spaces, tabs and new-lines to make the format more readable.- Parameters:
object- an object to convert to a JSON formatted stringindentString- the string that should be used for indentation when pretty-printing is enabled- Returns:
- the JSON formatted string
- Throws:
java.io.IOException- if an I/O error has occurred
-
-