Skip to content
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

Closed
laurentlb opened this issue Mar 28, 2019 · 3 comments
Closed

Add indentation to struct.to_json() #7879

laurentlb opened this issue Mar 28, 2019 · 3 comments
Labels
P3 We're not considering working on this, but happy to review a PR. (No assignee) type: feature request

Comments

@laurentlb
Copy link
Contributor

A user at Google suggests to add a pretty_print argument to to_json for producing more readable output.

Before:

{"expect_response":0,"fuses":{"DEV_ID0":45,"DEV_ID1":74},"info":{},"keyid":1477893011,"timestamp":1551398606}

After:

{
  "err_response":0,
  "expect_response":0,
  "fuses":{
    "DEV_ID0":45,
    "DEV_ID1":74
  },
  "info":{},
  "keyid":1477893011,
  "timestamp":1551398606
}

I'm filing the bug to get more feedback on this feature request.

cc @alandonovan

@laurentlb laurentlb added team-Starlark type: feature request P3 We're not considering working on this, but happy to review a PR. (No assignee) labels Mar 28, 2019
@alandonovan
Copy link
Contributor

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.

alandonovan pushed a commit to google/starlark-go that referenced this issue Mar 29, 2019
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
alandonovan pushed a commit to google/starlark-go that referenced this issue Mar 29, 2019
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
@alandonovan
Copy link
Contributor

Please take a look at google/starlark-go#179 for a simple implementation of the idea just described.

alandonovan pushed a commit to google/starlark-go that referenced this issue Apr 2, 2019
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
alandonovan pushed a commit to google/starlark-go that referenced this issue Mar 5, 2020
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
alandonovan pushed a commit to google/starlark-go that referenced this issue Mar 5, 2020
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
alandonovan pushed a commit to google/starlark-go that referenced this issue Mar 6, 2020
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
alandonovan pushed a commit to google/starlark-go that referenced this issue Jun 11, 2020
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
alandonovan pushed a commit to google/starlark-go that referenced this issue Jun 11, 2020
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
@brandjon
Copy link
Member

The json module is implemented and has an indent function. We can use a separate issue to track deprecation of struct.to_json().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P3 We're not considering working on this, but happy to review a PR. (No assignee) type: feature request
Projects
None yet
Development

No branches or pull requests

3 participants