diff options
author | Jon Bratseth <bratseth@gmail.com> | 2023-01-14 18:41:49 +0100 |
---|---|---|
committer | Jon Bratseth <bratseth@gmail.com> | 2023-01-14 18:41:49 +0100 |
commit | 416f596b150ec159717bfd2f9b2ef70e4d4cd3dd (patch) | |
tree | fd78cf0541670dd50e2dc3256c5b9755ced8f73e /document/src/main/java/com/yahoo/document/json/JsonWriter.java | |
parent | a289581cbf94ff6997356110b54bd6993e956b9e (diff) |
Support direct tensor rendering
Diffstat (limited to 'document/src/main/java/com/yahoo/document/json/JsonWriter.java')
-rw-r--r-- | document/src/main/java/com/yahoo/document/json/JsonWriter.java | 63 |
1 files changed, 32 insertions, 31 deletions
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 27a1dd150f3..33243ab832c 100644 --- a/document/src/main/java/com/yahoo/document/json/JsonWriter.java +++ b/document/src/main/java/com/yahoo/document/json/JsonWriter.java @@ -78,30 +78,20 @@ public class JsonWriter implements DocumentWriter { private final JsonGenerator generator; private final boolean tensorShortForm; + private final boolean tensorDirectValues; - // I really hate exception unsafe constructors, but the alternative - // requires generator to not be a final /** + * Creates a JsonWriter. * - * @param out - * the target output stream - * @throws RuntimeException - * if unable to create the internal JSON generator + * @param out the target output stream + * @throws RuntimeException if unable to create the internal JSON generator */ public JsonWriter(OutputStream out) { this(createPrivateGenerator(out)); } - public JsonWriter(OutputStream out, boolean tensorShortForm) { - this(createPrivateGenerator(out), tensorShortForm); - } - - private static JsonGenerator createPrivateGenerator(OutputStream out) { - try { - return jsonFactory.createGenerator(out); - } catch (IOException e) { - throw new RuntimeException(e); - } + public JsonWriter(OutputStream out, boolean tensorShortForm, boolean tensorDirectValues) { + this(createPrivateGenerator(out), tensorShortForm, tensorDirectValues); } /** @@ -110,16 +100,26 @@ public class JsonWriter implements DocumentWriter { * after having written a full Document instance. In other words, JsonWriter * will <i>not</i> take ownership of the generator. * - * @param generator - * the output JSON generator + * @param generator the output JSON generator + * @param tensorShortForm whether to use the short type-dependent form for tensor values + * @param tensorDirectValues whether to output tensor values directly or wrapped in a map also containing the type */ - public JsonWriter(JsonGenerator generator) { - this(generator, false); - } - - public JsonWriter(JsonGenerator generator, boolean tensorShortForm) { + public JsonWriter(JsonGenerator generator, boolean tensorShortForm, boolean tensorDirectValues) { this.generator = generator; this.tensorShortForm = tensorShortForm; + this.tensorDirectValues = tensorDirectValues; + } + + private static JsonGenerator createPrivateGenerator(OutputStream out) { + try { + return jsonFactory.createGenerator(out); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public JsonWriter(JsonGenerator generator) { + this(generator, false, false); } /** @@ -128,8 +128,7 @@ public class JsonWriter implements DocumentWriter { * updating this class. This implementation throws an exception if it is * reached. * - * @throws UnsupportedOperationException - * if invoked + * @throws UnsupportedOperationException if invoked */ @Override public void write(FieldBase field, FieldValue value) { @@ -217,7 +216,7 @@ public class JsonWriter implements DocumentWriter { @Override public void write(FieldBase field, TensorFieldValue value) { - serializeTensorField(generator, field, value, tensorShortForm, false); + serializeTensorField(generator, field, value, tensorShortForm, tensorDirectValues); } @Override @@ -265,12 +264,14 @@ public class JsonWriter implements DocumentWriter { * Utility method to easily serialize a single document. * * @param document the document to be serialized - * @param tensorShortForm whether tensors should be serialized in short form + * @param tensorShortForm whether tensors should be serialized in a type-dependent short form + * @param tensorDirectValues whether tensors should be serialized as direct values or wrapped in a + * map also containing the type * @return the input document serialised as UTF-8 encoded JSON */ - public static byte[] toByteArray(Document document, boolean tensorShortForm) { + public static byte[] toByteArray(Document document, boolean tensorShortForm, boolean tensorDirectValues) { ByteArrayOutputStream out = new ByteArrayOutputStream(); - JsonWriter writer = new JsonWriter(out, tensorShortForm); + JsonWriter writer = new JsonWriter(out, tensorShortForm, tensorDirectValues); writer.write(document); return out.toByteArray(); } @@ -282,8 +283,8 @@ public class JsonWriter implements DocumentWriter { * @return the input document serialised as UTF-8 encoded JSON */ public static byte[] toByteArray(Document document) { - // TODO Vespa 9: change tensorShortForm default to true - return toByteArray(document, false); + // TODO Vespa 9: change tensorShortForm and tensorDirectValues default to true + return toByteArray(document, false, false); } /** |