Design a Java program that compares if two JSON Objects areequal. You must use the sample code and implement least oneof the following: method(s) with recursion, lists, stack.A sample code is provided for assistance.

Sample Code (copyable):

/*
* This is a simple ccanner that breaks up the text into JSONtokens.
* The scanner provides teo constructors and two methods:
* JSONScanner( String s ) : takes a String and creates a stream oftokens
* JSONScanner( InputStream is ) : takes an InputStream object, suchas
* System.in and creates a stream of tokens
* String next() : returns next token in the stream
* String hasNext() : returns true if another token isavailable
*/

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 JSONobject
private int idx = 0; // current index

/* This constructor takes a String, whose contents is to betokenized.
* 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 asSystem.in, whose
* contents is to be tokenized.
* Param: s : InputStream, the contents of which is to betokenized
*/
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 areavailable
*/
public String next() {
for (; idx < obj.length; idx++) {
if (Arrays.binarySearch(singletons,obj[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 < obj.length; idx++) {
if (!Character.isWhitespace(obj[idx])) {
return true;
}
}
return false;
}
  
/* Returns the JSON number in the character stream
* Returns: String: the JSON number
*/
private String getNumber() {
String num = “”;
for (; idx < obj.length; idx++) {
if ((obj[idx] == ‘-‘) || (obj[idx] == ‘+’) || (obj[idx] == ‘e’)||
(obj[idx] == ‘E’) || (obj[idx] == ‘.’) ||
Character.isDigit(obj[idx])) {
num = num + obj[idx];
} else {
return num;
}
}
return num;
}
  
/* Returns the JSON string in the character stream, the methodexits
* if the string is not properly quoted.
* Returns: String: the JSON string
*/
private String getString() {
String str = “””;
idx++;

for (; idx < obj.length; idx++) {
str += obj[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;
}
}

Sample Code (Screenshot-View):

2 This is a simple ccanner that breaks up the text into JSON tokens 3The scanner provides teo constructors and two methods: 4

83 84/* Returns the JSON number in the character stream *Returns: String: the JSON number 85 86 87 private String getNumber)

Background: JSON JSON, the (JavaScript Object Notation) is a widely-used encoding format that makes is easy to exchange data.

In an object, key-value pairs are denoted key : value and multiple key-value pairs are sep- arated by commas (). In JSON, an

Write a program called JSim.java that reads in two JSON objects from the console and determines if they are the equal. Your J

An object is said to be in sorted form if the key-value pairs are in sorted (alphabetical) order and all objects inside the o

2 This is a simple ccanner that breaks up the text into JSON tokens 3The scanner provides teo constructors and two methods: 4SONScanner( String s) : takes a String and creates a stream of tokens 5sONScanner Inpu takes an InputStream object, such as System.in and creates a stream of tokens 7String next() : returns next token in the stream : returns true if another token is available 8String hasNext) 10 11 import java.util.Arrays; 12 import java.util.Scanner; 13 import java.io.Inputstream; 15public class test f 16 final static char [] singletons- 17 18 static [ 19 20 21 22 private char 1 obj 23 private int idx=0; C,’ ,”,,, Arrays.sort (singletons); /I character array containing the JSON object I current index 25 This constructor takes a String, whose contents is to be tokenized 26 27 28 public JSONScanner String s)( Param: String, the contents of which is to be tokenized obj s.toCharArray); 30 31 32 33 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 35 36 37public JSONScanner( InputStream in) 38 Scanner sin- new Scanne in); String s 40while (sin.hasNextline))t 41 42 43 s-sin.nextLine); obj s.toCharArray ); 45 46 47 Returns the next JSON token in the stream 48Returns: String: the next token or exits if no more tokens are available 49 50public String next(O 51for ( idx – 0) { idx++ return +obj[idx 11; } else if ((obj [idx) ) Il Character.isDigit(obj [idx))) { = return getNmberO; else if (objtidx]’) return getStringO; else if (Character.isLetter(obj[idx])) return getLiteral); else if (!Character.iswhitespace(obitidx])) System.err.println(“Unexpected input:”obj[idx]); System.exit(1) 56 58 61 62 63 65 67 System.err.printIn(“Unexpected end of İnput”); System.exit(1); return null; 71 72 Returns true if there is a next JSON token in the stream 73 74 75public boolean hasNext) t 76. for (; idx obj . length; İdx++) { Returns: boolean İf (!Character.ishite space(obj[idx))) return true; { 79 81 82 return false; 83 84/* Returns the JSON number in the character stream *Returns: String: the JSON number 85 86 87 private String getNumber) string num “”; 89for ( idx 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. Show transcribed image text