Class JsonWriter

java.lang.Object
com.yahoo.document.json.JsonWriter
All Implemented Interfaces:
DocumentWriter, FieldWriter, com.yahoo.vespa.objects.Serializer

public class JsonWriter extends Object implements DocumentWriter
Serialize Document and other FieldValue instances as JSON.
Author:
Steinar Knutsen
  • Constructor Summary

    Constructors
    Constructor
    Description
    JsonWriter(com.fasterxml.jackson.core.JsonGenerator generator)
     
    JsonWriter(com.fasterxml.jackson.core.JsonGenerator generator, boolean tensorShortForm, boolean tensorDirectValues)
    Create a Document writer which will write to the input JSON generator.
    Creates a JsonWriter.
    JsonWriter(OutputStream out, boolean tensorShortForm, boolean tensorDirectValues)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static byte[]
    Utility method to easily serialize a single document ID as a remove operation.
    com.yahoo.vespa.objects.Serializer
    put(com.yahoo.vespa.objects.FieldBase field, byte[] value)
     
    com.yahoo.vespa.objects.Serializer
    put(com.yahoo.vespa.objects.FieldBase field, String value)
     
    com.yahoo.vespa.objects.Serializer
    put(com.yahoo.vespa.objects.FieldBase field, ByteBuffer value)
     
    com.yahoo.vespa.objects.Serializer
    putByte(com.yahoo.vespa.objects.FieldBase field, byte value)
     
    com.yahoo.vespa.objects.Serializer
    putDouble(com.yahoo.vespa.objects.FieldBase field, double value)
     
    com.yahoo.vespa.objects.Serializer
    putFloat(com.yahoo.vespa.objects.FieldBase field, float value)
     
    com.yahoo.vespa.objects.Serializer
    putInt(com.yahoo.vespa.objects.FieldBase field, int value)
     
    com.yahoo.vespa.objects.Serializer
    putLong(com.yahoo.vespa.objects.FieldBase field, long value)
     
    com.yahoo.vespa.objects.Serializer
    putShort(com.yahoo.vespa.objects.FieldBase field, short value)
     
    static byte[]
    Utility method to easily serialize a single document.
    static byte[]
    toByteArray(Document document, boolean tensorShortForm, boolean tensorDirectValues)
    Utility method to easily serialize a single document.
    void
    write(Document document)
    Writes a document.
    void
     
    void
    write(DocumentRemove documentRemove)
     
    void
     
    void
    write(DocumentUpdate documentUpdate)
     
    void
    write(com.yahoo.vespa.objects.FieldBase field, AnnotationReference value)
    Writes the value of an annotation reference.
    <T extends FieldValue>
    void
    write(com.yahoo.vespa.objects.FieldBase field, Array<T> value)
    Writes the value of an array field.
    void
    write(com.yahoo.vespa.objects.FieldBase field, BoolFieldValue value)
    Writes the value of byte field.
    void
    write(com.yahoo.vespa.objects.FieldBase field, ByteFieldValue value)
    Writes the value of a byte field.
    <T extends FieldValue>
    void
    write(com.yahoo.vespa.objects.FieldBase field, CollectionFieldValue<T> value)
    Writes the value of a collection field.
    void
    write(com.yahoo.vespa.objects.FieldBase field, DoubleFieldValue value)
    Writes the value of a double field.
    void
    write(com.yahoo.vespa.objects.FieldBase field, FieldValue value)
    This method will only be called if there is some type which is not properly supported in the API, or if something has been changed without updating this class.
    void
    write(com.yahoo.vespa.objects.FieldBase field, FloatFieldValue value)
    Writes the value of a flosat field.
    void
    write(com.yahoo.vespa.objects.FieldBase field, IntegerFieldValue value)
    Writes the value of an integer collection field.
    void
    write(com.yahoo.vespa.objects.FieldBase field, LongFieldValue value)
    Writes the value of a long field.
    <K extends FieldValue, V extends FieldValue>
    void
    write(com.yahoo.vespa.objects.FieldBase field, MapFieldValue<K,V> map)
    Writes the value of a map field.
    void
    write(com.yahoo.vespa.objects.FieldBase field, PredicateFieldValue value)
    Writes the value of a predicate field.
    void
    write(com.yahoo.vespa.objects.FieldBase field, Raw value)
    Writes the value of a raw field.
    void
    write(com.yahoo.vespa.objects.FieldBase field, ReferenceFieldValue value)
    Writes the value of a reference field.
    void
    write(com.yahoo.vespa.objects.FieldBase field, StringFieldValue value)
    Writes the value of a string field.
    void
    write(com.yahoo.vespa.objects.FieldBase field, Struct value)
    Writes the value of a struct field.
    void
    write(com.yahoo.vespa.objects.FieldBase field, StructuredFieldValue value)
    Writes the value of a structured field.
    void
    write(com.yahoo.vespa.objects.FieldBase field, TensorFieldValue value)
    Writes the value of a tensor field.
    <T extends FieldValue>
    void
    write(com.yahoo.vespa.objects.FieldBase field, WeightedSet<T> value)
    Writes the value of a weighted set field.
    void
    write(com.yahoo.vespa.objects.FieldBase field, Document value)
    Writes the value of a field.
    void
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • JsonWriter

      public JsonWriter(OutputStream out)
      Creates a JsonWriter.
      Parameters:
      out - the target output stream
      Throws:
      RuntimeException - if unable to create the internal JSON generator
    • JsonWriter

      public JsonWriter(OutputStream out, boolean tensorShortForm, boolean tensorDirectValues)
    • JsonWriter

      public JsonWriter(com.fasterxml.jackson.core.JsonGenerator generator, boolean tensorShortForm, boolean tensorDirectValues)
      Create a Document writer which will write to the input JSON generator. JsonWriter will not close the generator and only flush it explicitly after having written a full Document instance. In other words, JsonWriter will not take ownership of the generator.
      Parameters:
      generator - the output JSON generator
      tensorShortForm - whether to use the short type-dependent form for tensor values
      tensorDirectValues - whether to output tensor values directly or wrapped in a map also containing the type
    • JsonWriter

      public JsonWriter(com.fasterxml.jackson.core.JsonGenerator generator)
  • Method Details

    • write

      public void write(com.yahoo.vespa.objects.FieldBase field, FieldValue value)
      This method will only be called if there is some type which is not properly supported in the API, or if something has been changed without updating this class. This implementation throws an exception if it is reached.
      Specified by:
      write in interface FieldWriter
      Throws:
      UnsupportedOperationException - if invoked
    • write

      public void write(com.yahoo.vespa.objects.FieldBase field, Document value)
      Description copied from interface: FieldWriter
      Writes the value of a field.
      Specified by:
      write in interface FieldWriter
    • write

      public <T extends FieldValue> void write(com.yahoo.vespa.objects.FieldBase field, Array<T> value)
      Description copied from interface: FieldWriter
      Writes the value of an array field.
      Specified by:
      write in interface FieldWriter
    • write

      public <K extends FieldValue, V extends FieldValue> void write(com.yahoo.vespa.objects.FieldBase field, MapFieldValue<K,V> map)
      Description copied from interface: FieldWriter
      Writes the value of a map field.
      Specified by:
      write in interface FieldWriter
    • write

      public void write(com.yahoo.vespa.objects.FieldBase field, ByteFieldValue value)
      Description copied from interface: FieldWriter
      Writes the value of a byte field.
      Specified by:
      write in interface FieldWriter
    • write

      public void write(com.yahoo.vespa.objects.FieldBase field, BoolFieldValue value)
      Description copied from interface: FieldWriter
      Writes the value of byte field.
      Specified by:
      write in interface FieldWriter
    • write

      public <T extends FieldValue> void write(com.yahoo.vespa.objects.FieldBase field, CollectionFieldValue<T> value)
      Description copied from interface: FieldWriter
      Writes the value of a collection field.
      Specified by:
      write in interface FieldWriter
    • write

      public void write(com.yahoo.vespa.objects.FieldBase field, DoubleFieldValue value)
      Description copied from interface: FieldWriter
      Writes the value of a double field.
      Specified by:
      write in interface FieldWriter
    • write

      public void write(com.yahoo.vespa.objects.FieldBase field, FloatFieldValue value)
      Description copied from interface: FieldWriter
      Writes the value of a flosat field.
      Specified by:
      write in interface FieldWriter
    • write

      public void write(com.yahoo.vespa.objects.FieldBase field, IntegerFieldValue value)
      Description copied from interface: FieldWriter
      Writes the value of an integer collection field.
      Specified by:
      write in interface FieldWriter
    • write

      public void write(com.yahoo.vespa.objects.FieldBase field, LongFieldValue value)
      Description copied from interface: FieldWriter
      Writes the value of a long field.
      Specified by:
      write in interface FieldWriter
    • write

      public void write(com.yahoo.vespa.objects.FieldBase field, Raw value)
      Description copied from interface: FieldWriter
      Writes the value of a raw field.
      Specified by:
      write in interface FieldWriter
    • write

      public void write(com.yahoo.vespa.objects.FieldBase field, PredicateFieldValue value)
      Description copied from interface: FieldWriter
      Writes the value of a predicate field.
      Specified by:
      write in interface FieldWriter
    • write

      public void write(com.yahoo.vespa.objects.FieldBase field, StringFieldValue value)
      Description copied from interface: FieldWriter
      Writes the value of a string field.
      Specified by:
      write in interface FieldWriter
    • write

      public void write(com.yahoo.vespa.objects.FieldBase field, TensorFieldValue value)
      Description copied from interface: FieldWriter
      Writes the value of a tensor field.
      Specified by:
      write in interface FieldWriter
    • write

      public void write(com.yahoo.vespa.objects.FieldBase field, ReferenceFieldValue value)
      Description copied from interface: FieldWriter
      Writes the value of a reference field.
      Specified by:
      write in interface FieldWriter
    • write

      public void write(com.yahoo.vespa.objects.FieldBase field, Struct value)
      Description copied from interface: FieldWriter
      Writes the value of a struct field.
      Specified by:
      write in interface FieldWriter
    • write

      public void write(com.yahoo.vespa.objects.FieldBase field, StructuredFieldValue value)
      Description copied from interface: FieldWriter
      Writes the value of a structured field.
      Specified by:
      write in interface FieldWriter
    • write

      public <T extends FieldValue> void write(com.yahoo.vespa.objects.FieldBase field, WeightedSet<T> value)
      Description copied from interface: FieldWriter
      Writes the value of a weighted set field.
      Specified by:
      write in interface FieldWriter
    • write

      public void write(com.yahoo.vespa.objects.FieldBase field, AnnotationReference value)
      Description copied from interface: FieldWriter
      Writes the value of an annotation reference.
      Specified by:
      write in interface FieldWriter
    • write

      public void write(Document document)
      Description copied from interface: DocumentWriter
      Writes a document.
      Specified by:
      write in interface DocumentWriter
    • write

      public void write(DocumentId id)
      Specified by:
      write in interface DocumentWriter
    • write

      public void write(DocumentType type)
      Specified by:
      write in interface DocumentWriter
    • write

      public void write(DocumentRemove documentRemove)
      Specified by:
      write in interface DocumentWriter
    • write

      public void write(DocumentUpdate documentUpdate)
      Specified by:
      write in interface DocumentWriter
    • toByteArray

      public static byte[] toByteArray(Document document, boolean tensorShortForm, boolean tensorDirectValues)
      Utility method to easily serialize a single document.
      Parameters:
      document - the document to be serialized
      tensorShortForm - whether tensors should be serialized in a type-dependent short form
      tensorDirectValues - whether tensors should be serialized as direct values or wrapped in a map also containing the type
      Returns:
      the input document serialised as UTF-8 encoded JSON
    • toByteArray

      public static byte[] toByteArray(Document document)
      Utility method to easily serialize a single document.
      Parameters:
      document - the document to be serialized
      Returns:
      the input document serialised as UTF-8 encoded JSON
    • documentRemove

      public static byte[] documentRemove(DocumentId docId)
      Utility method to easily serialize a single document ID as a remove operation.
      Parameters:
      docId - the document to remove or which has been removed
      Returns:
      a document remove operation serialised as UTF-8 encoded JSON for the input document ID
    • putByte

      public com.yahoo.vespa.objects.Serializer putByte(com.yahoo.vespa.objects.FieldBase field, byte value)
      Specified by:
      putByte in interface com.yahoo.vespa.objects.Serializer
    • putShort

      public com.yahoo.vespa.objects.Serializer putShort(com.yahoo.vespa.objects.FieldBase field, short value)
      Specified by:
      putShort in interface com.yahoo.vespa.objects.Serializer
    • putInt

      public com.yahoo.vespa.objects.Serializer putInt(com.yahoo.vespa.objects.FieldBase field, int value)
      Specified by:
      putInt in interface com.yahoo.vespa.objects.Serializer
    • putLong

      public com.yahoo.vespa.objects.Serializer putLong(com.yahoo.vespa.objects.FieldBase field, long value)
      Specified by:
      putLong in interface com.yahoo.vespa.objects.Serializer
    • putFloat

      public com.yahoo.vespa.objects.Serializer putFloat(com.yahoo.vespa.objects.FieldBase field, float value)
      Specified by:
      putFloat in interface com.yahoo.vespa.objects.Serializer
    • putDouble

      public com.yahoo.vespa.objects.Serializer putDouble(com.yahoo.vespa.objects.FieldBase field, double value)
      Specified by:
      putDouble in interface com.yahoo.vespa.objects.Serializer
    • put

      public com.yahoo.vespa.objects.Serializer put(com.yahoo.vespa.objects.FieldBase field, byte[] value)
      Specified by:
      put in interface com.yahoo.vespa.objects.Serializer
    • put

      public com.yahoo.vespa.objects.Serializer put(com.yahoo.vespa.objects.FieldBase field, ByteBuffer value)
      Specified by:
      put in interface com.yahoo.vespa.objects.Serializer
    • put

      public com.yahoo.vespa.objects.Serializer put(com.yahoo.vespa.objects.FieldBase field, String value)
      Specified by:
      put in interface com.yahoo.vespa.objects.Serializer
    • writeFields

      public void writeFields(Document value) throws IOException
      Throws:
      IOException