summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--document/src/main/java/com/yahoo/document/json/JsonSerializationHelper.java2
-rw-r--r--document/src/main/java/com/yahoo/document/json/readers/TensorReader.java4
-rw-r--r--document/src/test/java/com/yahoo/document/json/DocumentUpdateJsonSerializerTest.java1
-rw-r--r--document/src/test/java/com/yahoo/document/json/JsonWriterTestCase.java8
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/serialization/JsonFormat.java2
-rw-r--r--vespajlib/src/test/java/com/yahoo/tensor/serialization/JsonFormatTestCase.java2
6 files changed, 14 insertions, 5 deletions
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 0f6d6cb4a65..a3cda4034ac 100644
--- a/document/src/main/java/com/yahoo/document/json/JsonSerializationHelper.java
+++ b/document/src/main/java/com/yahoo/document/json/JsonSerializationHelper.java
@@ -80,7 +80,7 @@ public class JsonSerializationHelper {
fieldNameIfNotNull(generator, field);
if (value.getTensor().isPresent()) {
Tensor tensor = value.getTensor().get();
- byte[] encoded = shortForm ? JsonFormat.encodeShortForm(tensor) : JsonFormat.encode(tensor);
+ byte[] encoded = shortForm ? JsonFormat.encodeShortForm(tensor) : JsonFormat.encodeWithType(tensor);
generator.writeRawValue(new String(encoded, StandardCharsets.UTF_8));
}
else {
diff --git a/document/src/main/java/com/yahoo/document/json/readers/TensorReader.java b/document/src/main/java/com/yahoo/document/json/readers/TensorReader.java
index 0d971859550..914ab670142 100644
--- a/document/src/main/java/com/yahoo/document/json/readers/TensorReader.java
+++ b/document/src/main/java/com/yahoo/document/json/readers/TensorReader.java
@@ -22,6 +22,7 @@ import static com.yahoo.tensor.serialization.JsonFormat.decodeHexString;
*/
public class TensorReader {
+ public static final String TENSOR_TYPE = "type";
public static final String TENSOR_ADDRESS = "address";
public static final String TENSOR_CELLS = "cells";
public static final String TENSOR_VALUES = "values";
@@ -43,6 +44,9 @@ public class TensorReader {
else if (TENSOR_BLOCKS.equals(buffer.currentName())) {
readTensorBlocks(buffer, builder);
}
+ else if (TENSOR_TYPE.equals(buffer.currentName()) && buffer.current() == JsonToken.VALUE_STRING) {
+ // Ignore input tensor type
+ }
else {
buffer.previous(); // Back up to the start of the enclosing block
readDirectTensorValue(buffer, builder);
diff --git a/document/src/test/java/com/yahoo/document/json/DocumentUpdateJsonSerializerTest.java b/document/src/test/java/com/yahoo/document/json/DocumentUpdateJsonSerializerTest.java
index 7bdb526bb1c..08a5c9a124c 100644
--- a/document/src/test/java/com/yahoo/document/json/DocumentUpdateJsonSerializerTest.java
+++ b/document/src/test/java/com/yahoo/document/json/DocumentUpdateJsonSerializerTest.java
@@ -285,6 +285,7 @@ public class DocumentUpdateJsonSerializerTest {
" 'fields': {",
" 'sparse_tensor': {",
" 'assign': {",
+ " 'type': 'tensor(x{},y{})',",
" 'cells': [",
" { 'address': { 'x': 'a', 'y': 'b' }, 'value': 2.0 },",
" { 'address': { 'x': 'c', 'y': 'b' }, 'value': 3.0 }",
diff --git a/document/src/test/java/com/yahoo/document/json/JsonWriterTestCase.java b/document/src/test/java/com/yahoo/document/json/JsonWriterTestCase.java
index e7368a691ab..edf410b312e 100644
--- a/document/src/test/java/com/yahoo/document/json/JsonWriterTestCase.java
+++ b/document/src/test/java/com/yahoo/document/json/JsonWriterTestCase.java
@@ -396,19 +396,21 @@ public class JsonWriterTestCase {
@Test
public void testWritingOfEmptyTensor() throws IOException {
- assertTensorRoundTripEquality("{}","{ \"cells\": [] }");
+ assertTensorRoundTripEquality("{}","{ \"type\":\"tensor(x{},y{})\", \"cells\": [] }");
}
@Test
public void testWritingOfTensorWithCellsOnly() throws IOException {
assertTensorRoundTripEquality("{ "
- + " \"cells\": [ "
+ + " \"type\": \"tensor(x{},y{})\","
+ + " \"cells\": [ "
+ " { \"address\": { \"x\": \"a\", \"y\": \"b\" }, "
+ " \"value\": 2.0 }, "
+ " { \"address\": { \"x\": \"c\", \"y\": \"b\" }, "
+ " \"value\": 3.0 } "
+ " ]"
+ "}", "{ "
+ + " \"type\": \"tensor(x{},y{})\","
+ " \"cells\": [ "
+ " { \"address\": { \"x\": \"a\", \"y\": \"b\" }, "
+ " \"value\": 2.0 }, "
@@ -449,7 +451,7 @@ public class JsonWriterTestCase {
Tensor tensor = Tensor.from("tensor(x[3]):[1,2,3]");
doc.setFieldValue(tensorField, new TensorFieldValue(tensor));
- assertEqualJson(asDocument(docId, "{ \"tensorfield\": {\"cells\":[{\"address\":{\"x\":\"0\"},\"value\":1.0},{\"address\":{\"x\":\"1\"},\"value\":2.0},{\"address\":{\"x\":\"2\"},\"value\":3.0}]} }"),
+ assertEqualJson(asDocument(docId, "{ \"tensorfield\": {\"type\":\"tensor(x[3])\", \"cells\":[{\"address\":{\"x\":\"0\"},\"value\":1.0},{\"address\":{\"x\":\"1\"},\"value\":2.0},{\"address\":{\"x\":\"2\"},\"value\":3.0}]} }"),
writeDocument(doc, false));
assertEqualJson(asDocument(docId, "{ \"tensorfield\": {\"type\":\"tensor(x[3])\", \"values\":[1.0, 2.0, 3.0] } }"),
writeDocument(doc, true));
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/serialization/JsonFormat.java b/vespajlib/src/main/java/com/yahoo/tensor/serialization/JsonFormat.java
index 0e8fbc30bb6..8322b3b6327 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/serialization/JsonFormat.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/serialization/JsonFormat.java
@@ -74,7 +74,7 @@ public class JsonFormat {
encodeBlocks((MixedTensor) tensor, root);
}
else {
- // No other short forms exist: default to standard cell address output
+ // default to standard cell address output
encodeCells(tensor, root);
}
diff --git a/vespajlib/src/test/java/com/yahoo/tensor/serialization/JsonFormatTestCase.java b/vespajlib/src/test/java/com/yahoo/tensor/serialization/JsonFormatTestCase.java
index 6a6bb3c6781..1cbf1709be1 100644
--- a/vespajlib/src/test/java/com/yahoo/tensor/serialization/JsonFormatTestCase.java
+++ b/vespajlib/src/test/java/com/yahoo/tensor/serialization/JsonFormatTestCase.java
@@ -32,11 +32,13 @@ public class JsonFormatTestCase {
assertDecoded("tensor(x{}):{cells:2, b:3}", "{'cells':2.0, 'b':3.0}");
assertDecoded("tensor(x{}):{values:2, b:3}", "{'values':2.0, 'b':3.0}");
assertDecoded("tensor(x{}):{block:2, b:3}", "{'block':2.0, 'b':3.0}");
+ assertDecoded("tensor(x{}):{type:2, b:3}", "{'type':2.0, 'b':3.0}");
// Multi-valued
assertDecoded("tensor(x{},y[2]):{cells:[2, 3], b:[4, 5]}", "{'cells':[2, 3], 'b':[4, 5]}");
assertDecoded("tensor(x{},y[2]):{values:[2, 3], b:[4, 5]}", "{'values':[2, 3], 'b':[4, 5]}");
assertDecoded("tensor(x{},y[2]):{block:[2, 3], b:[4, 5]}", "{'block':[2, 3], 'b':[4, 5]}");
+ assertDecoded("tensor(x{},y[2]):{type:[2, 3], b:[4, 5]}", "{'type':[2, 3], 'b':[4, 5]}");
}
@Test