Generic JSON Tree Parser Plugin

Plugin source location: <serge_root>/lib/Serge/Engine/Plugin/parse_json.pm

This plugin is used to parse arbitrary JSON data structures. It uses regular expressions as a configuration parameter to match translatable nodes in the JSON object tree, and to identify the nodes whose content needs to be treated as HTML which needs to be parsed additionally using the parse_php_xhtml parser.

If your JSON is a plain dictionary in "key" : "value" format, where all keys need to be translated, use the parse_json_keyvalue plugin instead.

This is a validating parser, but it parses JSON in a relaxed mode. Specifically, it allows commas after the last element of the array or dictionary, and hash-style comments (see JSON.pm documentation for more information). In case JSON format validation fails, the plugin can send an error report to specified recipients. If no email settings are provided, it will simply report the error in the console output.

Code Examples

products.json
{ "description" => "Product list", "products": [ { "sku": "P001", "price": 1.23, "title": "First Product" "description": "First Product Description" }, { "sku": "P002", "price": 2.34, "title": "Second Product" "description": "Second Product Description" }, //... ], //... }

Please see the example configuration file below to learn why only title and certain description nodes are extracted here for translation.

Node Paths

When JSON document is parsed, each node in it is given its path, and this path is what path_matches and path_doesnt_match regular expressions should match against (see the example configuration file). This is how the paths are constructed, given the example JSON file above:

products.json (internal 'path => value' representation)
description => Product list products[0]/sku => P001 products[0]/price => 1.23 products[0]/title => First Product products[0]/description => First Product Description products[1]/sku => P001 products[1]/price => 1.23 products[1]/title => Second Product products[1]/description => Second Product Description //...

The constructed path to a node is also extracted as a hint along with the corresponding string.

Usage

example-project.serge