Skip to content
sholzer edited this page Jun 8, 2017 · 26 revisions

JSON Plug-in

At the moment the plug-in can be used for merge generic JSOn files and Sencha Architect generation depending on the merge strategy defined at the templates.

Trigger Extensions

As for the Sencha Architect generation the input is a java object, the trigger expressions (including matchers and variable assignments) are implemented as Java.

Merger extensions

There are currently these merge strategies:

Generic JSON Merge

  • merge strategy jsonmerge(add the new code respecting the existent is case of conflict)

  • merge strategy jsonmerge_override (add the new code overwriting the existent in case of conflict)

Sencha Architect

  • merge strategy sencharchmerge(add the new code respecting the existent is case of conflict)

  • merge strategy sencharchmerge_override (add the new code overwriting the existent in case of conflict)

    1. JsonArray’s will be ignored / replaced in total

    2. JsonObjects in conclict will be processed recursively ignoring adding non existent elements.

Merge Process

The JSON plugin uses the GSON 2.7 library from Google as parser/interpreter of the JSON code.

The parsed JSON code is stored in a Jsonlement that can be accessed recursively to get all the objects and merge them following the same principles of the Sencha merger. The JSON merger is a little more simple due to only is needed to take into account two node types:

  • JSONObject

  • JSONArray

1. Parsing

The JSON parsed code is stored into a JsonElement. The JsonElement class is extended for the different JSON elements:

JsonParser parser = new JsonParser();
JsonElement jsonBase = parser.parse(new FileReader(file));
JsonObject objBase = jsonBase.getAsJsonObject();

2. Merging

The recursion is made over the JsonObjects in conflict and just overriding in case of JsonArray because, due to the nature of JSON, is impossible to know if the current index of the base array correspond to the same index of the patch array.

The concept for the JSON merger is very similar to the Sencha merger:

3. Outputting

To output the code with format, the library Json 20160810 is used only to build a JSONObject with the resultant code of the merge process and return a formatted code specifying and indentation (4 by default).

String result = jsonObjectMerge(objBase, patchOverrides, objPatch);
JSONObject jsonBase = new JSONObject(new JSONTokener(result));
return jsonBase.toString(4);

Relevant Notes

  • For the Sencha Architect Project generation, the FreeMarker model must have pre-loaded hexadecimal ID’s once CobiGen Eclipse plug-in is initialized. That ID’s (modelId, controllerId, viewId, viewControllerId, viewModelId) are random hexadecimal strings that must follow the format 8-4-4-4-12. These ID’s are used by Sencha Architect to identify at the project the respective generated JSON files. (Currently adding this functionallity to the templates instead)

AST

Clone this wiki locally