Please add a Skylark builtin for parsing JSON to a dict (useful for repo rules)
See original GitHub issueWhen writing repository rules that inspect the state of the local system, I commonly want to run some local command and inspect its machine-readable output. The rule would then make configuration decisions based on available features, versions, installed plugins, and so on.
Nearly all tools that offer machine-readable version data use JSON, or at least offer it as an option. Rather than try to hack my way through with .split
and .index
, it would be much nicer if I could take a JSON string and have Bazel parse it directly into a dict
within Skylark. A builtin json_to_dict(str) -> dict
function would do nicely.
Using cmake as an example, I want to get its version using repository_ctx.execute()
:
$ cmake -E capabilities
{"version":{"isDirty":false,"major":3,"minor":9,"patch":1,"string":"3.9.1","suffix":""}}
… and then use that (3,9,1)
tuple to decide whether it’s recent enough, or should be ignored in favor of an external repository.
There was a previous issue open about this (https://github.com/bazelbuild/bazel/issues/1813), but it had unclear motivations and was closed without action taken.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:13
- Comments:13 (10 by maintainers)
FWIW, rules_jvm_external has been using @erickj’s bazel-json parser in Starlark and we haven’t ran into any issues with it since the beginning of the project.
There’s now a couple implementations of a JSON API in non-Bazel tools built on Starlark. To see if we can settle on a reasonable shared JSON API, I sent https://github.com/bazelbuild/starlark/pull/83 as a proposed standard JSON module in the Starlark spec.