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

Support json.MarshalIndent & json.Encoder.SetIndent for json.Marshaller implementations #1

Open
luxas opened this issue Aug 30, 2021 · 0 comments

Comments

@luxas
Copy link
Owner

luxas commented Aug 30, 2021

json.MarshalIndent and json.Encoder.SetIndent doesn't indent output from json.Marshaller implementers, which means that e.g. k8s.io/apimachinery/pkg/apis/meta/v1/unstructured objects are never marshalled with the right indentation. This could be fixed upstream in encoding/json or github.com/json-iterator/go by walking (using an JSON iterator) the []byte return value of MarshalJSON(), and indenting accordingly whenever the right tokens are found.

As far as I understand, the implementations mentioned already validate the output of MarshalJSON(), i.e. verify that the syntax is correct, and as such already do "most" of the work needed.

The workaround for this would be to first do a normal Marshal of the object, and then do a json.Indent into a temporary buffer, only to later return the buf.Bytes() in case of MarshalIndent or write the buffer contents to the underlying io.Writer. It would of course be nice if json.Indent supported writing directly to any io.Writer, so the buffer "middle-man" could be avoided.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant