Skip to content

feat: allow custom json encoder/decoder. - #338

Closed
jmcarp wants to merge 1 commit into
mainfrom
jmcarp/custom-json-codec
Closed

feat: allow custom json encoder/decoder.#338
jmcarp wants to merge 1 commit into
mainfrom
jmcarp/custom-json-codec

Conversation

@jmcarp

@jmcarp jmcarp commented Nov 14, 2025

Copy link
Copy Markdown
Contributor

The encoding/json package in the standard library isn't particularly performant, and there have been many first-party and third-party efforts to improve it. This patch allows users of the sdk to configure a custom json codec to marshall and unmarshall json with their tools of choice. Note: we can update to encoding/json/v2 as the default once it's marked stable in a future golang release.

For context, I started thinking about this because we wind up unmarshalling fairly large json payloads in the Oxide otel receiver. The resource/performance improvement from switching to a faster json library turned out to be small, but I thought I would check this in anyway—I'll take performance improvements even if small, and this may prove helpful for users making large volumes of requests through the sdk.

The encoding/json package in the standard library isn't particularly
performant, and there have been many first-party and third-party efforts to
improve it. This patch allows users of the sdk to configure a custom json codec
to marshall and unmarshall json with their tools of choice. Note: we can update
to encoding/json/v2 as the default once it's marked stable in a future golang
release.
@jmcarp
jmcarp requested a review from a team as a code owner November 14, 2025 16:48

@lgfa29 lgfa29 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, provided the extra interface doesn't cause a performance hit in the default case. I don't think it would, but it may be worth adding a benchmark just to double-check.

I may also be worth documenting this option, and maybe provide an example or two of how to use an alternative encoder.

@sudomateo sudomateo left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I don't oppose this change, but I will say that Go currently is experimenting with a json/v2 package: https://go.dev/blog/jsonv2-exp

They claim the following.

The Marshal performance of v2 is roughly at parity with v1. Sometimes it is slightly faster, but other times it is slightly slower. The Unmarshal performance of v2 is significantly faster than v1, with benchmarks demonstrating improvements of up to 10x.

I'm okay with deferring until Go 1.26 is released to see if they promote json/v2 to stable or merging this now if there's a need to swap the json encoder/decoder.

@jmcarp

jmcarp commented Dec 3, 2025

Copy link
Copy Markdown
Contributor Author

Yep, I noted the upcoming json improvements in the standard library in the commit message, but I take your point. I don't think the performance improvements from this change justify the extra work when we can just upgrade to json/v2 hopefully next year. Closing.

@jmcarp jmcarp closed this Dec 3, 2025
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

Successfully merging this pull request may close these issues.

3 participants