org.gridkit.jvmtool.util.json
Class JsonWriter

java.lang.Object
  extended by org.gridkit.jvmtool.util.json.JsonWriter
All Implemented Interfaces:
JsonStreamWriter

public class JsonWriter
extends Object
implements JsonStreamWriter


Constructor Summary
JsonWriter(Writer writer)
           
 
Method Summary
 void flush()
           
 void writeArrayFieldStart(String fieldName)
          Convenience method for outputting a field entry ("member") (that will contain a JSON Array value), and the START_ARRAY marker.
 void writeBoolean(boolean state)
          Method for outputting literal Json boolean value (one of Strings 'true' and 'false').
 void writeBooleanField(String fieldName, boolean value)
          Convenience method for outputting a field entry ("member") that has a boolean value.
 void writeEndArray()
          Method for writing closing marker of a JSON Array value (character ']'; plus possible white space decoration if pretty-printing is enabled).
 void writeEndObject()
          Method for writing closing marker of a JSON Object value (character '}'; plus possible white space decoration if pretty-printing is enabled).
 void writeFieldName(String name)
          Method for writing a field name (JSON String surrounded by double quotes: syntactically identical to a JSON String value), possibly decorated by white space if pretty-printing is enabled.
 void writeNull()
          Method for outputting literal Json null value.
 void writeNullField(String fieldName)
          Convenience method for outputting a field entry ("member") that has JSON literal value null.
 void writeNumber(BigDecimal dec)
          Method for outputting indicate Json numeric value.
 void writeNumber(BigInteger v)
          Method for outputting given value as Json number.
 void writeNumber(double d)
          Method for outputting indicate Json numeric value.
 void writeNumber(float f)
          Method for outputting indicate Json numeric value.
 void writeNumber(int v)
          Method for outputting given value as Json number.
 void writeNumber(long v)
          Method for outputting given value as Json number.
 void writeNumber(String encodedValue)
          Write method that can be used for custom numeric types that can not be (easily?) converted to "standard" Java number types.
 void writeNumberField(String fieldName, BigDecimal value)
          Convenience method for outputting a field entry ("member") that has the specified numeric value.
 void writeNumberField(String fieldName, double value)
          Convenience method for outputting a field entry ("member") that has the specified numeric value.
 void writeNumberField(String fieldName, float value)
          Convenience method for outputting a field entry ("member") that has the specified numeric value.
 void writeNumberField(String fieldName, int value)
          Convenience method for outputting a field entry ("member") that has the specified numeric value.
 void writeNumberField(String fieldName, long value)
          Convenience method for outputting a field entry ("member") that has the specified numeric value.
 void writeObjectFieldStart(String fieldName)
          Convenience method for outputting a field entry ("member") (that will contain a JSON Object value), and the START_OBJECT marker.
 void writeRaw(char c)
          Method that will force generator to copy input text verbatim with no modifications (including that no escaping is done and no separators are added even if context [array, object] would otherwise require such).
 void writeRaw(char[] text, int offset, int len)
          Method that will force generator to copy input text verbatim with no modifications (including that no escaping is done and no separators are added even if context [array, object] would otherwise require such).
 void writeRaw(String text)
          Method that will force generator to copy input text verbatim with no modifications (including that no escaping is done and no separators are added even if context [array, object] would otherwise require such).
 void writeRaw(String text, int offset, int len)
          Method that will force generator to copy input text verbatim with no modifications (including that no escaping is done and no separators are added even if context [array, object] would otherwise require such).
 void writeRawUTF8String(byte[] text, int offset, int length)
          Method similar to JsonStreamWriter.writeString(String) but that takes as its input a UTF-8 encoded String that is to be output as-is, without additional escaping (type of which depends on data format; backslashes for JSON).
 void writeRawValue(char[] text, int offset, int len)
           
 void writeRawValue(String text)
          Method that will force generator to copy input text verbatim without any modifications, but assuming it must constitute a single legal JSON value (number, string, boolean, null, Array or List).
 void writeRawValue(String text, int offset, int len)
           
 void writeStartArray()
          Method for writing starting marker of a JSON Array value (character '['; plus possible white space decoration if pretty-printing is enabled).
 void writeStartObject()
          Method for writing starting marker of a JSON Object value (character '{'; plus possible white space decoration if pretty-printing is enabled).
 void writeString(char[] text, int offset, int len)
          Method for outputting a String value.
 void writeString(String text)
          Method for outputting a String value.
 void writeStringField(String fieldName, String value)
          Convenience method for outputting a field entry ("member") that has a String value.
 void writeUTF8String(byte[] text, int offset, int length)
          Method similar to JsonStreamWriter.writeString(String) but that takes as its input a UTF-8 encoded String which has not been escaped using whatever escaping scheme data format requires (for JSON that is backslash-escaping for control characters and double-quotes; for other formats something else).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JsonWriter

public JsonWriter(Writer writer)
Method Detail

flush

public void flush()

writeStartArray

public void writeStartArray()
                     throws IOException,
                            JsonGenerationException
Description copied from interface: JsonStreamWriter
Method for writing starting marker of a JSON Array value (character '['; plus possible white space decoration if pretty-printing is enabled).

Array values can be written in any context where values are allowed: meaning everywhere except for when a field name is expected.

Specified by:
writeStartArray in interface JsonStreamWriter
Throws:
IOException
JsonGenerationException

writeEndArray

public void writeEndArray()
                   throws IOException,
                          JsonGenerationException
Description copied from interface: JsonStreamWriter
Method for writing closing marker of a JSON Array value (character ']'; plus possible white space decoration if pretty-printing is enabled).

Marker can be written if the innermost structured type is Array.

Specified by:
writeEndArray in interface JsonStreamWriter
Throws:
IOException
JsonGenerationException

writeStartObject

public void writeStartObject()
                      throws IOException,
                             JsonGenerationException
Description copied from interface: JsonStreamWriter
Method for writing starting marker of a JSON Object value (character '{'; plus possible white space decoration if pretty-printing is enabled).

Object values can be written in any context where values are allowed: meaning everywhere except for when a field name is expected.

Specified by:
writeStartObject in interface JsonStreamWriter
Throws:
IOException
JsonGenerationException

writeEndObject

public void writeEndObject()
                    throws IOException,
                           JsonGenerationException
Description copied from interface: JsonStreamWriter
Method for writing closing marker of a JSON Object value (character '}'; plus possible white space decoration if pretty-printing is enabled).

Marker can be written if the innermost structured type is Object, and the last written event was either a complete value, or START-OBJECT marker (see JSON specification for more details).

Specified by:
writeEndObject in interface JsonStreamWriter
Throws:
IOException
JsonGenerationException

writeFieldName

public void writeFieldName(String name)
                    throws IOException,
                           JsonGenerationException
Description copied from interface: JsonStreamWriter
Method for writing a field name (JSON String surrounded by double quotes: syntactically identical to a JSON String value), possibly decorated by white space if pretty-printing is enabled.

Field names can only be written in Object context (check out JSON specification for details), when field name is expected (field names alternate with values).

Specified by:
writeFieldName in interface JsonStreamWriter
Throws:
IOException
JsonGenerationException

writeString

public void writeString(String text)
                 throws IOException,
                        JsonGenerationException
Description copied from interface: JsonStreamWriter
Method for outputting a String value. Depending on context this means either array element, (object) field value or a stand alone String; but in all cases, String will be surrounded in double quotes, and contents will be properly escaped as required by JSON specification.

Specified by:
writeString in interface JsonStreamWriter
Throws:
IOException
JsonGenerationException

writeString

public void writeString(char[] text,
                        int offset,
                        int len)
                 throws IOException,
                        JsonGenerationException
Description copied from interface: JsonStreamWriter
Method for outputting a String value. Depending on context this means either array element, (object) field value or a stand alone String; but in all cases, String will be surrounded in double quotes, and contents will be properly escaped as required by JSON specification.

Specified by:
writeString in interface JsonStreamWriter
Throws:
IOException
JsonGenerationException

writeRawUTF8String

public void writeRawUTF8String(byte[] text,
                               int offset,
                               int length)
                        throws IOException,
                               JsonGenerationException
Description copied from interface: JsonStreamWriter
Method similar to JsonStreamWriter.writeString(String) but that takes as its input a UTF-8 encoded String that is to be output as-is, without additional escaping (type of which depends on data format; backslashes for JSON). However, quoting that data format requires (like double-quotes for JSON) will be added around the value if and as necessary.

Note that some backends may choose not to support this method: for example, if underlying destination is a Writer using this method would require UTF-8 decoding. If so, implementation may instead choose to throw a UnsupportedOperationException due to ineffectiveness of having to decode input.

Specified by:
writeRawUTF8String in interface JsonStreamWriter
Throws:
IOException
JsonGenerationException

writeUTF8String

public void writeUTF8String(byte[] text,
                            int offset,
                            int length)
                     throws IOException,
                            JsonGenerationException
Description copied from interface: JsonStreamWriter
Method similar to JsonStreamWriter.writeString(String) but that takes as its input a UTF-8 encoded String which has not been escaped using whatever escaping scheme data format requires (for JSON that is backslash-escaping for control characters and double-quotes; for other formats something else). This means that textual JSON backends need to check if value needs JSON escaping, but otherwise can just be copied as is to output. Also, quoting that data format requires (like double-quotes for JSON) will be added around the value if and as necessary.

Note that some backends may choose not to support this method: for example, if underlying destination is a Writer using this method would require UTF-8 decoding. In this case generator implementation may instead choose to throw a UnsupportedOperationException due to ineffectiveness of having to decode input.

Specified by:
writeUTF8String in interface JsonStreamWriter
Throws:
IOException
JsonGenerationException

writeRaw

public void writeRaw(String text)
              throws IOException,
                     JsonGenerationException
Description copied from interface: JsonStreamWriter
Method that will force generator to copy input text verbatim with no modifications (including that no escaping is done and no separators are added even if context [array, object] would otherwise require such). If such separators are desired, use JsonStreamWriter.writeRawValue(String) instead.

Note that not all generator implementations necessarily support such by-pass methods: those that do not will throw UnsupportedOperationException.

Specified by:
writeRaw in interface JsonStreamWriter
Throws:
IOException
JsonGenerationException

writeRaw

public void writeRaw(String text,
                     int offset,
                     int len)
              throws IOException,
                     JsonGenerationException
Description copied from interface: JsonStreamWriter
Method that will force generator to copy input text verbatim with no modifications (including that no escaping is done and no separators are added even if context [array, object] would otherwise require such). If such separators are desired, use JsonStreamWriter.writeRawValue(String) instead.

Note that not all generator implementations necessarily support such by-pass methods: those that do not will throw UnsupportedOperationException.

Specified by:
writeRaw in interface JsonStreamWriter
Throws:
IOException
JsonGenerationException

writeRaw

public void writeRaw(char[] text,
                     int offset,
                     int len)
              throws IOException,
                     JsonGenerationException
Description copied from interface: JsonStreamWriter
Method that will force generator to copy input text verbatim with no modifications (including that no escaping is done and no separators are added even if context [array, object] would otherwise require such). If such separators are desired, use JsonStreamWriter.writeRawValue(String) instead.

Note that not all generator implementations necessarily support such by-pass methods: those that do not will throw UnsupportedOperationException.

Specified by:
writeRaw in interface JsonStreamWriter
Throws:
IOException
JsonGenerationException

writeRaw

public void writeRaw(char c)
              throws IOException,
                     JsonGenerationException
Description copied from interface: JsonStreamWriter
Method that will force generator to copy input text verbatim with no modifications (including that no escaping is done and no separators are added even if context [array, object] would otherwise require such). If such separators are desired, use JsonStreamWriter.writeRawValue(String) instead.

Note that not all generator implementations necessarily support such by-pass methods: those that do not will throw UnsupportedOperationException.

Specified by:
writeRaw in interface JsonStreamWriter
Throws:
IOException
JsonGenerationException

writeRawValue

public void writeRawValue(String text)
                   throws IOException,
                          JsonGenerationException
Description copied from interface: JsonStreamWriter
Method that will force generator to copy input text verbatim without any modifications, but assuming it must constitute a single legal JSON value (number, string, boolean, null, Array or List). Assuming this, proper separators are added if and as needed (comma or colon), and generator state updated to reflect this.

Specified by:
writeRawValue in interface JsonStreamWriter
Throws:
IOException
JsonGenerationException

writeRawValue

public void writeRawValue(String text,
                          int offset,
                          int len)
                   throws IOException,
                          JsonGenerationException
Specified by:
writeRawValue in interface JsonStreamWriter
Throws:
IOException
JsonGenerationException

writeRawValue

public void writeRawValue(char[] text,
                          int offset,
                          int len)
                   throws IOException,
                          JsonGenerationException
Specified by:
writeRawValue in interface JsonStreamWriter
Throws:
IOException
JsonGenerationException

writeNumber

public void writeNumber(int v)
                 throws IOException,
                        JsonGenerationException
Description copied from interface: JsonStreamWriter
Method for outputting given value as Json number. Can be called in any context where a value is expected (Array value, Object field value, root-level value). Additional white space may be added around the value if pretty-printing is enabled.

Specified by:
writeNumber in interface JsonStreamWriter
Throws:
IOException
JsonGenerationException

writeNumber

public void writeNumber(long v)
                 throws IOException,
                        JsonGenerationException
Description copied from interface: JsonStreamWriter
Method for outputting given value as Json number. Can be called in any context where a value is expected (Array value, Object field value, root-level value). Additional white space may be added around the value if pretty-printing is enabled.

Specified by:
writeNumber in interface JsonStreamWriter
Throws:
IOException
JsonGenerationException

writeNumber

public void writeNumber(BigInteger v)
                 throws IOException,
                        JsonGenerationException
Description copied from interface: JsonStreamWriter
Method for outputting given value as Json number. Can be called in any context where a value is expected (Array value, Object field value, root-level value). Additional white space may be added around the value if pretty-printing is enabled.

Specified by:
writeNumber in interface JsonStreamWriter
Throws:
IOException
JsonGenerationException

writeNumber

public void writeNumber(double d)
                 throws IOException,
                        JsonGenerationException
Description copied from interface: JsonStreamWriter
Method for outputting indicate Json numeric value. Can be called in any context where a value is expected (Array value, Object field value, root-level value). Additional white space may be added around the value if pretty-printing is enabled.

Specified by:
writeNumber in interface JsonStreamWriter
Throws:
IOException
JsonGenerationException

writeNumber

public void writeNumber(float f)
                 throws IOException,
                        JsonGenerationException
Description copied from interface: JsonStreamWriter
Method for outputting indicate Json numeric value. Can be called in any context where a value is expected (Array value, Object field value, root-level value). Additional white space may be added around the value if pretty-printing is enabled.

Specified by:
writeNumber in interface JsonStreamWriter
Throws:
IOException
JsonGenerationException

writeNumber

public void writeNumber(BigDecimal dec)
                 throws IOException,
                        JsonGenerationException
Description copied from interface: JsonStreamWriter
Method for outputting indicate Json numeric value. Can be called in any context where a value is expected (Array value, Object field value, root-level value). Additional white space may be added around the value if pretty-printing is enabled.

Specified by:
writeNumber in interface JsonStreamWriter
Throws:
IOException
JsonGenerationException

writeNumber

public void writeNumber(String encodedValue)
                 throws IOException,
                        JsonGenerationException,
                        UnsupportedOperationException
Description copied from interface: JsonStreamWriter
Write method that can be used for custom numeric types that can not be (easily?) converted to "standard" Java number types. Because numbers are not surrounded by double quotes, regular JsonStreamWriter.writeString(java.lang.String) method can not be used; nor JsonStreamWriter.writeRaw(java.lang.String) because that does not properly handle value separators needed in Array or Object contexts.

Note: because of lack of type safety, some generator implementations may not be able to implement this method. For example, if a binary json format is used, it may require type information for encoding; similarly for generator-wrappers around Java objects or Json nodes. If implementation does not implement this method, it needs to throw UnsupportedOperationException.

Specified by:
writeNumber in interface JsonStreamWriter
Throws:
IOException
JsonGenerationException
UnsupportedOperationException

writeBoolean

public void writeBoolean(boolean state)
                  throws IOException,
                         JsonGenerationException
Description copied from interface: JsonStreamWriter
Method for outputting literal Json boolean value (one of Strings 'true' and 'false'). Can be called in any context where a value is expected (Array value, Object field value, root-level value). Additional white space may be added around the value if pretty-printing is enabled.

Specified by:
writeBoolean in interface JsonStreamWriter
Throws:
IOException
JsonGenerationException

writeNull

public void writeNull()
               throws IOException,
                      JsonGenerationException
Description copied from interface: JsonStreamWriter
Method for outputting literal Json null value. Can be called in any context where a value is expected (Array value, Object field value, root-level value). Additional white space may be added around the value if pretty-printing is enabled.

Specified by:
writeNull in interface JsonStreamWriter
Throws:
IOException
JsonGenerationException

writeStringField

public void writeStringField(String fieldName,
                             String value)
                      throws IOException,
                             JsonGenerationException
Description copied from interface: JsonStreamWriter
Convenience method for outputting a field entry ("member") that has a String value. Equivalent to:
  writeFieldName(fieldName);
  writeString(value);

Note: many performance-sensitive implementations override this method

Specified by:
writeStringField in interface JsonStreamWriter
Throws:
IOException
JsonGenerationException

writeBooleanField

public final void writeBooleanField(String fieldName,
                                    boolean value)
                             throws IOException,
                                    JsonGenerationException
Description copied from interface: JsonStreamWriter
Convenience method for outputting a field entry ("member") that has a boolean value. Equivalent to:
  writeFieldName(fieldName);
  writeBoolean(value);

Specified by:
writeBooleanField in interface JsonStreamWriter
Throws:
IOException
JsonGenerationException

writeNullField

public final void writeNullField(String fieldName)
                          throws IOException,
                                 JsonGenerationException
Description copied from interface: JsonStreamWriter
Convenience method for outputting a field entry ("member") that has JSON literal value null. Equivalent to:
  writeFieldName(fieldName);
  writeNull();

Specified by:
writeNullField in interface JsonStreamWriter
Throws:
IOException
JsonGenerationException

writeNumberField

public final void writeNumberField(String fieldName,
                                   int value)
                            throws IOException,
                                   JsonGenerationException
Description copied from interface: JsonStreamWriter
Convenience method for outputting a field entry ("member") that has the specified numeric value. Equivalent to:
  writeFieldName(fieldName);
  writeNumber(value);

Specified by:
writeNumberField in interface JsonStreamWriter
Throws:
IOException
JsonGenerationException

writeNumberField

public final void writeNumberField(String fieldName,
                                   long value)
                            throws IOException,
                                   JsonGenerationException
Description copied from interface: JsonStreamWriter
Convenience method for outputting a field entry ("member") that has the specified numeric value. Equivalent to:
  writeFieldName(fieldName);
  writeNumber(value);

Specified by:
writeNumberField in interface JsonStreamWriter
Throws:
IOException
JsonGenerationException

writeNumberField

public final void writeNumberField(String fieldName,
                                   double value)
                            throws IOException,
                                   JsonGenerationException
Description copied from interface: JsonStreamWriter
Convenience method for outputting a field entry ("member") that has the specified numeric value. Equivalent to:
  writeFieldName(fieldName);
  writeNumber(value);

Specified by:
writeNumberField in interface JsonStreamWriter
Throws:
IOException
JsonGenerationException

writeNumberField

public final void writeNumberField(String fieldName,
                                   float value)
                            throws IOException,
                                   JsonGenerationException
Description copied from interface: JsonStreamWriter
Convenience method for outputting a field entry ("member") that has the specified numeric value. Equivalent to:
  writeFieldName(fieldName);
  writeNumber(value);

Specified by:
writeNumberField in interface JsonStreamWriter
Throws:
IOException
JsonGenerationException

writeNumberField

public final void writeNumberField(String fieldName,
                                   BigDecimal value)
                            throws IOException,
                                   JsonGenerationException
Description copied from interface: JsonStreamWriter
Convenience method for outputting a field entry ("member") that has the specified numeric value. Equivalent to:
  writeFieldName(fieldName);
  writeNumber(value);

Specified by:
writeNumberField in interface JsonStreamWriter
Throws:
IOException
JsonGenerationException

writeArrayFieldStart

public final void writeArrayFieldStart(String fieldName)
                                throws IOException,
                                       JsonGenerationException
Description copied from interface: JsonStreamWriter
Convenience method for outputting a field entry ("member") (that will contain a JSON Array value), and the START_ARRAY marker. Equivalent to:
  writeFieldName(fieldName);
  writeStartArray();

Note: caller still has to take care to close the array (by calling {#link #writeEndArray}) after writing all values of the value Array.

Specified by:
writeArrayFieldStart in interface JsonStreamWriter
Throws:
IOException
JsonGenerationException

writeObjectFieldStart

public final void writeObjectFieldStart(String fieldName)
                                 throws IOException,
                                        JsonGenerationException
Description copied from interface: JsonStreamWriter
Convenience method for outputting a field entry ("member") (that will contain a JSON Object value), and the START_OBJECT marker. Equivalent to:
  writeFieldName(fieldName);
  writeStartObject();

Note: caller still has to take care to close the Object (by calling {#link #writeEndObject}) after writing all entries of the value Object.

Specified by:
writeObjectFieldStart in interface JsonStreamWriter
Throws:
IOException
JsonGenerationException


Copyright © 2019. All Rights Reserved.