Class JSONBuilder

  • All Implemented Interfaces:
    Builder

    public class JSONBuilder
    extends Object
    implements Builder
    This class implements Builder interface and JSON builder for data mapper engine using jackson
    • Constructor Summary

      Constructors 
      Constructor Description
      JSONBuilder()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Method called to close builder, so that no more content can be written.
      String getContent()
      Methid called to get the final content after closing the builder
      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 writeBinary​(byte[] data, int offset, int len)
      Method that will output given chunk of binary data as base64 encoded, as a complete String value (surrounded by double quotes).
      void writeBinaryField​(String fieldName, byte[] data)
      Convenience method for outputting a field entry ("member") that contains specified data in base64-encoded form.
      void writeBoolean​(boolean state)
      Method for outputting literal 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 Array.
      void writeEndObject()
      Method for writing closing marker of a Object value.
      void writeField​(String fieldName, Object value, String fieldType)
      Convenience method for outputting a field entry ("member") that has a String value.
      void writeFieldName​(String name)
      Method for writing a field name.
      void writeNumber​(double number)
      Method for outputting indicate JSON numeric value.
      void writeNumber​(int number)
      Method for outputting given value as number.
      void writeNumberField​(String fieldName, double 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 writeObjectFieldStart​(String fieldName)
      Convenience method for outputting a field entry ("member") (that will contain a JSON Object value), and the START_OBJECT marker.
      void writePrimitive​(Object value, String fieldType)
      Convenience method for outputting a primitive that has a primitive value.
      void writeStartArray()
      Method for writing start marker of a Array value.
      void writeStartObject()
      Method for writing starting marker of a Object 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.
    • Method Detail

      • writeStartArray

        public void writeStartArray()
                             throws IOException
        Description copied from interface: Builder
        Method for writing start marker of a Array value.

        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 Builder
        Throws:
        IOException
      • writeEndArray

        public void writeEndArray()
                           throws IOException
        Description copied from interface: Builder
        Method for writing closing marker of a Array.

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

        Specified by:
        writeEndArray in interface Builder
        Throws:
        IOException
      • writeStartObject

        public void writeStartObject()
                              throws IOException
        Description copied from interface: Builder
        Method for writing starting marker of a Object value.

        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 Builder
        Throws:
        IOException
      • writeEndObject

        public void writeEndObject()
                            throws IOException
        Description copied from interface: Builder
        Method for writing closing marker of a Object value.

        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

        Specified by:
        writeEndObject in interface Builder
        Throws:
        IOException
      • writeFieldName

        public void writeFieldName​(String name)
                            throws IOException
        Description copied from interface: Builder
        Method for writing a field name.

        Field names can only be written in Object context , when field name is expected

        Specified by:
        writeFieldName in interface Builder
        Throws:
        IOException
      • writeString

        public void writeString​(String text)
                         throws IOException
        Description copied from interface: Builder
        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.
        Specified by:
        writeString in interface Builder
        Throws:
        IOException
      • writeBinary

        public void writeBinary​(byte[] data,
                                int offset,
                                int len)
                         throws IOException
        Description copied from interface: Builder
        Method that will output given chunk of binary data as base64 encoded, as a complete String value (surrounded by double quotes). This method defaults

        Alternatively if linefeeds are not included, resulting String value may violate the requirement of base64 RFC which mandates line-length of 76 characters and use of linefeeds. However, all Parser implementations are required to accept such "long line base64"; as do typical production-level base64 decoders.

        Specified by:
        writeBinary in interface Builder
        Throws:
        IOException
      • writeNumber

        public void writeNumber​(int number)
                         throws IOException
        Description copied from interface: Builder
        Method for outputting given value as number. Can be called in any context where a value is expected (Array value, Object field value, root-level value).
        Specified by:
        writeNumber in interface Builder
        Parameters:
        number - Number value to write
        Throws:
        IOException
      • writeNumber

        public void writeNumber​(double number)
                         throws IOException
        Description copied from interface: Builder
        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).
        Specified by:
        writeNumber in interface Builder
        Parameters:
        number - Number value to write
        Throws:
        IOException
      • writeBoolean

        public void writeBoolean​(boolean state)
                          throws IOException
        Description copied from interface: Builder
        Method for outputting literal 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).
        Specified by:
        writeBoolean in interface Builder
        Throws:
        IOException
      • writeStringField

        public void writeStringField​(String fieldName,
                                     String value)
                              throws IOException
        Description copied from interface: Builder
        Convenience method for outputting a field entry ("member") that has a String value. Equivalent to:
          writeFieldName(fieldName);
          writeString(value);
         
        Specified by:
        writeStringField in interface Builder
        Throws:
        IOException
      • writeField

        public void writeField​(String fieldName,
                               Object value,
                               String fieldType)
                        throws IOException
        Description copied from interface: Builder
        Convenience method for outputting a field entry ("member") that has a String value. Equivalent to:
          writeFieldName(fieldName);
          writeString(value);
         
        Specified by:
        writeField in interface Builder
        Throws:
        IOException
      • writeBooleanField

        public void writeBooleanField​(String fieldName,
                                      boolean value)
                               throws IOException
        Description copied from interface: Builder
        Convenience method for outputting a field entry ("member") that has a boolean value. Equivalent to:
          writeFieldName(fieldName);
          writeBoolean(value);
         
        Specified by:
        writeBooleanField in interface Builder
        Throws:
        IOException
      • writeNumberField

        public void writeNumberField​(String fieldName,
                                     int value)
                              throws IOException
        Description copied from interface: Builder
        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 Builder
        Throws:
        IOException
      • writeNumberField

        public void writeNumberField​(String fieldName,
                                     double value)
                              throws IOException
        Description copied from interface: Builder
        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 Builder
        Throws:
        IOException
      • writeBinaryField

        public void writeBinaryField​(String fieldName,
                                     byte[] data)
                              throws IOException
        Description copied from interface: Builder
        Convenience method for outputting a field entry ("member") that contains specified data in base64-encoded form. Equivalent to:
          writeFieldName(fieldName);
          writeBinary(value);
         
        Specified by:
        writeBinaryField in interface Builder
        Throws:
        IOException
      • writeArrayFieldStart

        public void writeArrayFieldStart​(String fieldName)
                                  throws IOException
        Description copied from interface: Builder
        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 Builder
        Throws:
        IOException
      • writeObjectFieldStart

        public void writeObjectFieldStart​(String fieldName)
                                   throws IOException
        Description copied from interface: Builder
        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 Builder
        Throws:
        IOException
      • close

        public void close()
                   throws IOException
        Description copied from interface: Builder
        Method called to close builder, so that no more content can be written.
        Specified by:
        close in interface Builder
        Throws:
        IOException
      • getContent

        public String getContent()
                          throws IOException
        Description copied from interface: Builder
        Methid called to get the final content after closing the builder
        Specified by:
        getContent in interface Builder
        Returns:
        built content
        Throws:
        IOException