- COMP.CS.140
- 7. Packages
- 7.3 ⌛⌛ JSON
⌛⌛ JSON¶
JSON is a system independent file format to store data and to transfer data between systems. If you are not yet familiar with JSON, you can earn more, for example, from Wikipedia.
JSON data consists of following types of data items:
Number. In this task type
double.Truth value. In this task type
boolean.String. In this task type
String.Null. In this task the
nullreference.Array. An ordered list that may contain zero or more JSON data items.
Object. A dictionary type structure that may contain zero or more key–item pairs. The keys are strings.
In this task, you will familiarise yourself with JSON by implementing a class hierarchy for
JSON data items. The readily provided Node.java contains an abstract class Node
that acts as the base class for JSON data items. This class is available at the remote
material repository (round7/json/Node.java). The class Node provides functions isValue,
isArray and isObject for inspecting the Node type and function printJson
for the printing of the data items.
You need to implement concrete subclasses ValueNode, ArrayNode and ObjectNode that
inherit Node. Place your code into files named ArrayNode.java,
ObjectNode.java and ValueNode.java in the directory round7/json of your local
repository.
You do not need to create a Maven project for this task. If you do, copy your classes into
the round7/json directory of your local repository and make a commit before submitting
your answer.
Implement the classes as follows:
ValueNodestores a number, truth value, string ornull. Public members:Default constructor
ValueNode()that initialises the object by storing thenullin the object.Constructors
ValueNode(double value),ValueNode(boolean value)andValueNode(String value)initialize the object to store the value of corresponding type. It is assumed thatnullis not passed as a parameter value to theValueNode(String value)constructor.Member functions
isNumber(),isBoolean(),isString()andisNull()that return truth values indicating whether the stored value is of the corresponding type or not. For example,isStringreturnstrue, if and only if the stored value is a string.Member functions
double getNumber(),boolean getBoolean(),String getString()andObject getNull()that return the corresponding value. For examplegetBooleanreturns the value stored by the object as aboolean, and it is the responsibility of the caller to first callisBooleanto check that the value really can be returned as a truth value.
The
ArrayNodeclass storesNodeobjects. Properties and public members:Implements the interface
Iterable<Node>.A default constructor that initializes an empty array that does not contain any
Nodeobjects yet.Member function
void add(Node node)that addsNodeto the end of the array.Member function
int size()that returns the number of storedNodeobjects.
ObjectNodethat stores key–value pairs, where keys are strings and values areNodeobjects. Properties and public members:Implements the interface
Iterable<String>. Iterates over the keys of the stored key–value pairs in natural sorted order ofString.A default constructor that initializes an empty JSON object that does not have any key-value pairs yet.
Member function
Node get(String key)that returns theNodeobject corresponding tokeyornull, if it does not exits. Please, note that the “plain”nullis here an error code, while aValueNodecontaining thenullvalue is a valid return value.Member function
void set(String key, Node node)that adds a key–value pair described by the parameters. A possible earlier value for the key will be overwritten.Member function
int size()that returns the number of storedNodeobjects.
The implementations of ArrayNode and ObjectNode can be simple, if you use the Java
containers in a wisely.
Testing¶
You may test your implementation by using the test program given in the file JsonTest.java and
the example output given in the files output1.txt, output2.txt, output3.txt and
output4.txt. Place these files to the same directory with your code, compile the test
program, for example, as javac *.java, and run the tests as java JsonTest 1, java JsonTest 2,
java JsonTest 3, and java JsonTest 4. The expected outputs of these four tests are given
in the files output1.txt, output2.txt, output3.txt and output4.txt.
A+ presents the exercise submission form here.