From f6d3249d38f07a4526b2343ada38564d762d50d7 Mon Sep 17 00:00:00 2001 From: Jon Bratseth Date: Tue, 18 Jun 2019 13:46:08 +0200 Subject: Read dense tensor form in documents --- .../src/main/java/com/yahoo/tensor/serialization/JsonFormat.java | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'vespajlib/src/main/java/com/yahoo/tensor/serialization/JsonFormat.java') 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 01da45c67aa..c73ff03a0eb 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/serialization/JsonFormat.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/serialization/JsonFormat.java @@ -58,6 +58,7 @@ public class JsonFormat { } /** Deserializes the given tensor from JSON format */ + // NOTE: This must be kept in sync with com.yahoo.document.json.readers.TensorReader in the document module public static Tensor decode(TensorType type, byte[] jsonTensorValue) { Tensor.Builder builder = Tensor.Builder.of(type); Inspector root = new JsonDecoder().decode(new Slime(), jsonTensorValue).get(); @@ -66,6 +67,8 @@ public class JsonFormat { decodeCells(root.field("cells"), builder); else if (root.field("values").valid()) decodeValues(root.field("values"), builder); + else if (builder.type().dimensions().stream().anyMatch(d -> d.isIndexed())) // sparse can be empty + throw new IllegalArgumentException("Expected a tensor value to contain either 'cells' or 'values'"); return builder.build(); } @@ -102,4 +105,5 @@ public class JsonFormat { indexedBuilder.cellByDirectIndex(index.next(), value.asDouble()); }); } + } -- cgit v1.2.3