Nyzo tech→Release notes→Nyzo 628: JSON refactoring
Nyzo 628: JSON refactoring
Nyzo version 628
(commit on GitHub) refactors some JSON code and fixes a bug in the JSON parser.
This version affects the client primarily.
The CommandTable class now implements the Json
Renderable interface. The CommandTable.renderJson() method, which is
the sole method of needed to implement the interface, renders a CommandTable to
JSON with code that was previously contained in the SimpleExecutionResult.json
ForCommandTable() method.
The SimpleExecutionResult.toEndpointResponse() method now calls
the toJson() method in its new location in the Json
Renderer class.
The toJson() method was removed from the Simple
ExecutionResult class. This method is now contained in the Json
Renderer class.
The following image shows the remainder of the toJson() method that was removed
from the SimpleExecutionResult class.
The jsonForCollection() method was also moved from the
SimpleExecutionResult class to the Json
Renderable class.
The jsonForArray() method was also moved from the
SimpleExecutionResult class to the Json
Renderable class.
As noted above, the SimpleExecutionResult.jsonForCommand
Table() method became the CommandTable.renderJson() method.
The escapeStringForJson() method was also moved from the
SimpleExecutionResult class to the Json
Renderer class.
In the Json.parseJsonArray() method, conditions were added to properly parse
values not contained in quotes, such as [0, 2].
The JsonArray class now implements the Json
Renderable interface, implementing its renderJson() method to produce a
String representation of its contents.
The JsonObject class also now implements the Json
Renderable interface, also implementing its renderJson() method to produce
a String representation of its contents.
The JsonRenderable interface declares a single method:
renderJson(). This interface should be implemented by any class that does not want
the structure of its public interface to define how it is rendered into JSON.
The JsonRenderer.toJson() method was moved from the
SimpleExecutionResult class. Modifications were made to support the more
general cases of JsonRenderable objects instead of the more specific cases of
CommandTable objects that were implemented previously.
The fallback case in the JsonRenderer.toJson() method is unchanged from the
previous implementation in the SimpleExecutionResult class.
The JsonRenderer.jsonForCollection() method is unchanged from its
previous implementation in the SimpleExecutionResult class.
The JsonRenderer.jsonForArray() method is also unchanged from its
previous implementation in the SimpleExecutionResult class.
The JsonRenderer.escapeStringForJson() method is also unchanged
from its previous implementation in the SimpleExecutionResult class.
The JsonTest is a typical implementation of the Nyzo
Test interface. The main() method runs the test independently.
The run() method, required by the NyzoTest interface,
invokes two other methods: testDeserialization() and
testFrozenEdgeCommand().
The getFailureCause() method, also required by the
NyzoTest interface, returns a human-readable description of the source of test
failure.
The JsonTest.testDeserialization() method first tests two simple JSON
objects, one with a single key and value, followed by one with five keys and five values.
Next, the JsonTest.testDeserialization() method deserializes a hard-coded
result from the client with some arrays and nesting.
The result object in a client result is contained within an array. All values are checked. For simplicity, no type
conversions are performed in this test.
Finally, a simple array of numbers is tested. This is the test that revealed the bug that was fixed in the
Json.parseJsonArray() method.
The JsonTest.testFrozenEdgeCommand() method creates an instance of
the client's FrozenEdgeCommand, serializes its result, then deserializes the
JSON string and checks the values in the JsonObject that is produced.
This method first creates a Block and sets it as the Block
Manager's frozen edge in order to produce a predictable result.
The FrozenEdgeCommand is run, its API result is rendered as a
byte array, and that byte array is converted back to a
String. That String is then parsed to a
JsonObject.
The notices and errors arrays are both confirmed to be
present and empty.
Finally, all of the values in the nested result object are checked against the expected values.
A JsonTest instance was added to the array of tests in
TestUtil.
The HtmlTag class contains a small change to catch and print errors in the
Javascript produced by the ajaxUpdate() method.