diff options
author | Håkon Hallingstad <hakon@verizonmedia.com> | 2020-04-08 19:11:33 +0200 |
---|---|---|
committer | Håkon Hallingstad <hakon@verizonmedia.com> | 2020-04-08 19:11:33 +0200 |
commit | 71f0d4bd2ce9f005ac9e9d06bc6fe893f370e38e (patch) | |
tree | 34ea5e6ab55b8ba187f276d232d2480376d84b73 /vespajlib/src/main/java/com/yahoo/text | |
parent | 49413ece8a980c07bf4906233cd221c41d2dc91d (diff) |
Validate deserialization of flag data
Diffstat (limited to 'vespajlib/src/main/java/com/yahoo/text')
-rw-r--r-- | vespajlib/src/main/java/com/yahoo/text/JSON.java | 23 |
1 files changed, 22 insertions, 1 deletions
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); + } + } |