List of usage examples for com.fasterxml.jackson.databind JsonNode equals
public abstract boolean equals(Object paramObject);
From source file:com.flipkart.zjsonpatch.JsonDiff.java
private static void compareArray(List<Diff> diffs, List<Object> path, JsonNode source, JsonNode target) { List<JsonNode> lcs = getLCS(source, target); int srcIdx = 0; int targetIdx = 0; int lcsIdx = 0; int srcSize = source.size(); int targetSize = target.size(); int lcsSize = lcs.size(); int pos = 0;// w w w. j a v a 2s . c om while (lcsIdx < lcsSize) { JsonNode lcsNode = lcs.get(lcsIdx); JsonNode srcNode = source.get(srcIdx); JsonNode targetNode = target.get(targetIdx); if (lcsNode.equals(srcNode) && lcsNode.equals(targetNode)) { // Both are same as lcs node, nothing to do here srcIdx++; targetIdx++; lcsIdx++; pos++; } else { if (lcsNode.equals(srcNode)) { // src node is same as lcs, but not targetNode //addition List<Object> currPath = getPath(path, pos); diffs.add(Diff.generateDiff(Operation.ADD, currPath, targetNode)); pos++; targetIdx++; } else if (lcsNode.equals(targetNode)) { //targetNode node is same as lcs, but not src //removal, List<Object> currPath = getPath(path, pos); diffs.add(Diff.generateDiff(Operation.REMOVE, currPath, srcNode)); srcIdx++; } else { List<Object> currPath = getPath(path, pos); //both are unequal to lcs node generateDiffs(diffs, currPath, srcNode, targetNode); srcIdx++; targetIdx++; pos++; } } } while ((srcIdx < srcSize) && (targetIdx < targetSize)) { JsonNode srcNode = source.get(srcIdx); JsonNode targetNode = target.get(targetIdx); List<Object> currPath = getPath(path, pos); generateDiffs(diffs, currPath, srcNode, targetNode); srcIdx++; targetIdx++; pos++; } pos = addRemaining(diffs, path, target, pos, targetIdx, targetSize); removeRemaining(diffs, path, pos, srcIdx, srcSize, source); }
From source file:com.squarespace.template.JsonUtils.java
/** * Compare two JsonNode objects and return an integer. * * @return a negative integer, zero, or a positive integer as this object * is less than, equal to, or greater than the specified object. *///from w ww.ja va 2s . com public static int compare(JsonNode left, JsonNode right) { if (left.isLong() || left.isInt()) { return Long.compare(left.asLong(), right.asLong()); } else if (left.isDouble() || left.isFloat()) { return Double.compare(left.asDouble(), right.asDouble()); } else if (left.isTextual()) { return left.asText().compareTo(right.asText()); } else if (left.isBoolean()) { return Boolean.compare(left.asBoolean(), right.asBoolean()); } // Not comparable in a relative sense, default to equals. return left.equals(right) ? 0 : -1; }
From source file:org.candlepin.test.TestUtil.java
public static boolean isJsonEqual(String one, String two) throws JsonProcessingException, IOException { ObjectMapper mapper = new ObjectMapper(); JsonNode tree1 = mapper.readTree(one); JsonNode tree2 = mapper.readTree(two); return tree1.equals(tree2); }
From source file:com.cloudmine.api.rest.JsonUtilities.java
/** * Tests whether two json strings are equivalent; ignores formating and order. Expensive operation * as the strings are parsed to JsonNodes, which are compared. * @param first/*w w w . java2 s. c om*/ * @param second * @return true if first and second are equivalent JSON objects * @throws ConversionException if unable to convert first or second to JsonNodes */ public static boolean isJsonEquivalent(String first, String second) throws ConversionException { if (first == null) return second == null; if (second == null) return false; try { JsonNode firstNode = jsonMapper.readTree(first); try { JsonNode secondNode = jsonMapper.readTree(second); return firstNode.equals(secondNode); } catch (IOException e) { throw new ConversionException("Couldn't convert second string to json: " + second, e); } } catch (IOException e) { throw new ConversionException("Couldn't convert first string to json: " + first, e); } }
From source file:com.google.api.server.spi.tools.JacksonUtil.java
/** * Safely merge two nodes. This will appropriately merge objects or lists, as * well as verifying that values are compatible if both nodes are values. If * {@code throwOnConflict} is set, an exception will be thrown if there is a merge conflict. * Otherwise, node1 will be returned as the conflict resolution. *///from www . ja v a 2s .c o m public static JsonNode mergeNode(JsonNode node1, JsonNode node2, boolean throwOnConflict) { if (node1.isArray()) { if (!node2.isArray()) { if (throwOnConflict) { throw new IllegalArgumentException("Cannot merge array and non-array: " + node1 + ", " + node2); } return node1; } return mergeArray((ArrayNode) node1, (ArrayNode) node2); } else if (node1.isObject()) { if (!node2.isObject()) { if (throwOnConflict) { throw new IllegalArgumentException( "Cannot merge object and non-object: " + node1 + ", " + node2); } return node1; } return mergeObject((ObjectNode) node1, (ObjectNode) node2, throwOnConflict); } else { // Value node, verify equivalence. if (throwOnConflict && !node1.equals(node2)) { throw new IllegalArgumentException("Cannot merge different values: " + node1 + ", " + node2); } return node1; } }
From source file:io.sidecar.query.UserAnswerBucketJsonValidationTest.java
@Test(description = "Asserts that serializing a UserAnswerBucket containing RawEventsAnswers produces the proper" + "json") public void testRawEventsAnswerBucketSerialization() throws Exception { JsonNode expectedJson = mapper/*w ww . j a v a 2 s . c o m*/ .readTree(this.getClass().getResource("/raw_events_answer_bucket_response.json")); Event event = mapper.readValue(expectedJson.path("answer").path("events").get(0).traverse(), Event.class); RawEventsAnswer rawEventsAnswer = RawEventsAnswer.fromEvents(event); UserAnswerBucket<RawEventsAnswer> uab = new UserAnswerBucket<>( UUID.fromString(expectedJson.path("userId").asText()), expectedJson.path("deviceId").asText(), rawEventsAnswer); JsonNode actualJson = mapper.readTree(mapper.writeValueAsBytes(uab)); assertTrue(actualJson.equals(expectedJson)); }
From source file:de.jlo.talendcomp.json.JsonComparator.java
/** * Checks if the array contains the given value * @param array the array which perhaps contains the value * @param value the value to search for/*ww w.jav a 2s. c om*/ * @return true or false */ public boolean contains(ArrayNode array, JsonNode value) { for (int i = 0, n = array.size(); i < n; i++) { JsonNode node = array.get(i); if (node.equals(value)) { return true; } } return false; }
From source file:org.opendaylight.sfc.sbrest.provider.task.SbRestSfstTaskTest.java
@Test public void testSbRestSfstTask1() throws IOException { SbRestSfstTask sbRestSfstTask = new SbRestSfstTask(RestOperation.DELETE, this.buildServiceFunctionSchedulerType(), executorService); JsonNode jsonObject = mapper.readTree(sbRestSfstTask.jsonObject); assertTrue("Must be true", jsonObject.equals(buildServiceFunctionSchedulerTypeObjectNode())); assertTrue("Must be true", sbRestSfstTask.restUriList.get(0).contains(SFST_REST_URI)); }
From source file:org.opendaylight.sfc.sbrest.provider.task.SbRestSfstTaskTest.java
@Test // SbRestAbstractClass creates string jsonObject from dataObject (service function scheduler // type) & set Rest uri list // contain of jsonObject also depends on rest operation // this jsonObject is then compared with object node created in this class public void testSbRestSfstTask() throws IOException { SbRestSfstTask sbRestSfstTask = new SbRestSfstTask(RestOperation.PUT, this.buildServiceFunctionSchedulerType(), executorService); JsonNode jsonObject = mapper.readTree(sbRestSfstTask.jsonObject); assertTrue("Must be true", jsonObject.equals(this.buildServiceFunctionSchedulerTypeObjectNode())); assertTrue("Must be true", sbRestSfstTask.restUriList.get(0).contains(SFST_REST_URI)); }
From source file:org.opendaylight.sfc.sbrest.provider.task.SbRestSfTaskTest.java
@Test public void testSbRestSfTask() throws IOException { SbRestSfTask sbRestSfTask = new SbRestSfTask(RestOperation.PUT, this.buildServiceFunction(), executorService);//from www. j a v a 2 s . c o m JsonNode jsonObject = mapper.readTree(sbRestSfTask.jsonObject); assertTrue("Must be true", jsonObject.equals(this.buildServiceFunctionObjectNode())); assertTrue("Must be true", sbRestSfTask.restUriList.get(0).contains(REST_URI)); }