This course has already ended.

⌛⌛⌛ JSON

Place your code into files named (a template is provided for it),, and in the directory Round6/json. Remember to pull student_template_project for material.

In this task you will implement a simple class hierarchy for creating JSON data. If you are not yet familiar with JSON, it is high time to learn about it e.g. from Wikipedia.

JSON data consists of following types of data items:

  • Numbero. In this task type double.

  • Truth value. In this task type boolean.

  • String. In this task type String.

  • Null. In this task the null reference.

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

The readily provided contains an abstract class Node that acts as the common sypertype of JSON data items. You need to implement classes ValueNode, ArrayNode and ObjectNode that inherit Node and are otherwise as follows:

  • ValueNode stores a number, truth value, string or null. Public members:

    • Constructors ValueNode(double value), ValueNode(boolean value) and ValueNode(String value).

      • Initialize the object to store the value of one of the accepted types. Note that the last constructor allows also to store null (which could not be specified as a primitive value).

    • Member functions isNumber(), isBoolean(), isString() and isNull() that return truth values telling whether the stored value is of the corresponding type. E.g. isString returns true if and only if the stored value is a string.

    • Member functions double getNumber(), boolean getBoolean() and String getString() that return the value (which is assumed to be of the corresponding type). E.g. getNumber returns a stored number as a double, and it is the responsibility of the caller to first call isNumber to check that the value really can be returned as a number.

  • ArrayNode stores zero or more Node objects. Properties and public members:

    • Implements the interface Iterable<Node>.

    • A default constructor that initializes an empty array (no Node objects yet).

    • Member function void add(Node node) that adds Node to the end of the array.

    • Member function int size() that returns the number of stored Node objects.

  • ObjectNode that stores zero or more key-value pairs where keys are strings and values are Node objects. Properties and public members:

    • Implements the interface Iterable<String>.

      • Iterates over the keys of the stored key-value pairs in natural sorted order of String.

    • A default constructor that initializes an empty JSON object (has no key-value pairs yet).

    • Member function Node get(String key) that returns the Node object corresponding to key (or null if it does not exits).

    • 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 stored Node objects.

The implementations of ArrayNode and ObjectNode can be extremely simple if you use Java containers in a wise manner.

The class Node contains a function printSimple that prints a crude description of the JSON data represented by the Node object it receives as a parameter. The class also contains a function printJson that does not have a proper implementation. You may receive up to 30 points if you implement printJson in such manner that it prints out a “nice” JSON data representation. If you skip this part, you may receive up to 20 points. See the example outputs for details about what kind of output printJson should produce.

The class Node also already provides functions isValue, isArray and isObject for inspecting the Node type.


You may test your implementation by using the test program given in the file and the example output given in the files output1.txt, output2.txt, output3.txt, output4.txt, output5.txt and output6.txt. Place these files to the same directory with your code, compile the test program e.g. as javac *.java, and run the tests as java JsonTest 1 simple, java JsonTest 2 simple, java JsonTest 3 simple, java JsonTest 4 simple, java JsonTest 3 json and java JsonTest 4 json. The expected outputs of these six tests are given in the files output1.txt, output2.txt, output3.txt, output4.txt, output5.txt and output6.txt. The first four tests use printSimple and the last two tests use printJson. If you implement printJson, see the last two example output files for details about the expected output format. Below is also an example of how printJson would print the JSON data of the second test:

  "address": {
    "city": "Tampere",
    "country": "Finland"
  "faculties": [
      "name": "Faculty of Built Environment"
      "name": "Faculty of Education and Culture"
      "name": "Faculty of Engineering and Natural Sciences"
      "name": "Faculty of Information Technology and Communication Sciences"
      "name": "Faculty of Management and Business"
      "name": "Faculty of Medicine and Health Technology"
      "name": "Faculty of Social Sciences"
  "name": "Tampere University"

A+ presents the exercise submission form here.

Posting submission...