summaryrefslogtreecommitdiffstats
path: root/vespajlib
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@verizonmedia.com>2019-06-02 13:25:45 +0200
committerJon Bratseth <bratseth@verizonmedia.com>2019-06-02 13:25:45 +0200
commitc25c8a52e2328bcff2f5a35496e7568ee5a7c752 (patch)
treecd624363ad22b7a2b6a76e41bd27c0cd7f5169d7 /vespajlib
parente9e5a422c0aa6364c3c5f7b9da53e9fcf9a5f0f8 (diff)
Vespa global model import
Diffstat (limited to 'vespajlib')
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/serialization/JsonFormat.java23
-rw-r--r--vespajlib/src/test/java/com/yahoo/tensor/serialization/JsonFormatTestCase.java4
2 files changed, 24 insertions, 3 deletions
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 3213982355b..6382361f187 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/serialization/JsonFormat.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/serialization/JsonFormat.java
@@ -1,7 +1,11 @@
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.tensor.serialization;
+import com.yahoo.slime.ArrayTraverser;
import com.yahoo.slime.Cursor;
+import com.yahoo.slime.Inspector;
+import com.yahoo.slime.JsonDecoder;
+import com.yahoo.slime.ObjectTraverser;
import com.yahoo.slime.Slime;
import com.yahoo.tensor.Tensor;
import com.yahoo.tensor.TensorAddress;
@@ -17,9 +21,7 @@ import java.util.Iterator;
// TODO: We should probably move reading of this format from the document module to here
public class JsonFormat {
- /**
- * Serialize the given tensor into JSON format
- */
+ /** Serializes the given tensor into JSON format */
public static byte[] encode(Tensor tensor) {
Slime slime = new Slime();
Cursor root = slime.setObject();
@@ -38,4 +40,19 @@ public class JsonFormat {
addressObject.setString(type.dimensions().get(i).name(), address.label(i));
}
+ /** Deserializes the given tensor from JSON format */
+ // TODO: Add explicit validation (valid() checks) below
+ public static Tensor decode(TensorType type, byte[] jsonTensorValue) {
+ Tensor.Builder tensorBuilder = Tensor.Builder.of(type);
+ Inspector root = new JsonDecoder().decode(new Slime(), jsonTensorValue).get();
+ Inspector cells = root.field("cells");
+ cells.traverse((ArrayTraverser) (__, cell) -> decodeCell(cell, tensorBuilder.cell()));
+ return tensorBuilder.build();
+ }
+
+ private static void decodeCell(Inspector cell, Tensor.Builder.CellBuilder cellBuilder) {
+ cell.field("address").traverse((ObjectTraverser) (dimension, label) -> cellBuilder.label(dimension, label.asString()));
+ cellBuilder.value(cell.field("value").asDouble());
+ }
+
}
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 16af413f2f0..5a025b6eb96 100644
--- a/vespajlib/src/test/java/com/yahoo/tensor/serialization/JsonFormatTestCase.java
+++ b/vespajlib/src/test/java/com/yahoo/tensor/serialization/JsonFormatTestCase.java
@@ -26,6 +26,8 @@ public class JsonFormatTestCase {
"{\"address\":{\"x\":\"c\",\"y\":\"d\"},\"value\":3.0}" +
"]}",
new String(json, StandardCharsets.UTF_8));
+ Tensor decoded = JsonFormat.decode(tensor.type(), json);
+ assertEquals(tensor, decoded);
}
@Test
@@ -44,6 +46,8 @@ public class JsonFormatTestCase {
"{\"address\":{\"x\":\"1\",\"y\":\"1\"},\"value\":7.0}" +
"]}",
new String(json, StandardCharsets.UTF_8));
+ Tensor decoded = JsonFormat.decode(tensor.type(), json);
+ assertEquals(tensor, decoded);
}
}