tree: f3b7919fd009e7bcb3608c69869ef6d2daa086e1 [path history] [tgz]
  1. test/
  2. .style.yapf
  6. json_data_generator.gni

JSON Data Generator

This GN template will help you share JSON data across multiple languages (e.g javascript, c++, etc.), it uses whatever JSON files (*.json5) you provided as the data sources and generates whatever files you like based on the template files (*.jinja) you provided.

When to use?

If you see a comment in a C++ file saying “the data change here should be reflected in another file xxx.js”, then it's a good chance the shared data can be extracted to a standalone JSON file to keep consistency and make it easier to maintain.

Get Started

Simply import the GN template file in your own GN file and pass your JSON data source files and template files.


json_data_generator("my_json_data") {
  sources = [ "my_json_data.json5" ]
  templates = [
  output_dir = "$root_gen_dir/my_dir"

The above GN build task will generate 2 files (given your are building for out/Default): * out/Default/gen/my_dir/ * out/Default/gen/my_dir/my_json_data.js

Available variables

The GN temple supports the following variables:

Variable NameDescriptionMandatory
sourcesAn array to include all the JSON data sourceYes
: : file paths. : :
templatesAn array to include all the template fileYes
: : paths. : :
template_helperA string represents the template helper fileNo
: : path. : :
output_dirA string to indicate the output directoryNo
: : for the generated files. The default value : :
: : for this is $target_gen_dir, which is the : :
: : same directory your GN file stays. : :
depsSame as the standard deps array.No

Available globals in Jinja templates

The json data will be stored under a key in the template model, the key is the same as the JSON file name.

// my_data1.json5
  "a": 11
// my_data2.json5
  22, 33

For example, with the above 2 JSON files as the data sources, we can use these expressions in the Jinja template files:

  • {{ model.my_data1.a }} will output 11.
  • {{ model.my_data2[1] }} will output 33.

Beside the data itself, there are also some other useful globals available:

  • source_json_files will give you the JSON paths array (same as sources).
  • out_file_path will give you the full path of this generated file.

Check GetGlobals() in to see all available globals.

Available filters in Jinja templates

  • to_header_guard will convert the string path you provide to the upper case with underscore style, which is quite handy if you are generate C++ header files.

Check GetFilters() in to see all available globals.

Provide your own custom globals/filters

Check test/ for more examples.

Jinja template language only supports very limited filters and python expressions, if you need specific logic to handle your JSON data, you can pass an additional python file as template_helper, the only requirement for the file is it must include 2 functions:

def get_custom_globals(model):
    return {
        # custom globals

def get_custom_filters(model):
    return {
        # custom filters

This file will be processed when rendering your template, the loaded JSON data will be passed in as the model parameter, so you can do whatever logic you like here and use your custom globals/filters in your template file.