Skip to content

Update json example to encoding/json/v2 - #676

Merged
eliben merged 1 commit into
mmcgrana:masterfrom
prattmic:push-ywrxoxwwuwxm
Sep 2, 2026
Merged

eliben merged 1 commit into
mmcgrana:masterfrom
prattmic:push-ywrxoxwwuwxm

Conversation

@prattmic

Copy link
Copy Markdown
Contributor

The core marshal/unmarshal APIs are the same, so most examples don't change at all.

MarshalWrite and UnmarshalRead provide an easier way to use io.Reader/Writers.

Go 1.27 is not released yet. This shouldn't be merged until some time after release.

Comment thread examples/json/json.go Outdated
d := map[string]int{"apple": 5, "lettuce": 7}
_ = enc.Encode(d)
_ = json.MarshalWrite(os.Stdout, d)
fmt.Println()

@prattmic prattmic Aug 12, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awkward, I'm not sure the best way to handle this.

encoding/json.Encoder writes a newline after the JSON output. encoding/json/v2.MarshalWrite does not, so without an extra newline here this example and the next are output on the same line.

encoding/json/jsontext.Encoder (to be used with json.MarshalEncoder) does write a newline, but I don't think we want that here, since MarshalWrite is what most users will want most of the time.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose one alternative would be to MarshalWrite to a bytes.Buffer and then print the buffer afterwards.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose one alternative would be to MarshalWrite to a bytes.Buffer and then print the buffer afterwards.

Done. I think this is pretty natural given that the next example also uses strings.Reader.

The core marshal/unmarshal APIs are the same, so most examples don't
change at all.

MarshalWrite and UnmarshalRead provide an easier way to use
io.Reader/Writers.
@eliben

eliben commented Aug 14, 2026

Copy link
Copy Markdown
Collaborator

LGTM. I plan to merge this after 1.27.1 is released to give folks some time to absorb 1.27

@eliben
eliben merged commit d6492ff into mmcgrana:master Sep 2, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants