Last active
May 7, 2018 07:04
-
-
Save Nirupg63/8c9877e0c2bbe6bd50382980e89d156c to your computer and use it in GitHub Desktop.
Simple java implementation that checks if two JSON Objects are equal and returns the inequal data as a JSON message.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.json.JSONObject; | |
import java.util.HashMap; | |
import java.util.Map; | |
public class Main { | |
public static void main(String[] args) { | |
// write your code here | |
String sample1 = "{\"a\":\"test\",\"b\":\"test\",\"msg\":{\"data1\":\"valid\",\"data2\":\"valid\",\"data3\":\"valid\",\"data4\":\"valid\"}}"; | |
String sample2 = "{\"a\":\"test\",\"b\":\"test\",\"msg\":{\"data1\":\"valid\",\"data2\":\"valid\",\"data3\":\"valid\",\"data4\":\"invalid\"}}"; | |
String sample3 = "{\"a\":\"test\",\"b\":\"test\",\"msg\":{\"data1\":\"valid\",\"data2\":\"invalid\",\"data3\":\"invalid\",\"data4\":\"invalid\"}}"; | |
String sample4 = "{\"a\":\"test\",\"b\":\"test\",\"msg\":{\"data2\":\"valid\",\"data1\":\"valid\",\"data4\":\"valid\",\"data3\":\"valid\"}}"; | |
String sample5 = "{\"a\":\"test\",\"b\":\"test\",\"msg\":{\"data1\":\"valid\",\"data2\":\"valid\",\"data3\":\"valid\"}}"; | |
String sample6 = "{\"a\":\"test\",\"b\":\"test\",\"msg\":{\"data1\":\"valid\",\"data2\":\"valid\",\"data4\":\"invalid\"}}"; | |
String sample7 = "{\"a\":\"test\",\"b\":\"test\",\"msg\":{\"data1\":\"valid\",\"data2\":\"valid\",\"data4\":\"invalid\",\"data5\":\"valid\"}}"; | |
JSONObject validJSON = new JSONObject(sample1); | |
JSONObject invalidJSON = new JSONObject(sample2); | |
JSONObject multiInvalidJSON = new JSONObject(sample3); | |
JSONObject unsortedValidJSON = new JSONObject(sample4); | |
JSONObject unequalValidJSON = new JSONObject(sample5); | |
JSONObject unequalInValidJSON = new JSONObject(sample6); | |
JSONObject differentInValidJSON = new JSONObject(sample7); | |
Main main = new Main(); | |
// To test sorted validity | |
JSONObject result1 = main.evalJSON(validJSON, validJSON); | |
System.out.println("Testing equality " + result1.toString()); | |
// To test invalidity | |
JSONObject result2 = main.evalJSON(validJSON, invalidJSON); | |
System.out.println("Testing inequality " + result2.toString()); | |
// To test multiple invalidity | |
JSONObject result3 = main.evalJSON(validJSON, multiInvalidJSON); | |
System.out.println("Testing inequality " + result3.toString()); | |
// To test unsorted validity | |
JSONObject result4 = main.evalJSON(validJSON, unsortedValidJSON); | |
System.out.println("Testing unsorted equality " + result4.toString()); | |
// To test unequal valid sets | |
JSONObject result5 = main.evalJSON(validJSON, unequalValidJSON); | |
System.out.println("Testing unequal validity " + result5.toString()); | |
// To test unequal invalid sets | |
JSONObject result6 = main.evalJSON(validJSON, unequalInValidJSON); | |
System.out.println("Testing unequal validity " + result6.toString()); | |
// To test unique invalid sets | |
JSONObject result7 = main.evalJSON(validJSON, differentInValidJSON); | |
System.out.println("Testing different validity " + result7.toString()); | |
} | |
private JSONObject evalJSON(JSONObject sample1, JSONObject sample2) { | |
boolean validity = true; | |
JSONObject msg1 = sample1.getJSONObject("msg"); | |
JSONObject msg2 = sample2.getJSONObject("msg"); | |
HashMap<String, Object> rejectPile = new HashMap<>(); | |
if (msg1.hashCode() == msg2.hashCode()) { | |
System.out.println("The two messages are equal"); | |
} else { | |
System.out.println("The two messages are inequal"); | |
Map<String, Object> proofMap = msg1.toMap(); | |
Map<String, Object> comparisonMap = msg2.toMap(); | |
for (Object o : proofMap.entrySet()) { | |
Map.Entry entry = (Map.Entry) o; | |
String key = (String) entry.getKey(); | |
Object value = entry.getValue(); | |
if (comparisonMap.get(key) != null && !comparisonMap.get(key).equals(value)) { | |
validity = false; | |
rejectPile.put(key, comparisonMap.get(key)); | |
} | |
} | |
} | |
JSONObject resultJSON = new JSONObject(); | |
resultJSON.put("result", validity); | |
resultJSON.put("mismatch", rejectPile); | |
return resultJSON; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment