Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a Java program that compares if two JSON Objects are equal. You must use the sample code and implement least one of the following

Write a Java program that compares if two JSON Objects are equal. You must use the sample code and implement least one of the following: method(s) with recursion, lists, stack. A sample code is provided for assistance. Additionally, follow the Object Output Format.

Sample Code (Screenshot-View):

image text in transcribed

image text in transcribed

Sample Code (copyable):

/* * 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; } }

image text in transcribed

Sample Output & Input:

image text in transcribed

image text in transcribed

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 - 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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions