aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib
diff options
context:
space:
mode:
authorHåkon Hallingstad <hakon@verizonmedia.com>2020-04-08 19:11:33 +0200
committerHåkon Hallingstad <hakon@verizonmedia.com>2020-04-08 19:11:33 +0200
commit71f0d4bd2ce9f005ac9e9d06bc6fe893f370e38e (patch)
tree34ea5e6ab55b8ba187f276d232d2480376d84b73 /vespajlib
parent49413ece8a980c07bf4906233cd221c41d2dc91d (diff)
Validate deserialization of flag data
Diffstat (limited to 'vespajlib')
-rw-r--r--vespajlib/abi-spec.json3
-rw-r--r--vespajlib/pom.xml10
-rw-r--r--vespajlib/src/main/java/com/yahoo/text/JSON.java23
-rw-r--r--vespajlib/src/test/java/com/yahoo/text/JSONTest.java64
4 files changed, 92 insertions, 8 deletions
diff --git a/vespajlib/abi-spec.json b/vespajlib/abi-spec.json
index 04f859e2802..7c9f1f31bff 100644
--- a/vespajlib/abi-spec.json
+++ b/vespajlib/abi-spec.json
@@ -2859,7 +2859,8 @@
],
"methods": [
"public static java.lang.String encode(java.util.Map)",
- "public static java.lang.String escape(java.lang.String)"
+ "public static java.lang.String escape(java.lang.String)",
+ "public static boolean equals(java.lang.String, java.lang.String)"
],
"fields": []
},
diff --git a/vespajlib/pom.xml b/vespajlib/pom.xml
index 7631a2af0fb..1b23d024d29 100644
--- a/vespajlib/pom.xml
+++ b/vespajlib/pom.xml
@@ -39,6 +39,11 @@
<scope>provided</scope>
</dependency>
<dependency>
+ <groupId>com.fasterxml.jackson.core</groupId>
+ <artifactId>jackson-databind</artifactId>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
<groupId>com.yahoo.vespa</groupId>
<artifactId>annotations</artifactId>
<version>${project.version}</version>
@@ -73,11 +78,6 @@
<version>${project.version}</version>
<scope>test</scope>
</dependency>
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-databind</artifactId>
- <scope>test</scope>
- </dependency>
</dependencies>
<build>
diff --git a/vespajlib/src/main/java/com/yahoo/text/JSON.java b/vespajlib/src/main/java/com/yahoo/text/JSON.java
index cfff16c9aba..4091930ed0e 100644
--- a/vespajlib/src/main/java/com/yahoo/text/JSON.java
+++ b/vespajlib/src/main/java/com/yahoo/text/JSON.java
@@ -1,15 +1,23 @@
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.text;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
import java.util.Map;
+import java.util.Objects;
+
+import static com.yahoo.yolean.Exceptions.uncheck;
/**
- * Static methods for working with the map textual format which is parsed by {@link MapParser}
+ * Static mthods for working with JSON.
*
* @author bratseth
*/
public final class JSON {
+ private static final ObjectMapper mapper = new ObjectMapper();
+
/** No instances */
private JSON() {}
@@ -56,4 +64,17 @@ public final class JSON {
return b != null ? b.toString() : s;
}
+ /**
+ * Test whether two JSON strings are equal, e.g. the order of fields in an object is irrelevant.
+ *
+ * <p>When comparing two numbers of the two JSON strings, the result is only guaranteed to be
+ * correct if (a) both are integers (without fraction and exponent) and each fits in a long, or
+ * (b) both are non-integers, were syntactically identical, and fits in a double.</p>
+ */
+ public static boolean equals(String left, String right) {
+ JsonNode leftJsonNode = uncheck(() -> mapper.readTree(left));
+ JsonNode rightJsonNode = uncheck(() -> mapper.readTree(right));
+ return Objects.equals(leftJsonNode, rightJsonNode);
+ }
+
}
diff --git a/vespajlib/src/test/java/com/yahoo/text/JSONTest.java b/vespajlib/src/test/java/com/yahoo/text/JSONTest.java
index 22174761571..bba36a7cadd 100644
--- a/vespajlib/src/test/java/com/yahoo/text/JSONTest.java
+++ b/vespajlib/src/test/java/com/yahoo/text/JSONTest.java
@@ -2,11 +2,15 @@
package com.yahoo.text;
import org.junit.Test;
-import static org.junit.Assert.assertEquals;
import java.util.LinkedHashMap;
import java.util.Map;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertTrue;
+
/**
* @author bratseth
*/
@@ -22,4 +26,62 @@ public class JSONTest {
assertEquals("{\"a \\\"key\\\"\":3,\"key2\":\"value\",\"key3\":3.3}", JSON.encode(map));
}
+ @Test
+ public void testEquals() {
+ assertTrue(JSON.equals("{}", "{}"));
+
+ // Whitespace is irrelevant
+ assertTrue(JSON.equals("{}", "\n{ }"));
+
+ // Order of fields in object is irrelevant
+ assertTrue(JSON.equals("{\"a\":0, \"c\":1}", "{\"c\":1, \"a\":0}"));
+
+ // Order of elements of array is significant
+ assertFalse(JSON.equals("[\"a\",\"b\"]", "[\"b\",\"c\"]"));
+
+ // Verify null-valued fields are not ignored
+ assertFalse(JSON.equals("{\"a\":null}", "{}"));
+
+ // Current impl uses BigInteger if integer doesn't fit in a long.
+ assertEquals(9223372036854775807L, Long.MAX_VALUE);
+ assertTrue(JSON.equals("{\"a\": 9223372036854775807}", "{\"a\": 9223372036854775807}"));
+
+ // double 1.0 and int 1 are different
+ assertTrue(JSON.equals( "{\"a\": 1}", "{\"a\": 1}"));
+ assertTrue(JSON.equals( "{\"a\": 1.0}", "{\"a\": 1.0}"));
+ assertFalse(JSON.equals( "{\"a\": 1.0}", "{\"a\": 1}"));
+
+ // Double-precision on numbers. Constant from Math.E.
+ assertTrue(JSON.equals( "{\"e\": 2.71828182845904}", "{\"e\": 2.71828182845904}"));
+
+ // Justification of above float values
+ double e1 = 2.7182818284590452354;
+ double e2 = 2.718281828459045;
+ double e3 = 2.71828182845904;
+ assertEquals(e1, Math.E, -1);
+ assertEquals(e1, e2, -1);
+ assertNotEquals(e1, e3, -1);
+
+ // Impl uses BigInteger
+ assertTrue(JSON.equals( "{\"a\": 92233720368547758070}",
+ "{\"a\": 92233720368547758070}"));
+ assertFalse(JSON.equals("{\"a\": 92233720368547758070}",
+ "{\"a\": 92233720368547758071}"));
+
+ // Impl converts to double and ignores extraneous digits
+ assertTrue(JSON.equals( "{\"e\": 2.7182818284590452354}",
+ "{\"e\": 2.7182818284590452354}"));
+ assertTrue(JSON.equals( "{\"e\": 2.7182818284590452354}",
+ "{\"e\": 2.7182818284590452355}"));
+ assertFalse(JSON.equals("{\"e\": 2.7182818284590452354}",
+ "{\"e\": 2.71828182845904}"));
+
+ // Misc impl defined results
+ assertFalse(JSON.equals("{\"a\": 1.0}", "{\"a\":1}"));
+ assertTrue(JSON.equals("{\"a\": 1.0}", "{\"a\":1.00}"));
+ assertTrue(JSON.equals("{\"a\": 1.0}", "{\"a\":1.0000000000000000000000000000}"));
+ assertTrue(JSON.equals("{\"a\": 10.0}", "{\"a\":1e1}"));
+ assertTrue(JSON.equals("{\"a\": 1.2}", "{\"a\":12e-1}"));
+ }
+
}