Improved Array Functions and JSON

0 views
Skip to first unread message

Bill Farrell

unread,
Aug 5, 2016, 9:21:43 AM8/5/16
to InterSystems: MV Community
I've been working with the array functions I posted earlier and found a few annoyances in practice that I've since fixed. The array functions offer some handy PHP-like extensions, like .append(), .shift(), .unshift(), push(), .split(), and .pop(). Array shift and unshift work identically to PHP's array functions.

The first version of the JSON class didn't have an encode function. I acid-tested it yesterday and fixed a couple of annoyances in that. This JSON class differs from the ZEN counterpart in that there is no property-mapping necessary. It will accept a random hunk of JSON and parse it into nested arrays, exactly mirroring the original JSON structure. This is handy during development when you may encounter a whole new data transfer scenario. It's possible to use cascading-dot (or -> in MV) syntax to dig into the structure.

I'm working a lot with JQuery and didn't want to have to map JSON nodes to object properties every time something wiggled. (During development, that happens a lot.) I can now work with JSON in its original structure, just viewing it as nested datatypes, either as an array-like or list-like structure.

The unit test first parses a sample blob of JSON and hands back a nested structure. Another test decodes parses another JSON object into nested arrays (testing a nested structure. The third case turns a nested Cache array/list structure into JSON. Test case four takes that result and turns it back into a nested array/list structure. The encode/decode functions now can start and end up with identical structures. There is no need to map JSON to a specific class with specific properties.

Of course, these aren't as fully-fleshed as the original ZEN JSON mapper but they aren't intended to be. What they offer instead is some fairly mindless use; crank something in one end and its counterpart comes out the other.

These classes are part of the EasyCSP site-building suite so they're open source and free to use. EasyCSP RC 1 is ready for best testing; there are a couple of maintenance pages left to write. Otherwise, I'm super tickled at how it's turning up and how easy it is to work with.

The stand-alone classes are attached.

Bill

core_component_json_and_arrays.xml
Reply all
Reply to author
Forward
0 new messages