List of usage examples for com.fasterxml.jackson.databind JsonNode equals
public abstract boolean equals(Object paramObject);
From source file:json_cmp.Comparer.java
public static void main(String[] args) { System.out.println("Testing Begin"); try {//from w w w . j ava2s .c o m String accessLogFolder = "/Users/herizhao/workspace/accessLog/"; // String yqlFileName = "json_cmp/test1.log"; // String yqlpFileName = "json_cmp/test2.log"; String yqlFileName = "tempLog/0812_yql.res"; String yqlpFileName = "tempLog/0812_yqlp.res"; ReadResults input1 = new ReadResults(accessLogFolder + yqlFileName); ReadResults input2 = new ReadResults(accessLogFolder + yqlpFileName); Integer diffNum = 0; Integer errorCount = 0; Integer totalIDNum1 = 0; Integer totalIDNum2 = 0; Integer equalIDwithDuplicate = 0; Integer beacons = 0; Integer lineNum = 0; Integer tempCount = 0; HashMap<String, IDclass> IDarray = new HashMap<String, IDclass>(); FileOutputStream fos = new FileOutputStream( "/Users/herizhao/workspace/accessLog/json_cmp/cmp_result.txt"); OutputStreamWriter osw = new OutputStreamWriter(fos); BufferedWriter bw = new BufferedWriter(osw); FileOutputStream consoleStream = new FileOutputStream( "/Users/herizhao/workspace/accessLog/json_cmp/console"); OutputStreamWriter consoleOSW = new OutputStreamWriter(consoleStream); BufferedWriter console = new BufferedWriter(consoleOSW); while (true) { input1.ReadNextLine(); if (input1.line == null) break; input2.ReadNextLine(); if (input2.line == null) break; while (input1.line.equals("")) { lineNum++; input1.ReadNextLine(); input2.ReadNextLine(); } if (input2.line == null) break; if (input1.line == null) break; lineNum++; System.out.println("lineNum = " + lineNum); String str1 = input1.line; String str2 = input2.line; ObjectMapper mapper1 = new ObjectMapper(); ObjectMapper mapper2 = new ObjectMapper(); JsonNode root1 = mapper1.readTree(str1); JsonNode root2 = mapper2.readTree(str2); JsonNode mediaNode1 = root1.path("query").path("results").path("mediaObj"); JsonNode mediaNode2 = root2.path("query").path("results").path("mediaObj"); if (mediaNode2.isMissingNode() && !mediaNode1.isMissingNode()) tempCount += mediaNode1.size(); //For yqlp if (mediaNode2.isArray()) { totalIDNum2 += mediaNode2.size(); for (int i = 0; i < mediaNode2.size(); i++) { ObjectNode mediaObj = (ObjectNode) mediaNode2.get(i); mediaObj.put("yvap", ""); JsonNode streamsNode = mediaObj.path("streams"); //streams if (streamsNode.isArray()) { for (int j = 0; j < streamsNode.size(); j++) { ObjectNode streamsObj = (ObjectNode) streamsNode.get(j); changeStreamsPath(streamsObj); ChangedHost(streamsObj); //if(streamsObj.path("h264_profile").isMissingNode()) streamsObj.put("h264_profile", ""); if (streamsObj.path("is_primary").isMissingNode()) streamsObj.put("is_primary", false); } } //meta if (!mediaObj.path("meta").isMissingNode()) { ObjectNode metaObj = (ObjectNode) mediaObj.path("meta"); changeMetaThumbnail(metaObj); if (metaObj.path("show_name").isMissingNode()) metaObj.put("show_name", ""); if (metaObj.path("event_start").isMissingNode()) metaObj.put("event_start", ""); if (metaObj.path("event_stop").isMissingNode()) metaObj.put("event_stop", ""); //if(metaObj.path("credits").path("label").isMissingNode()) ((ObjectNode) metaObj.path("credits")).put("label", ""); } //Metrics -> plidl & isrc changeMetrics(mediaObj); } } //For yql if (mediaNode1.isArray()) { totalIDNum1 += mediaNode1.size(); for (int i = 0; i < mediaNode1.size(); i++) { JsonNode mediaObj = mediaNode1.get(i); ((ObjectNode) mediaObj).put("yvap", ""); //Meta //System.out.println("meta: "); if (!mediaObj.path("meta").isMissingNode()) { ObjectNode metaObj = (ObjectNode) mediaObj.path("meta"); changeMetaThumbnail(metaObj); metaObj.put("event_start", ""); metaObj.put("event_stop", ""); FloatingtoInt(metaObj, "duration"); if (metaObj.path("show_name").isMissingNode()) metaObj.put("show_name", ""); //System.out.println("thub_dem: "); if (!metaObj.path("thumbnail_dimensions").isMissingNode()) { ObjectNode thub_demObj = (ObjectNode) metaObj.path("thumbnail_dimensions"); FloatingtoInt(thub_demObj, "height"); FloatingtoInt(thub_demObj, "width"); } ((ObjectNode) metaObj.path("credits")).put("label", ""); } //Visualseek //System.out.println("visualseek: "); if (!mediaObj.path("visualseek").isMissingNode()) { ObjectNode visualseekObj = (ObjectNode) mediaObj.path("visualseek"); FloatingtoInt(visualseekObj, "frequency"); FloatingtoInt(visualseekObj, "width"); FloatingtoInt(visualseekObj, "height"); //visualseek -> images, float to int JsonNode imagesNode = visualseekObj.path("images"); if (imagesNode.isArray()) { for (int j = 0; j < imagesNode.size(); j++) { ObjectNode imageObj = (ObjectNode) imagesNode.get(j); FloatingtoInt(imageObj, "start_index"); FloatingtoInt(imageObj, "count"); } } } //Streams //System.out.println("streams: "); JsonNode streamsNode = mediaObj.path("streams"); if (streamsNode.isArray()) { for (int j = 0; j < streamsNode.size(); j++) { ObjectNode streamsObj = (ObjectNode) streamsNode.get(j); FloatingtoInt(streamsObj, "height"); FloatingtoInt(streamsObj, "bitrate"); FloatingtoInt(streamsObj, "duration"); FloatingtoInt(streamsObj, "width"); changeStreamsPath(streamsObj); ChangedHost(streamsObj); // if(streamsObj.path("h264_profile").isMissingNode()) streamsObj.put("h264_profile", ""); if (streamsObj.path("is_primary").isMissingNode()) streamsObj.put("is_primary", false); } } //Metrics -> plidl & isrc changeMetrics(mediaObj); } } //Compare if (mediaNode2.isArray() && mediaNode1.isArray()) { for (int i = 0; i < mediaNode2.size() && i < mediaNode1.size(); i++) { JsonNode mediaObj1 = mediaNode1.get(i); JsonNode mediaObj2 = mediaNode2.get(i); if (!mediaObj1.equals(mediaObj2)) { if (!mediaObj1.path("id").toString().equals(mediaObj2.path("id").toString())) { errorCount++; } else { Integer IFdiffStreams = 0; Integer IFdiffMeta = 0; Integer IFdiffvisualseek = 0; Integer IFdiffMetrics = 0; Integer IFdifflicense = 0; Integer IFdiffclosedcaptions = 0; String statusCode = ""; MetaClass tempMeta = new MetaClass(); if (!mediaObj1.path("status").equals(mediaObj2.path("status"))) { JsonNode statusNode1 = mediaObj1.path("status"); JsonNode statusNode2 = mediaObj2.path("status"); if (statusNode2.path("code").toString().equals("\"100\"") || (statusNode1.path("code").toString().equals("\"400\"") && statusNode1.path("code").toString().equals("\"404\"")) || (statusNode1.path("code").toString().equals("\"200\"") && statusNode1.path("code").toString().equals("\"200\"")) || (statusNode1.path("code").toString().equals("\"200\"") && statusNode1.path("code").toString().equals("\"403\""))) statusCode = ""; else statusCode = "yql code: " + mediaObj1.path("status").toString() + " yqlp code:" + mediaObj2.path("status").toString(); } else {//Status code is 100 if (!mediaObj1.path("streams").equals(mediaObj2.path("streams"))) IFdiffStreams = 1; if (!tempMeta.CompareMeta(mediaObj1.path("meta"), mediaObj2.path("meta"), lineNum)) IFdiffMeta = 1; if (!mediaObj1.path("visualseek").equals(mediaObj2.path("visualseek"))) IFdiffvisualseek = 1; if (!mediaObj1.path("metrics").equals(mediaObj2.path("metrics"))) { IFdiffMetrics = 1; JsonNode metrics1 = mediaObj1.path("metrics"); JsonNode metrics2 = mediaObj2.path("metrics"); if (!metrics1.path("beacons").equals(metrics2.path("beacons"))) beacons++; } if (!mediaObj1.path("license").equals(mediaObj2.path("license"))) IFdifflicense = 1; if (!mediaObj1.path("closedcaptions").equals(mediaObj2.path("closedcaptions"))) IFdiffclosedcaptions = 1; } if (IFdiffStreams + IFdiffMeta + IFdiffvisualseek + IFdiffMetrics + IFdifflicense + IFdiffclosedcaptions != 0 || !statusCode.equals("")) { String ID_str = mediaObj1.path("id").toString(); if (!IDarray.containsKey(ID_str)) { IDclass temp_IDclass = new IDclass(ID_str); temp_IDclass.addNum(IFdiffStreams, IFdiffMeta, IFdiffvisualseek, IFdiffMetrics, IFdifflicense, IFdiffclosedcaptions, lineNum); if (!statusCode.equals("")) temp_IDclass.statusCode = statusCode; IDarray.put(ID_str, temp_IDclass); } else { IDarray.get(ID_str).addNum(IFdiffStreams, IFdiffMeta, IFdiffvisualseek, IFdiffMetrics, IFdifflicense, IFdiffclosedcaptions, lineNum); if (!statusCode.equals("")) IDarray.get(ID_str).statusCode = statusCode; } IDarray.get(ID_str).stream.CompareStream(IFdiffStreams, mediaObj1.path("streams"), mediaObj2.path("streams"), lineNum); if (!IDarray.get(ID_str).metaDone) { IDarray.get(ID_str).meta = tempMeta; IDarray.get(ID_str).metaDone = true; } } else equalIDwithDuplicate++; } } else equalIDwithDuplicate++; } } bw.flush(); console.flush(); } //while System.out.println("done"); bw.write("Different ID" + " " + "num "); bw.write(PrintStreamsTitle()); bw.write(PrintMetaTitle()); bw.write(PrintTitle()); bw.newLine(); Iterator<String> iter = IDarray.keySet().iterator(); while (iter.hasNext()) { String key = iter.next(); bw.write(key + " "); bw.write(IDarray.get(key).num.toString() + " "); bw.write(IDarray.get(key).stream.print()); bw.write(IDarray.get(key).meta.print()); bw.write(IDarray.get(key).print()); bw.newLine(); //System.out.println(key); } //System.out.println("different log num = " + diffNum); //System.out.println("same log num = " + sameLogNum); System.out.println("Different ID size = " + IDarray.size()); // System.out.println("streamEqual = " + streamEqual); // System.out.println("metaEqual = " + metaEqual); // System.out.println("metricsEqual = " + metricsEqual); // System.out.println("visualseekEqual = " + visualseekEqual); // System.out.println("licenseEqual = " + licenseEqual); // System.out.println("closedcaptionsEqualEqual = " + closedcaptionsEqual); System.out.println(tempCount); System.out.println("beacons = " + beacons); System.out.println("equalIDwithDuplicate = " + equalIDwithDuplicate); System.out.println("Total ID num yql (including duplicates) = " + totalIDNum1); System.out.println("Total ID num yqpl (including duplicates) = " + totalIDNum2); System.out.println("Error " + errorCount); bw.close(); console.close(); } catch (IOException e) { } }
From source file:io.fabric8.maven.core.util.JSONUtil.java
public static boolean equals(JSONObject first, JSONObject second) { final ObjectMapper mapper = new ObjectMapper(); try {/*from w w w . j av a2 s .c o m*/ final JsonNode tree1 = mapper.readTree(first.toString()); final JsonNode tree2 = mapper.readTree(second.toString()); return tree1.equals(tree2); } catch (IOException e) { return false; } }
From source file:com.pros.jsontransform.filter.ArrayFilterEquals.java
public static boolean evaluate(final JsonNode filterNode, final JsonNode elementNode, final ObjectTransformer transformer) throws ObjectTransformerException { boolean result = false; JsonNode filterArguments = filterNode.get(ArrayFilter.$EQUALS.name().toLowerCase()); JsonNode valueNode = transformer.transformValueNode(elementNode, filterArguments); JsonNode likeNode = filterArguments.path(ArrayFilter.ARGUMENT_WHAT); if (valueNode.equals(likeNode)) { result = true;/*w w w. j av a2s.com*/ } return result; }
From source file:com.sangupta.comparator.JSONComparer.java
/** * Compare two JSON string representations. * //w ww . ja v a2 s. c o m * @param json1 * the first representation * * @param json2 * the second representation * * @return <code>true</code> if the two JSON representations represent the * same object, <code>false</code> otherwise. * * @throws JsonParseException * if something fails * * @throws JsonProcessingException * if something fails * * @throws IOException * if something fails * */ public static boolean compareJson(String json1, String json2) throws JsonProcessingException, IOException { if (json1 == null || json2 == null) { return false; } if (json1 == json2) { return true; } JsonFactory factory = new JsonFactory(); factory.enable(Feature.ALLOW_COMMENTS); ObjectMapper mapper = new ObjectMapper(); JsonNode node1 = mapper.readTree(json1); JsonNode node2 = mapper.readTree(json2); return node1.equals(node2); }
From source file:com.sangupta.comparator.JSONComparer.java
/** * Compare two JSON representations.//from w w w . j ava2 s . c om * * @param reader1 * the first representation * * @param reader2 * the second representation * * @return <code>true</code> if the two JSON representations represent the * same object, <code>false</code> otherwise. * * @throws JsonParseException * if something fails * * @throws JsonProcessingException * if something fails * * @throws IOException * if something fails * */ public static boolean compareJson(Reader reader1, Reader reader2) throws JsonProcessingException, IOException { if (reader1 == null || reader2 == null) { return false; } if (reader1 == reader2) { return true; } JsonFactory factory = new JsonFactory(); factory.enable(Feature.ALLOW_COMMENTS); ObjectMapper mapper = new ObjectMapper(); JsonNode node1 = mapper.readTree(reader1); JsonNode node2 = mapper.readTree(reader2); return node1.equals(node2); }
From source file:com.sangupta.comparator.JSONComparer.java
/** * Compare two JSON representations./* w ww . j av a 2 s .com*/ * * @param stream1 * the first representation * * @param stream2 * the second representation * * @return <code>true</code> if the two JSON representations represent the * same object, <code>false</code> otherwise. * * @throws JsonParseException * if something fails * * @throws JsonProcessingException * if something fails * * @throws IOException * if something fails * */ public static boolean compareJson(InputStream stream1, InputStream stream2) throws JsonProcessingException, IOException { if (stream1 == null || stream2 == null) { return false; } if (stream1 == stream2) { return true; } JsonFactory factory = new JsonFactory(); factory.enable(Feature.ALLOW_COMMENTS); ObjectMapper mapper = new ObjectMapper(); JsonNode node1 = mapper.readTree(stream1); JsonNode node2 = mapper.readTree(stream2); return node1.equals(node2); }
From source file:com.pros.jsontransform.constraint.ConstraintValues.java
public static void validate(final JsonNode constraintNode, final JsonNode resultNode, final ObjectTransformer transformer) throws ObjectTransformerException { boolean found = false; JsonNode valuesArray = constraintNode.get(Constraint.$VALUES.toString()); if (valuesArray.isArray()) { for (JsonNode valueNode : valuesArray) { if (resultNode.equals(valueNode)) { found = true;// ww w.j ava2s . com break; } } } if (!found) { throw new ObjectTransformerException("Constraint violation [" + Constraint.$VALUES.toString() + "]" + " on transform node " + transformer.getTransformNodeFieldName()); } }
From source file:com.flipkart.zjsonpatch.JsonDiff.java
private static void generateDiffs(List<Diff> diffs, List<Object> path, JsonNode source, JsonNode target) { if (!source.equals(target)) { final NodeType sourceType = NodeType.getNodeType(source); final NodeType targetType = NodeType.getNodeType(target); if (sourceType == NodeType.ARRAY && targetType == NodeType.ARRAY) { //both are arrays compareArray(diffs, path, source, target); } else if (sourceType == NodeType.OBJECT && targetType == NodeType.OBJECT) { //both are json compareObjects(diffs, path, source, target); } else {//from w w w . j a v a2 s . c om //can be replaced diffs.add(Diff.generateDiff(Operation.REPLACE, path, target)); } } }
From source file:com.github.fge.jackson.JsonNumEquals.java
private static boolean numEquals(final JsonNode a, final JsonNode b) { /*// w ww. j a v a 2s . c o m * If both numbers are integers, delegate to JsonNode. */ if (a.isIntegralNumber() && b.isIntegralNumber()) return a.equals(b); /* * Otherwise, compare decimal values. */ return a.decimalValue().compareTo(b.decimalValue()) == 0; }
From source file:com.ikanow.aleph2.core.shared.utils.BatchRecordUtils.java
/** Pattern match to check equality between the different Json containers * @param json//w w w .jav a2 s . c o m * @param content * @param o2 * @return */ protected static boolean isEquals(JsonNode json, ByteArrayOutputStream content, Object o2) { return Patterns.match(o2).<Boolean>andReturn().when(j -> null == j, __ -> false) .when(BatchRecord.class, j -> json.equals(j._json) && (null == content) && (null == j._content)) .when(JsonBatchRecord.class, j -> json.equals(j._json) && (null == content)).otherwise(() -> false); }