diff options
author | Jon Bratseth <bratseth@gmail.com> | 2023-01-13 13:30:06 +0100 |
---|---|---|
committer | Jon Bratseth <bratseth@gmail.com> | 2023-01-13 13:30:06 +0100 |
commit | feb27aa51f1274e2b247b8f764f3293c7e5f1ad6 (patch) | |
tree | 34b4279c6d8c9b14c1ca766ed802db732828b68c /document | |
parent | 6ea555de57ad11bae44e7f9abca4d2b06d3863ae (diff) |
Use JsonFormat for JSON rendering
Diffstat (limited to 'document')
4 files changed, 37 insertions, 185 deletions
diff --git a/document/src/main/java/com/yahoo/document/json/DocumentUpdateJsonSerializer.java b/document/src/main/java/com/yahoo/document/json/DocumentUpdateJsonSerializer.java index 1f92bed5e75..8b44910e75c 100644 --- a/document/src/main/java/com/yahoo/document/json/DocumentUpdateJsonSerializer.java +++ b/document/src/main/java/com/yahoo/document/json/DocumentUpdateJsonSerializer.java @@ -369,7 +369,7 @@ public class DocumentUpdateJsonSerializer { @Override public void write(FieldBase field, TensorFieldValue value) { - serializeTensorField(generator, field, value); + serializeTensorField(generator, field, value, false, false); } @Override diff --git a/document/src/main/java/com/yahoo/document/json/JsonSerializationHelper.java b/document/src/main/java/com/yahoo/document/json/JsonSerializationHelper.java index 7f6ead528fe..0f6d6cb4a65 100644 --- a/document/src/main/java/com/yahoo/document/json/JsonSerializationHelper.java +++ b/document/src/main/java/com/yahoo/document/json/JsonSerializationHelper.java @@ -74,32 +74,22 @@ public class JsonSerializationHelper { } } - public static void serializeTensorFieldShortForm(JsonGenerator generator, FieldBase field, TensorFieldValue value) { + public static void serializeTensorField(JsonGenerator generator, FieldBase field, TensorFieldValue value, + boolean shortForm, boolean valueOnly) { wrapIOException(() -> { fieldNameIfNotNull(generator, field); if (value.getTensor().isPresent()) { Tensor tensor = value.getTensor().get(); - generator.writeRawValue(new String(JsonFormat.encodeShortForm(tensor), StandardCharsets.UTF_8)); - } else { + byte[] encoded = shortForm ? JsonFormat.encodeShortForm(tensor) : JsonFormat.encode(tensor); + generator.writeRawValue(new String(encoded, StandardCharsets.UTF_8)); + } + else { generator.writeStartObject(); generator.writeEndObject(); } }); } - public static void serializeTensorField(JsonGenerator generator, FieldBase field, TensorFieldValue value) { - wrapIOException(() -> { - fieldNameIfNotNull(generator, field); - generator.writeStartObject(); - - if (value.getTensor().isPresent()) { - Tensor tensor = value.getTensor().get(); - serializeTensorCells(generator, tensor); - } - generator.writeEndObject(); - }); - } - static void serializeTensorCells(JsonGenerator generator, Tensor tensor) throws IOException { generator.writeArrayFieldStart(TensorReader.TENSOR_CELLS); for (Map.Entry<TensorAddress, Double> cell : tensor.cells().entrySet()) { diff --git a/document/src/main/java/com/yahoo/document/json/JsonWriter.java b/document/src/main/java/com/yahoo/document/json/JsonWriter.java index 04edfeea26e..27a1dd150f3 100644 --- a/document/src/main/java/com/yahoo/document/json/JsonWriter.java +++ b/document/src/main/java/com/yahoo/document/json/JsonWriter.java @@ -63,7 +63,6 @@ import static com.yahoo.document.json.JsonSerializationHelper.serializeStringFie import static com.yahoo.document.json.JsonSerializationHelper.serializeStructField; import static com.yahoo.document.json.JsonSerializationHelper.serializeStructuredField; import static com.yahoo.document.json.JsonSerializationHelper.serializeTensorField; -import static com.yahoo.document.json.JsonSerializationHelper.serializeTensorFieldShortForm; import static com.yahoo.document.json.JsonSerializationHelper.serializeWeightedSet; import static com.yahoo.document.json.document.DocumentParser.FIELDS; import static com.yahoo.document.json.document.DocumentParser.REMOVE; @@ -218,11 +217,7 @@ public class JsonWriter implements DocumentWriter { @Override public void write(FieldBase field, TensorFieldValue value) { - if (tensorShortForm) { - serializeTensorFieldShortForm(generator, field, value); - } else { - serializeTensorField(generator, field, value); - } + serializeTensorField(generator, field, value, tensorShortForm, false); } @Override diff --git a/document/src/main/java/com/yahoo/document/serialization/FieldWriter.java b/document/src/main/java/com/yahoo/document/serialization/FieldWriter.java index deb1e7386df..43e25b24207 100644 --- a/document/src/main/java/com/yahoo/document/serialization/FieldWriter.java +++ b/document/src/main/java/com/yahoo/document/serialization/FieldWriter.java @@ -25,203 +25,70 @@ import com.yahoo.vespa.objects.FieldBase; import com.yahoo.vespa.objects.Serializer; /** - * Interface for writing out com.yahoo.document.datatypes.FieldValue. - * - * @author <a href="mailto:ravishar@yahoo-inc.com">ravishar</a> + * Interface for writing a com.yahoo.document.datatypes.FieldValue. * + * @author ravishar */ public interface FieldWriter extends Serializer { - /** - * Write out the value of field - * - * @param field - * field description (name and data type) - * @param value - * field value - */ + /** Writes the value of a field. */ void write(FieldBase field, FieldValue value); - /** - * Write out the value of field - * - * @param field - * field description (name and data type) - * @param value - * field value - */ - public void write(FieldBase field, Document value); - - /** - * Write out the value of array field - * - * @param field - * field description (name and data type) - * @param value - * field value - */ + /** Writes the value of a field. */ + void write(FieldBase field, Document value); + + /** Writes the value of an array field. */ <T extends FieldValue> void write(FieldBase field, Array<T> value); - /** - * Write the value of a map field - */ - <K extends FieldValue, V extends FieldValue> void write(FieldBase field, - MapFieldValue<K, V> map); - - /** - * Write out the value of byte field - * - * @param field - * field description (name and data type) - * @param value - * field value - */ + /** Writes the value of a map field. */ + <K extends FieldValue, V extends FieldValue> void write(FieldBase field, MapFieldValue<K, V> map); + + /*** Writes the value of a byte field. */ void write(FieldBase field, ByteFieldValue value); - /** - * Write out the value of byte field - * - * @param field - * field description (name and data type) - * @param value - * field value - */ + /** Writes the value of byte field. */ void write(FieldBase field, BoolFieldValue value); - /** - * Write out the value of collection field - * - * @param field - * field description (name and data type) - * @param value - * field value - */ - <T extends FieldValue> void write(FieldBase field, - CollectionFieldValue<T> value); - - /** - * Write out the value of double field - * - * @param field - * field description (name and data type) - * @param value - * field value - */ + /** Writes the value of a collection field. */ + <T extends FieldValue> void write(FieldBase field, CollectionFieldValue<T> value); + + /** Writes the value of a double field. */ void write(FieldBase field, DoubleFieldValue value); - /** - * Write out the value of float field - * - * @param field - * field description (name and data type) - * @param value - * field value - */ + /** Writes the value of a flosat field. */ void write(FieldBase field, FloatFieldValue value); - /** - * Write out the value of integer field - * - * @param field - * field description (name and data type) - * @param value - * field value - */ + /** Writes the value of an integer collection field. */ void write(FieldBase field, IntegerFieldValue value); - /** - * Write out the value of long field - * - * @param field - * field description (name and data type) - * @param value - * field value - */ + /** Writes the value of a long field. */ void write(FieldBase field, LongFieldValue value); - /** - * Write out the value of raw field - * - * @param field - * field description (name and data type) - * @param value - * field value - */ + /** Writes the value of a raw field. */ void write(FieldBase field, Raw value); - /** - * Write out the value of predicate field - * - * @param field - * field description (name and data type) - * @param value - * field value - */ + /** Writes the value of a predicate field. */ void write(FieldBase field, PredicateFieldValue value); - /** - * Write out the value of string field - * - * @param field - * field description (name and data type) - * @param value - * field value - */ + /** Writes the value of a string field. */ void write(FieldBase field, StringFieldValue value); - /** - * Write out the value of the given tensor field value. - * - * @param field field description (name and data type) - * @param value tensor field value - */ + /** Writes the value of a tensor field. */ void write(FieldBase field, TensorFieldValue value); - /** - * Write out the value of the given reference field value. - * - * @param field field description (name and data type) - * @param value reference field value - */ + /** Writes the value of a reference field. */ void write(FieldBase field, ReferenceFieldValue value); - /** - * Write out the value of struct field - * - * @param field - * field description (name and data type) - * @param value - * field value - */ + /** Writes the value of a struct field. */ void write(FieldBase field, Struct value); - /** - * Write out the value of structured field - * - * @param field - * field description (name and data type) - * @param value - * field value - */ + /** Writes the value of a structured field. */ void write(FieldBase field, StructuredFieldValue value); - /** - * Write out the value of weighted set field - * - * @param field - * field description (name and data type) - * @param value - * field value - */ + /** Writes the value of a weighted set field. */ <T extends FieldValue> void write(FieldBase field, WeightedSet<T> value); - /** - * Write out the value of annotation data. - * - * @param field - * field description (name and data type) - * @param value - * field value - */ + /** Writes the value of an annotation reference. */ void write(FieldBase field, AnnotationReference value); + } |