-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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 indentation to struct.to_json() #7879
Comments
Let's not add to the existing to_json method. Ideally any JSON functionality in Starlark should be completely decoupled from struct, so that you can apply it to any Starlark value of type T where T = string, int, bool, iterable of T (e.g, tuple, list), mapping of string to T (e.g. dict), or has-fields of type T (e.g. struct, provider). In that world, the struct.to_json behavior would be provided by a standalone 'encode' function imported from a 'json' module. Adding spaces and newlines to the output of struct.to_json can be done (asymptotically efficiently) after the fact; indeed, this is how it's implemented in the Go standard library: https://golang.org/src/encoding/json/encode.go?s=6923:6995#L165. So this argues for a json.indent function in the json module that adds indentation to a JSON string already produced by json.encode(x) or the (legacy) struct.to_json method. |
This is a sketch of a standard module for JSON encoding/decoding for Starlark. It is intended to subsume, generalize, and eventually replace the ill-conceived struct.to_json method. See related issues: bazelbuild/bazel#7896 https://buganizer.corp.google.com/issues/23962735 https://buganizer.corp.google.com/issues/70210417 bazelbuild/bazel#7879 (comment) bazelbuild/bazel#5542 Change-Id: I297ffaee9349eedeeb52f5a88f40636a4095f997
This is a sketch of a standard module for JSON encoding/decoding for Starlark. It is intended to subsume, generalize, and eventually replace the ill-conceived struct.to_json method. See related issues: bazelbuild/bazel#7896 https://buganizer.corp.google.com/issues/23962735 https://buganizer.corp.google.com/issues/70210417 bazelbuild/bazel#7879 (comment) bazelbuild/bazel#5542 Change-Id: I297ffaee9349eedeeb52f5a88f40636a4095f997
Please take a look at google/starlark-go#179 for a simple implementation of the idea just described. |
This is a sketch of a standard module for JSON encoding/decoding for Starlark. It is intended to subsume, generalize, and eventually replace the ill-conceived struct.to_json method. See related issues: bazelbuild/bazel#7896 https://buganizer.corp.google.com/issues/23962735 https://buganizer.corp.google.com/issues/70210417 bazelbuild/bazel#7879 (comment) bazelbuild/bazel#5542 Change-Id: I297ffaee9349eedeeb52f5a88f40636a4095f997
This change defines a standard Starlark module for JSON encoding and decoding. See json.go for documentation. It is intended to subsume, generalize, and eventually replace Bazel's ill-conceived struct.to_json method. The json module is predeclared in the Starlark REPL environment. See related issues: bazelbuild/bazel#7896 https://buganizer.corp.google.com/issues/23962735 https://buganizer.corp.google.com/issues/70210417 bazelbuild/bazel#7879 (comment) bazelbuild/bazel#5542 bazelbuild/bazel#10176 Change-Id: I297ffaee9349eedeeb52f5a88f40636a4095f997
This change defines a standard Starlark module for JSON encoding and decoding. See json.go for documentation. It is intended to subsume, generalize, and eventually replace Bazel's ill-conceived struct.to_json method. The json module is predeclared in the Starlark REPL environment. See related issues: bazelbuild/bazel#7896 https://buganizer.corp.google.com/issues/23962735 https://buganizer.corp.google.com/issues/70210417 bazelbuild/bazel#7879 (comment) bazelbuild/bazel#5542 bazelbuild/bazel#10176 Change-Id: I297ffaee9349eedeeb52f5a88f40636a4095f997
This change defines a standard Starlark module for JSON encoding and decoding. See json.go for documentation. It is intended to subsume, generalize, and eventually replace Bazel's ill-conceived struct.to_json method. The json module is predeclared in the Starlark REPL environment. See related issues: bazelbuild/bazel#7896 https://buganizer.corp.google.com/issues/23962735 https://buganizer.corp.google.com/issues/70210417 bazelbuild/bazel#7879 (comment) bazelbuild/bazel#5542 bazelbuild/bazel#10176 Change-Id: I297ffaee9349eedeeb52f5a88f40636a4095f997
This change defines a standard Starlark module for JSON encoding and decoding. See json.go for documentation. It is intended to subsume, generalize, and eventually replace Bazel's ill-conceived struct.to_json method. The json module is predeclared in the Starlark REPL environment. See related issues: bazelbuild/bazel#7896 https://buganizer.corp.google.com/issues/23962735 https://buganizer.corp.google.com/issues/70210417 bazelbuild/bazel#7879 (comment) bazelbuild/bazel#5542 bazelbuild/bazel#10176 bazelbuild/starlark#83 bazelbuild/bazel#3732 Change-Id: I297ffaee9349eedeeb52f5a88f40636a4095f997
This change defines a standard Starlark module for JSON encoding and decoding. See json.go for documentation. It is intended to subsume, generalize, and eventually replace Bazel's ill-conceived struct.to_json method. The json module is predeclared in the Starlark REPL environment. See related issues: bazelbuild/bazel#7896 https://buganizer.corp.google.com/issues/23962735 https://buganizer.corp.google.com/issues/70210417 bazelbuild/bazel#7879 (comment) bazelbuild/bazel#5542 bazelbuild/bazel#10176 bazelbuild/starlark#83 bazelbuild/bazel#3732
The |
A user at Google suggests to add a
pretty_print
argument toto_json
for producing more readable output.Before:
After:
I'm filing the bug to get more feedback on this feature request.
cc @alandonovan
The text was updated successfully, but these errors were encountered: