Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

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

image text in transcribed

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, 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 numbers, true, false, and null, two values are equal if they are the same. In the "subject code": "CSCI", "course number": 2110, "pre-reqs" "core" true, "course numbe2110, "exclusions" null, "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, "pre-reqs" "subject code" : "CSCI", "course numbe2110, "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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Concepts Of Database Management

Authors: Joy L. Starks, Philip J. Pratt, Mary Z. Last

9th Edition

1337093424, 978-1337093422

More Books

Students also viewed these Databases questions