A — JSON
Due Tuesday, 10 September 2024, 11:59pm
Purpose This first assignment has multiple learning goals:
T to deal with inputs from STDIN and outputs to STDOUT;
T to explore the JSON libraries of the chosen programming language, specifically its stream-parsing capabilities;
S to re-learn pair-programming with your new partner; and
S to live up to basic specifications.
Delivery You must deliver xjson and Tests/ in a repo-level directory called A in your assigned git repo. The two artifacts are specified in the Task and Tests sections below.
The xjson program must run at the shell command prompt as ./xjson. Our test harness will run the program as a sub-process, meaning your shell environment does not exist. In our experience, it is thus best to write xjson as a (Posix) shell script that invokes your program directly (say, Python or Racket source) or runs a compiled executable (say, a Java jar file or a C .o file).
All auxiliary files must be put into a sub-directory called A/Other/.
Task Develop xjson. The program consumes a stream of well-formed and valid JSON from STDIN and The word “well-formed” means that each input is formed according to the JSON grammar. The word “valid” means that it also satisfies all of the special shape and value constraints spelled out below. produces a stream of JSON responses. The task of xjson is to incrementally build a data representation of a directory tree as specified via the input elements. When queried, xjson writes the state of the directory to STDOUT as a JSON representation.
- "quit" |
INTERPRETATION xjson shuts down |
NOTE The STDIN stream may contain additional characters beyond "quit", |
but those are not guaranteed to be valid or even well-formed. |
|
- "query" |
INTERPRETATION xjson writes out a Directory representation |
|
- {"path" : PathString, "size" : Natural} |
INTERPRETATION xjson adds the file Name part of the |
PathString as a file to the path specified by the prefix of PathString |
up to the last name |
|
- {"path" : PathString, "subs" : [Name, ..., Name]} |
INTERPRETATION xjson adds the specified sub-directories (as empty |
folders) to the specified path |
Of course, if a sub-directory already exists, there is nothing to add. |
|
CONSTRAINT If the given directory path specification requests the replacement of a file with a sub-directory, the command request is ignored. |
|
CONSTRAINT If the directory path of a PathString contains a |
sub-directory name that does not yet exist, xjson ignores the command request. |
|
A PathString is a non-empty String that |
- starts with "/" |
- consists of Names separated by "/" |
- such that two consecutive "/" are separated by at least one letter |
- such that the entire string is less than 200 characters long. |
INTERPRETATION Every Name inside a PathString specifies a |
sub-directory relative to the current one, except for the last part which |
may be a file name |
EXAMPLE "/" always points to the 'root' of the Directory. |
|
A Name is a non-empty String that consists of letters "a" through "z" only. |
- such that the entire string is less than 200 characters long. |
|
|
A Natural is a natural number, as introduced in the Fundamentals courses. |
A Directory is an object whose keys are Names and whose values |
are one of: |
- Natural |
|
EXAMPLE xjson uses a data representation of the empty directory when |
its starts processing commands. |
For example, if STDIN contains the following sequence of JSON values,
{"path":"/","subs":["foo","bar"]} |
"query" |
then ./xjson displays this JSON:
{"bar":{},"foo":{}} |
to STDOUT.
Or, if STDIN contains these JSON values,
{"path":"/","subs":["foo","bar"]} |
{"path":"/bar/foodotrkt","size":3} |
"query" |
{"path":"/bar/foodotrkt","size":4} |
"query" |
"quit" |
{"path":"/bar/foodotrkt","size":5} |
then ./xjson displays this JSON:
{"bar":{"foodotrkt":3},"foo":{}} |
{"bar":{"foodotrkt":4},"foo":{}} |
to STDOUT.
Tests Place one test for xjson in the directory Tests/.
You should never refer to anything as a test that doesn’t specify both inputs and expected results.
A test case always consists of given inputs and expected outputs. For this
course, a test consists of a pair of files: n-in.json, the input file, and
n-out.json, the expected output file, where n is an integer between 0
and the requested number of tests (exclusive).—
Note Our test harness compares expected JSON and actual JSON via a JSON-equality predicate, so white space or ordering of fields in objects does not matter.