-
Notifications
You must be signed in to change notification settings - Fork 54
Add JSON schema #1426
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add JSON schema #1426
Changes from all commits
245e19d
a860269
4c957b4
a510957
facfa15
3749056
ba3f5b1
a719bb6
3a5fc19
334f2d5
90ce201
779cf12
f6b1f24
f18a88b
5e4a870
252c0d4
65aa12d
83ed23a
579e7b0
81533b0
985d505
3a2929f
d578167
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -685,11 +685,12 @@ set(openPMD_TEST_NAMES | |
| # command line tools | ||
| set(openPMD_CLI_TOOL_NAMES | ||
| ls | ||
| convert-toml-json | ||
| ) | ||
| set(openPMD_PYTHON_CLI_TOOL_NAMES | ||
| pipe | ||
| ) | ||
| set(openPMD_PYTHON_CLI_MODULE_NAMES ${openPMD_CLI_TOOL_NAMES}) | ||
| set(openPMD_PYTHON_CLI_MODULE_NAMES ls) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line change looks like a hack?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not really. Until now, both |
||
| # examples | ||
| set(openPMD_EXAMPLE_NAMES | ||
| 1_structure | ||
|
|
@@ -894,6 +895,9 @@ if(openPMD_BUILD_CLI_TOOLS) | |
| endif() | ||
|
|
||
| target_link_libraries(openpmd-${toolname} PRIVATE openPMD) | ||
| target_include_directories(openpmd-${toolname} SYSTEM PRIVATE | ||
| $<TARGET_PROPERTY:openPMD::thirdparty::nlohmann_json,INTERFACE_INCLUDE_DIRECTORIES> | ||
| $<TARGET_PROPERTY:openPMD::thirdparty::toml11,INTERFACE_INCLUDE_DIRECTORIES>) | ||
| endforeach() | ||
| endif() | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| convert := openpmd-convert-toml-json | ||
|
|
||
| json_files = attribute_defs.json attributes.json dataset_defs.json iteration.json mesh.json mesh_record_component.json particle_patches.json particle_species.json patch_record.json record.json record_component.json series.json | ||
|
|
||
| .PHONY: all | ||
| all: $(json_files) | ||
|
|
||
| # The target file should only be created if the conversion succeeded | ||
| $(json_files): %.json: %.toml | ||
| $(convert) @$^ > [email protected] | ||
| mv [email protected] $@ | ||
|
|
||
| .PHONY: clean | ||
| clean: | ||
| for file in $(json_files); do rm -f "$$file" "$$file.tmp"; done |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| # JSON Validation | ||
|
|
||
| This folder contains a JSON schema for validation of openPMD files written as `.json` files. | ||
|
|
||
| ## Usage | ||
|
|
||
| ### Generating the JSON schema | ||
|
|
||
| For improved readability, maintainability and documentation purposes, the JSON schema is written in `.toml` format and needs to be "compiled" to `.json` files first before usage. | ||
| To do this, the openPMD-api installs a tool named `openpmd-convert-toml-json` which can be used to convert between JSON and TOML files in both directions, e.g.: | ||
|
|
||
| ```bash | ||
| openpmd-convert-toml-json @series.toml > series.json | ||
| ``` | ||
|
|
||
| A `Makefile` is provided in this folder to automate generating the needed JSON files from the TOML files. | ||
|
|
||
| ### Verifying a file against the JSON schema | ||
|
|
||
| In theory, the JSON schema should be applicable by any JSON validator. This JSON schema is written in terms of multiple files however, and most validators require special care to properly set up the links between the single files. A Python script `check.py` is provided in this folder which sets up the [Python jsonschema](https://python-jsonschema.readthedocs.io) library and verifies a file against it, e.g.: | ||
|
|
||
| ```bash | ||
| ./check.py path/to/my/dataset.json | ||
| ``` | ||
|
|
||
| For further usage notes check the documentation of the script itself `./check.py --help`. | ||
|
|
||
| ## Caveats | ||
|
|
||
| The openPMD standard is not entirely expressible in terms of a JSON schema: | ||
|
|
||
| * Many semantic dependencies, e.g., that the `position/x` and `position/y` vectors of a particle species need to be of the same size, or that the `axisLabels` have the same dimensionality as the dataset itself, will go unchecked. | ||
| * The `meshesPath` is assumed to be `meshes/` and the `particlesPath` is assumed to be `particles/`. This dependency cannot be expressed. | ||
|
|
||
| While a large part of the openPMD standard can indeed be verified by checking against a static JSON schema, the standard is generally large enough to make this approach come to its limits. Verification of a JSON schema is similar to the use of a naive recursive-descent parser. Error messages may become unexpectedly verbose and not very informative, especially when parsing disjunctive statements such as "A Record is either a scalar Record Component or a vector of non-scalar Record Components". We have taken care to decide disjunctive statements early on, e.g. with json-schema's support for `if` statements, but error messages may in general become unwieldy even due to tiny mistakes far down in the parse tree. | ||
|
|
||
| The layout of attributes is assumed to be that which is created by the JSON backend of the openPMD-api. Both the longhand and shorthand forms are recognized: | ||
|
|
||
| ```json | ||
| "meshesPath": { | ||
| "datatype": "STRING", | ||
| "value": "meshes/" | ||
| }, | ||
| "particlesPath": "particles/" | ||
| ``` | ||
|
|
||
| For a custom-written verification of openPMD datasets, also consider using the [openPMD-validator](https://github.com/openPMD/openPMD-validator). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we able to patch this in
check.py?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not very easily. The JSON schema is on the file system and the single .json files refer to each other by their file names. Changing this would require (1) traversing the entire JSON schema and overriding the meshes path, the particles path and the references and (2) somehow setting up python-jsonschema to cross-reference in-memory schemas which I don't even know if it supports that, both at runtime of
check.py.