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.Flushable
Converts an object to a JSON formatted string. If pretty-printing is enabled, includes spaces, tabs and new-lines to make the format more readable. Pretty-printing is disabled by default. The default indentation string is a tab character.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 void
close()
Closes the writer.JsonWriter
closeCurlyBracket()
Close the open curly bracket.JsonWriter
closeSquareBracket()
Close the open square bracket.void
flush()
protected void
indent()
Write a tab character to a character stream.protected void
nextLine()
Write a new line character to a character stream.JsonWriter
openCurlyBracket()
Open a single curly bracket.JsonWriter
openSquareBracket()
Open a single square bracket.static java.lang.String
stringify(java.lang.Object object)
Converts an object to a JSON formatted string.static java.lang.String
stringify(java.lang.Object object, boolean prettyPrint)
Converts an object to a JSON formatted string.static java.lang.String
stringify(java.lang.Object object, java.lang.String indentString)
Converts an object to a JSON formatted string.JsonWriter
write(java.lang.Object object)
Write an object to a character stream.JsonWriter
writeComma()
Write a comma character to a character stream.JsonWriter
writeName(java.lang.String name)
Writes a key name to a character stream.JsonWriter
writeNull()
Write a string "null" to a character stream.JsonWriter
writeValue(java.lang.Boolean value)
Writes aBoolean
object to a character stream.JsonWriter
writeValue(java.lang.Number value)
Writes aNumber
object to a character stream.JsonWriter
writeValue(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.IOException
Write 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. Ifvalue
is 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 aBoolean
object to a character stream.- Parameters:
value
- aBoolean
object 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 aNumber
object to a character stream.- Parameters:
value
- aNumber
object 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.IOException
Write 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:
flush
in interfacejava.io.Flushable
- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOException
Closes 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.IOException
Converts 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.IOException
Converts 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.IOException
Converts 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
-
-