Nyzo techRelease notesNyzo 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.

RN_628 image 0

The SimpleExecutionResult.toEndpointResponse() method now calls the toJson() method in its new location in the Json Renderer class.

RN_628 image 1

The toJson() method was removed from the Simple ExecutionResult class. This method is now contained in the Json Renderer class.

RN_628 image 2

The following image shows the remainder of the toJson() method that was removed from the SimpleExecutionResult class.

RN_628 image 3

The jsonForCollection() method was also moved from the SimpleExecutionResult class to the Json Renderable class.

RN_628 image 4

The jsonForArray() method was also moved from the SimpleExecutionResult class to the Json Renderable class.

RN_628 image 5

As noted above, the SimpleExecutionResult.jsonForCommand Table() method became the CommandTable.renderJson() method.

RN_628 image 6

The escapeStringForJson() method was also moved from the SimpleExecutionResult class to the Json Renderer class.

RN_628 image 7

In the Json.parseJsonArray() method, conditions were added to properly parse values not contained in quotes, such as [0, 2].

RN_628 image 8

The JsonArray class now implements the Json Renderable interface, implementing its renderJson() method to produce a String representation of its contents.

RN_628 image 9

The JsonObject class also now implements the Json Renderable interface, also implementing its renderJson() method to produce a String representation of its contents.

RN_628 image 10

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.

RN_628 image 11

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.

RN_628 image 12

The fallback case in the JsonRenderer.toJson() method is unchanged from the previous implementation in the SimpleExecutionResult class.

RN_628 image 13

The JsonRenderer.jsonForCollection() method is unchanged from its previous implementation in the SimpleExecutionResult class.

RN_628 image 14

The JsonRenderer.jsonForArray() method is also unchanged from its previous implementation in the SimpleExecutionResult class.

RN_628 image 15

The JsonRenderer.escapeStringForJson() method is also unchanged from its previous implementation in the SimpleExecutionResult class.

RN_628 image 16

The JsonTest is a typical implementation of the Nyzo Test interface. The main() method runs the test independently.

RN_628 image 17

The run() method, required by the NyzoTest interface, invokes two other methods: testDeserialization() and testFrozenEdgeCommand().

RN_628 image 18

The getFailureCause() method, also required by the NyzoTest interface, returns a human-readable description of the source of test failure.

RN_628 image 19

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.

RN_628 image 20

Next, the JsonTest.testDeserialization() method deserializes a hard-coded result from the client with some arrays and nesting.

RN_628 image 21

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.

RN_628 image 22

Finally, a simple array of numbers is tested. This is the test that revealed the bug that was fixed in the Json.parseJsonArray() method.

RN_628 image 23

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.

RN_628 image 24

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.

RN_628 image 25

The notices and errors arrays are both confirmed to be present and empty.

RN_628 image 26

Finally, all of the values in the nested result object are checked against the expected values.

RN_628 image 27

A JsonTest instance was added to the array of tests in TestUtil.

RN_628 image 28

The HtmlTag class contains a small change to catch and print errors in the Javascript produced by the ajaxUpdate() method.

RN_628 image 29