Write a Java program that compares if two JSON Objects are equal. You must use the sample code and least one of the following :
Write a Java program that compares if two JSON Objects are equal. You must use the sample code and least one of the following: method(s) with recursion, lists, stack. A sample code is provided for assistance.
Sample Code:
/* * This is a simple ccanner that breaks up the text into JSON tokens. * The scanner provides teo constructors and two methods: * JSONScanner( String s ) : takes a String and creates a stream of tokens * JSONScanner( InputStream is ) : takes an InputStream object, such as * System.in and creates a stream of tokens * String next() : returns next token in the stream * String hasNext() : returns true if another token is available */
import java.util.Arrays; import java.util.Scanner; import java.io.InputStream;
public class test { final static char [] singletons = { '{', '}', '[', ']', ',', ':' };
static { Arrays.sort(singletons); }
private char [] obj; // character array containing the JSON object private int idx = 0; // current index
/* This constructor takes a String, whose contents is to be tokenized. * Param: s : String, the contents of which is to be tokenized */ public JSONScanner( String s ) { obj = s.toCharArray(); }
/* This constructor takes a InputStream object (such as System.in, whose * contents is to be tokenized. * Param: s : InputStream, the contents of which is to be tokenized */ public JSONScanner( InputStream in ) { Scanner sin = new Scanner(in); String s = ""; while (sin.hasNextLine()) { s = s + sin.nextLine(); } obj = s.toCharArray(); }
/* Returns the next JSON token in the stream. * Returns: String: the next token or exits if no more tokens are available */ public String next() { for (; idx = 0) { idx++; return "" + obj[idx - 1]; } else if ((obj[idx] == '-') || Character.isDigit(obj[idx])) { return getNumber(); } else if (obj[idx] == '"') { return getString(); } else if (Character.isLetter(obj[idx])) { return getLiteral(); } else if (!Character.isWhitespace(obj[idx])) { System.err.println("Unexpected input: " + obj[idx]); System.exit(1); } } System.err.println("Unexpected end of input"); System.exit(1); return null; } /* Returns true if there is a next JSON token in the stream. * Returns: boolean */ public boolean hasNext() { for (; idx
for (; idx
if (obj[idx] == '') { idx++; str += obj[idx]; } else if (obj[idx] == '"') { idx++; return str; } } System.err.println("Unexpected end of input"); System.exit(1); return null; }
/* Returns the JSON literal in (null, true, false) * Returns: String: the JSON literal */ private String getLiteral() { String str = ""; while (Character.isLetter(obj[idx])) { str += obj[idx]; idx++; } switch (str) { case "null": case "true": case "false": break; default: System.err.println("Unexpected input: " + str); System.exit(1); } return str; } }
Background: JSON JSON, the (JavaScript Object Notation) is a widely-used encoding format that makes is easy to exchange data. JSON is a text-based (human readable) format, which makes it ideal for many applications as well as easy to debug. For this assignment, you will need to research the JSON format on the web. For example: https://www.json.org. A brief summary is provided here, but you are encouraged to do more in-depth research. JSON encodes data using objects, arrays, and primitive values. A JSON object is encoded as zero or more key-value pairs, where the key is a string and a value can be an object, an array, a string, a number, true, false, or null. A string is any piece of text enclosed in double quotes, e.g., "Hello World" and a number is either an integer or a decimal value e.g., 42 and 3.14. Objects begin with an left brace (), contain zero or more key-value pairs and end with a right brace(), e.g., "subject code" "CSCI" , "course number" : 2110, pre-reqs" "CSCI 1110", "CSCI 1101" "exclusions": null, "core" : true, "textbook" : 1 "author": "S. Venugopal", "title""Data Structures Outside In with Java", "publisher" : Prentice Hall", "year" 2007 "ISBN-10":"0-13-198619-8" In an object, key-value pairs are denoted key : value and multiple key-value pairs are sep- arated by commas (). In JSON, an array is a sequence of values of any kind. An array begins with a left square bracket (C), has zero or more values, separated by commas, and ends with a right square bracket(]). In the example above the array contains two strings If you are not not clear on the structure of JSON, your first task is to review a JSON tutorial, such as the one here: https://www.tutorialspoint.com/json/index.htm Problem: JSON Object Equality Determining if two JSON objects are equal is a common and useful operation. Two JSON objects are equal if they have the same set of key-value pairs (not necessarily in the same order), where for each key, the values are equal. In the case of primitive values such as strings, numbers, true, false, and null, two values are equal if they are the same. In the case of arrays, two arrays are equal if both arrays have the same number of elements and they are pairwise equa Le., for each array element, the values stored in both arrays are equal For example, the following two figures are examples of equal and unequal objects "subject code" "CSCI", "course number" 2110 "core" true, "course numbe2110, "exclusions": null, "pre-reqs" "pre-reqs" "CSCI 1110", "CSCI 1101" "CSCI 1110", "CSCI 1101" "exclusions": null, "core" true "subject code" "CSCI" Figure 1: These JSON objects are equal. "subject code": "CSCI", "course number" 2110 "subject code" : "CSCI", "course numbe2110, "pre-reqs" "pre-reqs" "CSCI 1110", "CSCI 1101" "CSCI 1101" "CSCI 1110", "exclusions": null, "core" true "exclusions": null, "core" true Figure 2: These JSON objects are not equal, because the arrays are not equal. Your task is to create a program to determine if two JSON objects are equal Write a program called JSim.java that reads in two JSON objects from the console and determines if they are the equal. Your JSim.java program must contain the main) methood where your program starts running Input The input consists of two JSON objects, one after the other. The input is via the console System.in). You may assume that there will be no error in the input Reading Input with JSONScanner The provided JSONScanner class can be used to read in the JSON objects from the console This class has a next) method, just like the standard Scanner class. But, instead of returning the next word in the input, the next) method of the JSONScanner class returns the next token in the input. A token is one of token description or example |token description or example left brace string"Hello World" number 42 or 3.14 right brace [left square bracket ] right square bracket true false null colon comma The use of JSONScanner class is similar to the Scanner class. First, instantiate an object from the class, passing in System.in. Then, repeatedly call next ) on this object to get one token after the other (see example below). The hasNext ) method is used to check if another token is available public void main (String args) JSONScanner scanner - new JSDNScanner (System.in); String token -scanner .next); Processing The input will contain exactly two JSON objects, one after the other Two objects are equal if they have the same set of key-value pairs, which may be in different orders. Two arrays are equal if they are of the same size and are pair-wise equal Otherwise, two values are equal if they are the same An object is said to be in sorted form if the key-value pairs are in sorted (alphabetical) order and all objects inside the object are also in sorted form. For example, in Figure1, the object on the left is not in sortedform, while the object on the right is in sorted form. Output If the two objects are equal your program should output one of the objects in sorted form (formatted as described below), followed by The objects are equal If the two objects are not equal your program should output both objects in sorted form (formatted as described below), followed by The objects are not equal Object Output Format When outputting an object, the left and right braces should be on separate lines, with the key-value pairs indented two spaces. There should be single spaces between the key, the colon, and the value. If the value is an object or an array, the left brace or bracket should be on the same line. All the key-value pairs or values should be indented two more spaces. The right brace or bracket should be on separate line, with the same indentation as the key value pair. A comma separating key-value pairs in an object or values in an array should immediately follow the key-value pair or value. Please see Figures 1 and 2 for examples. Examples Sample Input Sample Output "subject code" "CSCI" "course number1110, "pre-reqs" core: true, "course number"1110, pre-reqs" "core" true subject code" "CSCI" The objects are equal "subject code": "CSCI" "course number1110. "core": true, "pre-reqs" Sample Input Sample Output "subject code" cSCI" "course ber": 1110. "pre-requisies" "core: true, course number": 1110, "exclusions: null pre-requisies" "exclusions" null "core" : true "subject code" "CSCI "subject code" cSCI" "course number"1110 "core" true, pre-reqs" "core: true "course umbr" 1110, "exclusions: null "exclusions":null "subject code" "CSCI" The objects are not equal Hints and Suggestions You will either need to use recursion or a stack (or both) to solve this problem. Java has both stacks (java, utils. Stack) and lists (java" utils. ArrayList) built in. As well as binary search (java.utils.Arrays) . The sample solution uses a 2-pass algorithm. The first pass reads in the two JSON objects and stores them. This can be done either iteratively with a stack or recursively The second pass determines if the two objects are the same. This is easier to do recursively. The third pass prints out the first or both objects, and is also easier to do recursively. Note: The solution cannot be done in a single pass. Background: JSON JSON, the (JavaScript Object Notation) is a widely-used encoding format that makes is easy to exchange data. JSON is a text-based (human readable) format, which makes it ideal for many applications as well as easy to debug. For this assignment, you will need to research the JSON format on the web. For example: https://www.json.org. A brief summary is provided here, but you are encouraged to do more in-depth research. JSON encodes data using objects, arrays, and primitive values. A JSON object is encoded as zero or more key-value pairs, where the key is a string and a value can be an object, an array, a string, a number, true, false, or null. A string is any piece of text enclosed in double quotes, e.g., "Hello World" and a number is either an integer or a decimal value e.g., 42 and 3.14. Objects begin with an left brace (), contain zero or more key-value pairs and end with a right brace(), e.g., "subject code" "CSCI" , "course number" : 2110, pre-reqs" "CSCI 1110", "CSCI 1101" "exclusions": null, "core" : true, "textbook" : 1 "author": "S. Venugopal", "title""Data Structures Outside In with Java", "publisher" : Prentice Hall", "year" 2007 "ISBN-10":"0-13-198619-8" In an object, key-value pairs are denoted key : value and multiple key-value pairs are sep- arated by commas (). In JSON, an array is a sequence of values of any kind. An array begins with a left square bracket (C), has zero or more values, separated by commas, and ends with a right square bracket(]). In the example above the array contains two strings If you are not not clear on the structure of JSON, your first task is to review a JSON tutorial, such as the one here: https://www.tutorialspoint.com/json/index.htm Problem: JSON Object Equality Determining if two JSON objects are equal is a common and useful operation. Two JSON objects are equal if they have the same set of key-value pairs (not necessarily in the same order), where for each key, the values are equal. In the case of primitive values such as strings, numbers, true, false, and null, two values are equal if they are the same. In the case of arrays, two arrays are equal if both arrays have the same number of elements and they are pairwise equa Le., for each array element, the values stored in both arrays are equal For example, the following two figures are examples of equal and unequal objects "subject code" "CSCI", "course number" 2110 "core" true, "course numbe2110, "exclusions": null, "pre-reqs" "pre-reqs" "CSCI 1110", "CSCI 1101" "CSCI 1110", "CSCI 1101" "exclusions": null, "core" true "subject code" "CSCI" Figure 1: These JSON objects are equal. "subject code": "CSCI", "course number" 2110 "subject code" : "CSCI", "course numbe2110, "pre-reqs" "pre-reqs" "CSCI 1110", "CSCI 1101" "CSCI 1101" "CSCI 1110", "exclusions": null, "core" true "exclusions": null, "core" true Figure 2: These JSON objects are not equal, because the arrays are not equal. Your task is to create a program to determine if two JSON objects are equal Write a program called JSim.java that reads in two JSON objects from the console and determines if they are the equal. Your JSim.java program must contain the main) methood where your program starts running Input The input consists of two JSON objects, one after the other. The input is via the console System.in). You may assume that there will be no error in the input Reading Input with JSONScanner The provided JSONScanner class can be used to read in the JSON objects from the console This class has a next) method, just like the standard Scanner class. But, instead of returning the next word in the input, the next) method of the JSONScanner class returns the next token in the input. A token is one of token description or example |token description or example left brace string"Hello World" number 42 or 3.14 right brace [left square bracket ] right square bracket true false null colon comma The use of JSONScanner class is similar to the Scanner class. First, instantiate an object from the class, passing in System.in. Then, repeatedly call next ) on this object to get one token after the other (see example below). The hasNext ) method is used to check if another token is available public void main (String args) JSONScanner scanner - new JSDNScanner (System.in); String token -scanner .next); Processing The input will contain exactly two JSON objects, one after the other Two objects are equal if they have the same set of key-value pairs, which may be in different orders. Two arrays are equal if they are of the same size and are pair-wise equal Otherwise, two values are equal if they are the same An object is said to be in sorted form if the key-value pairs are in sorted (alphabetical) order and all objects inside the object are also in sorted form. For example, in Figure1, the object on the left is not in sortedform, while the object on the right is in sorted form. Output If the two objects are equal your program should output one of the objects in sorted form (formatted as described below), followed by The objects are equal If the two objects are not equal your program should output both objects in sorted form (formatted as described below), followed by The objects are not equal Object Output Format When outputting an object, the left and right braces should be on separate lines, with the key-value pairs indented two spaces. There should be single spaces between the key, the colon, and the value. If the value is an object or an array, the left brace or bracket should be on the same line. All the key-value pairs or values should be indented two more spaces. The right brace or bracket should be on separate line, with the same indentation as the key value pair. A comma separating key-value pairs in an object or values in an array should immediately follow the key-value pair or value. Please see Figures 1 and 2 for examples. Examples Sample Input Sample Output "subject code" "CSCI" "course number1110, "pre-reqs" core: true, "course number"1110, pre-reqs" "core" true subject code" "CSCI" The objects are equal "subject code": "CSCI" "course number1110. "core": true, "pre-reqs" Sample Input Sample Output "subject code" cSCI" "course ber": 1110. "pre-requisies" "core: true, course number": 1110, "exclusions: null pre-requisies" "exclusions" null "core" : true "subject code" "CSCI "subject code" cSCI" "course number"1110 "core" true, pre-reqs" "core: true "course umbr" 1110, "exclusions: null "exclusions":null "subject code" "CSCI" The objects are not equal Hints and Suggestions You will either need to use recursion or a stack (or both) to solve this problem. Java has both stacks (java, utils. Stack) and lists (java" utils. ArrayList) built in. As well as binary search (java.utils.Arrays) . The sample solution uses a 2-pass algorithm. The first pass reads in the two JSON objects and stores them. This can be done either iteratively with a stack or recursively The second pass determines if the two objects are the same. This is easier to do recursively. The third pass prints out the first or both objects, and is also easier to do recursively. Note: The solution cannot be done in a single passStep by Step Solution
There are 3 Steps involved in it
Step: 1
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started