frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

A new experimental Go API for JSON

https://go.dev/blog/jsonv2-exp
165•darccio•8h ago

Comments

analytically•6h ago
Benchmark Analysis: Sonic vs Standard JSON vs JSON v2 in Go

https://github.com/centralci/go-benchmarks/tree/b647c45272c7...

tgv•6h ago
IIRC, sonic does JIT, has inline assembly (github says 41%), and it's huge. There's no way you can audit it. If you don't need to squeeze every cpu cycle out of your json parser (and most of us don't; go wouldn't be the first choice for such performance anyway), I'd stick with a simpler implementation.
gethly•5h ago
Those numbers look similar to goccy. I used to use it in the past, even Kubernetes uses it as direct dependency, but the amount of issues have been stockpiling for quite some time so I no longer trust it.

So it seems both are operating at the edge of Go's capabilities.

Personally, I think JSON should be in Go's core and highly optimised simd c code and not in the Go's std library as standard Go code. As JSON is such an important part of the web nowadays, it deserves to be treated with more care.

tptacek•5h ago
What does "highly optimized" have to do with whether it's in the standard library? Highly-optimized cryptography is in the standard library.
ronsor•5h ago
Not to mention that Go is never going to put C code in the standard library for anything portable. It's all Go or assembly now.
dilyevsky•4h ago
Previously Go team has been vocal about sacrificing performance to keep stdlib idiomatic and readable. Guess the crypto packages are the exception because they are used heavily by Google internally and json and some others (like say image/jpeg which had crap performance last time i checked) are not.

Edit: See: https://go.dev/wiki/AssemblyPolicy

nasretdinov•4h ago
Go doesn't yet have native SIMD support, but it actually might in the future: https://github.com/golang/go/issues/73787

I think when it's introduced it might be worth discussing that again. Otherwise providing assembly for JSON of all packages seems like a huge maintenance burden for very little benefit for end users (since faster alternatives are readily available)

CamouflagedKiwi•4h ago
Agreed. goccy has better performance most times but absolutely appalling worst-case performance which renders it unacceptable for many use cases - in my case even with trusted input it took effectively eternity to decode it. It's literally a quadratic worst case, what's the point of having a bunch of super clever optimisations if the big-O performance is that bad.

Sonic may be different but I'm feeling once bitten twice shy on "faster" JSON parsers at this point. A highly optimised SIMD version might be nice but the stdlib json package needs to work for everything out there, not just the cases the author decided to test on, and I'd be a lot more nervous about something like that being sufficiently well tested given the extra complexity.

godisdad•3h ago
> As JSON is such an important part of the web nowadays, it deserves to be treated with more care.

There is a case to be made here but Corba, SOAP and XML-RPC likely looked similarly sticky and eternal in the past

ForHackernews•1h ago
The fact that JSON is used so commonly for web stuff seems like an argument against wasting your time optimizing it. Network round trip is almost always going to dominate.

If you're pushing data around on disk where the serialization library is your bottleneck, pick a better format.

kiitos•2h ago
first of all, that doesn't exercise JSON v2 at all, afaict

second of all, sonic apparently uses unsafe to (unsafe-ly) cast byte slices to strings, which of course is gonna be faster than doing things correctly, but is also of course incomparable to doing things correctly

like almost all benchmark data posted to hn -- unsound, ignore

kristianp•33m ago
Just using the GOEXPERIMENT=jsonv2 compiler flag changes the underlying implementation if you don't change any code. You're still using the less correct and efficient API though.
Thaxll•1h ago
And Sonic with its "cutting edge" optimization is still slower than std Json on arm64 with basic use cases. It shows that JIT, simd, low level code comes at cost of maintenance for all platform.

https://github.com/bytedance/sonic/issues/785

gethly•5h ago
This V2 is still pushing forward the retarded behavior from v1 when it comes to handling nil for maps, slices and pointers. I am so sick and tired of this crap. I had to fork the v1 to make it behave properly and they still manage to fuck up completely new version just as well(by pushing omitempty and ignoring omitnil behavior as a standalone case) which means I will be stuck with the snale-pace slow v1 for ever.
ycombinatrix•5h ago
What is your preferred behavior for a nil map/slice? Feels weird that it doesn't map to null.
gethly•5h ago
When you are unmarshaling json, empty map/slice is something completely different than a null or no value present, as you are losing intent of the sender, in case of JSON REST.

For example, if my intent is to keep the present value, I will send {"foo": 1} or {"foo": 1, "bar": null} as null and no value has the same meaning. On the other hand, I might want to change the existing value to empty one and send {"foo": 1, "bar": []}.

The server must understand case when I am not mutating the field and when I am mutating the field and setting it to be empty.

On the other side, I never want to be sending json out with null values as that is waste of traffic and provides no information to the client, ie {"foo": 1, "bar": null} is the same as {"foo": 1}.

Protocol buffers have the exact same problem but they tackle it in even dumber way by requiring you to list fields from the request, in the request's special field, which you are mutating as they are unable to distinguish null and no value present and will default to empty value otherwise, like {} or [], which is not the intent of the sender and causes all sort of data corruption.

PS: obviosly this applies to pointers as a whole, so if i have type Request struct { Number *int `json:"number"} then sending {} and {"number": null} must behave the same and result in Result{Number nil}

adamrt•5h ago
Are you sure about that? Unless I'm misunderstanding they did fix this: https://pkg.go.dev/encoding/json

"In v1, a nil Go slice or Go map is marshaled as a JSON null. In contrast, v2 marshals a nil Go slice or Go map as an empty JSON array or JSON object, respectively. The jsonv2.FormatNilSliceAsNull and jsonv2.FormatNilMapAsNull options control this behavior difference. To explicitly specify a Go struct field to use a particular representation for nil, either the `format:emitempty` or `format:emitnull` field option can be specified. Field-specified options take precedence over caller-specified options."

Mogzol•5h ago
Isn't that what the new 'omitzero' option is for?

https://pkg.go.dev/github.com/go-json-experiment/json#exampl...

rjrodger•5h ago
null != nil !!!

It is good to see some partial solutions to this issue. It plagues most languages and introduces a nice little ambiguity that is just trouble waiting to happen.

Ironically, JavaScript with its hilarious `null` and `undefined` does not have this problem.

Most JSON parsers and emitters in most languages should use a special value for "JSON null".

pjmlp•5h ago
Fixed in 1976 by ML, followed up by Eiffel in 2005, but unfortunately yet to be made common.
afiori•4h ago
Null and undefined are fine imho with a sort of empty/missing semantics (especially since you mostly just care to == them) I have bigger issues to how similar yet different it is to have an undefined key and a not-defined key, I would almost prefer if

    obj['key']=undefined

 was the same as 

    delete obj['key']
coldtea•5h ago
>Over time, packages evolve with the needs of their users, and encoding/json is no exception

No, it's an exception. It was badly designed from the start - it's not just that people's json needs (which hardly changed) outgrew it.

bayindirh•4h ago
A bad design doesn't invalidate the sentence you have quoted.

Over time, it became evident that the JSON package didn't meet the needs of its users, and the package has evolved as a result. The size of the evolution doesn't matter.

pcthrowaway•4h ago
It's true that packages (generally) evolve with the needs of their users.

It's also true that a json IO built-in lib typically wouldn't be so poorly designed in the first release of a language, that it would immediately be in need of maintenance.

bayindirh•4h ago
> immediately

JSON library released with Go 1, in 2012. This makes the library 13 years old [0].

If that's immediate, I'm fine with that kind of immediate.

[0]: https://pkg.go.dev/encoding/json@go1

pcthrowaway•4h ago
In need of maintenance and having received maintenance are two different things
coldtea•4h ago
"immediately be in need of maintenance" means it needed this update 13 years ago.
kiitos•2h ago
encoding/json works perfectly great even today, please troll somewhere else
pcthrowaway•1h ago
It works well enough, despite being clunky, but it has a few issues, including significant performance-related ones.
coldtea•1h ago
Please don't use the "troll" accusation to something because you disagree with it. It's weasely.

Obviously encoding/json doesn't "work perfectly", the TFA lists several problems it has, and the need for a new version, and that's directly by the horse's mouth. Is the Go team "trolling" as well?

Second, we're not talking whether it "does the job", which is what you might mean by "works perfectly great".

We're talking about whether it's a good design for the problem domain, or whether it has footguns, bad API choices, performance problems, and other such issues.

coldtea•4h ago
"Becoming evident it doesn't meet the needs of its users" is not the same as "packages evolve with the needs of their users".

The latter is a weasely way to put the blame on changing needs - as if initially it was fine, but user needs grew and it's not covering them anymore. Truth is, user needs are the same, we havent had any magical change in JSON use patterns over the last 10 years. The design was just flawed to begin with.

I'd argue it didn't "become evident over time" either. It was evident on day one, and many people pointed it out 10 and 13 years ago.

geodel•3h ago
Well mostly I have seem people learn shortcomings of software by using or creating it and come up with new version when possible. In your case it seems v1 are perfect each time.
sroerick•4h ago
Could somebody give a high level overview of this for me, as not a godev? It looks like Go JSON lib has support to encode native go structures in JSON, which is cool, but maybe it was bad, which is not as cool. Do I have that right?
eknkc•3h ago
Go already has a JSON parser and serializer. It kind of resembles the JS api where you push some objects into JSON.stringify and it serializes them. Or you push some string and get an object (or string etc) from JSON.parse.

The types themselves have a way to customize their own JSON conversion code. You could have a struct serialize itself to a string, an array, do weird gymnastics, whatever. The JSON module calls these custom implementations when available.

The current way of doing it is shit though. If you want to customize serialization, you need to return a json string basically. Then the serializer has to check if you actually managed to return something sane. You also have no idea if there were some JSON options. Maybe there is an indentation setting or whatever. No, you return a byte array.

Deserialization is also shit because a) again, no options. b) the parser has to send you a byte array to parse. Hey, I have this JSON string, parse it. If that JSON string is 100MB long, too bad, it has to be read completely and allocated again for you to work on because you can only accept a byte array to parse.

New API fixes these. They provide a Decoder or Encoder to you. These carry any options from top. And they also can stream data. So you can serialize your 10GB array value by value while the underlying writer writes it into disk for example. Instead of allocating all on memory first, as the older API forces you to.

There are other improvements too but the post mainly focuses on these so thats what I got from it (I havent tried the new api btw, this is all from the post so maybe I’m wrong on some points)

stackedinserter•2h ago
gjson/sjson is probably for you if you need to work with 100MB JSONs.
skywhopper•1h ago
Nah, the existing implementation is pretty decent, actually, but doesn’t address every use case and has some flaws that are hard or impossible to fix. But for lots of use cases it works great.

Now here’s a new implantation that addresses some of the architectural problems that made the old library structurally problematic for some use cases (streaming large JSON docs being the main one).

resonantjacket5•44m ago
The main issues are under the Behavior differences https://go.dev/blog/jsonv2-exp#behavior-differences

The largest problem were around behavior around nil in golang and what to convert into json and vice versa.

* The v2 will now throw an error for invalid characters outside of ut8 (before silently accepted it) which meant one had to preprocess or process again the json before sending it off to the server * the golang nil will be converted to json empty array or map (for each type). previously it was converted to json null. * json field names will be converted to golang names with case sensitivity. before it was case-insentitive and would be lowercased. this kinda caused lots of problems if the field collided. (say there's bankName and bankname in json) * omitempty was problematic as it was used for say golang amount: nil would mean omit the field in json as {} instead of { amount: null}. however it also meant that the golang amount: 0 would also be omitted as { amount: 0 } which surprising. the new omitempty will only do so for nil and empty arrays/hashmaps but no longer for 0 or false. there's a new omitzero tag for that.

tibbe•3h ago
> Since encoding/json marshals a nil slice or map as a JSON null

How did that make it into the v1 design?

rowanseymour•3h ago
I had a back and forth with someone who really didn't want to change that behavior and their reasoning was that since you can create and provide an empty map or slice.. having the marshaler do that for you, and then also needing a way to disable that behavior, was unnecessary complexity.
binary132•3h ago
how is a nil map not null? It certainly isn’t a zero-valued map, that would be {}.
atombender•19m ago
The zero value of a map is indeed nil in Go: This prints true (https://go.dev/play/p/8dXgo8y2KTh):

    var m map[string]int
    println(m == nil)
rplnt•2h ago
Well those are different things, aren't they? Empty slice/map is different from nil. So it makes a lot of sense that nil = null and []string{} = [], and you have an option to use both. That being said, it starts to make less sense if you work with go where the API mostly treats it as equivalent (append, len, []). So that would be my guess how it ended up the way it did.

Also, now that nil map is an empty object, shouldn't that extend to every nil struct that doesn't have a custom marshaller? It would be an object if it wasn't nil after all...

stackedinserter•2h ago
Why shouldn't it be? The nil is null and empty array is an empty array, they are completely different objects.
h1fra•3h ago
I still don't get how a common thing like JSON is not solved in go. How convoluted it is to just get a payload from an api call compared to all languages is baffling
Thaxll•1h ago
You should read that, it's still relevant: https://seriot.ch/projects/parsing_json.html
breakingcups•3h ago
If/once this goes through, I wonder what the adoption is going to be like now that all LLMs still only have the v1 api in their corpus.
afdbcreid•2h ago
This is the second time a v2 is released to a package in the Go's standard library. Other ecosystems are not free of this problem.

And then people complain that Rust doesn't have a batteries-included stdlib. It is done to avoid cases like this.

oncallthrow•2h ago
Wow, two whole times in 19 years? That sounds terrible.

Yes, we should definitely go with the Rust approach instead.

Anyway, I'd better get back to figuring out which crate am I meant to be using...

ncruces•2h ago
That has its own downsides, though.

Both v1 packages continue work; both are maintained. They get security updates, and were both improved by implementing them on top of v2 to the extent possible without breaking their respective APIs.

More importantly: the Go authors remain responsible for both the v1 and v2 packages.

What most people want to avoid with a "batteries included standard library" (and few additional dependencies) is the debacle we had just today with NPM.

Well maintained packages, from a handful of reputable sources, with predictable release schedules, a responsive security team and well specified security process.

You can't get that with 100s of independently developed dependencies.

kiitos•2h ago
I'm not sure how this is a problem, and I'm very sure that even in the presence of this "problem" it is far better for a language to have a batteries-included stdlib than to not
skywhopper•1h ago
Two v2s in 15 years seems pretty good given the breadth of the stdlib.
phoenixhaber•24m ago
I will say this and I feel it's true. Dealing with JSON in Go is a pain. You should be able to write json and not have to care about the marshalling and the unmarshalling. It's the way that serde rust behaves and more or less every other language I've had to deal with and it makes managing this behavior when there's multiple writers complicated.
dmoy•15m ago
> serde rust

That does look a lot cleaner. I was just grumbling about this in golang yesterday (yaml, not json, but effectively the same problem).

Immunotherapy drug eliminates aggressive cancers in clinical trial

https://www.rockefeller.edu/news/38120-immunotherapy-drug-eliminates-aggressive-cancers-in-clinic...
197•marc__1•2h ago•33 comments

All vibe coding tools are selling a get rich quick scheme

https://varunraghu.com/all-vibe-coding-tools-are-selling-a-get-rich-quick-scheme/
71•Varun08•1h ago•43 comments

iPhone Air

https://www.apple.com/newsroom/2025/09/introducing-iphone-air-a-powerful-new-iphone-with-a-breakt...
392•excerionsforte•5h ago•947 comments

Anthropic judge rejects $1.5B AI copyright settlement

https://news.bloomberglaw.com/ip-law/anthropic-judge-blasts-copyright-pact-as-nowhere-close-to-done
131•nobody9999•14h ago•155 comments

E-paper display reaches the realm of LCD screens

https://spectrum.ieee.org/e-paper-display-modos
151•rbanffy•5h ago•67 comments

Memory Integrity Enforcement

https://security.apple.com/blog/memory-integrity-enforcement/
271•circuit•5h ago•115 comments

Claude can now create and edit files

https://www.anthropic.com/news/create-files
375•meetpateltech•9h ago•225 comments

Axial Twist Theory

https://en.wikipedia.org/wiki/Axial_twist_theory
32•lordnacho•2d ago•0 comments

I don't want AI agents controlling my laptop

https://sophiebits.com/2025/09/09/ai-agents-security
41•Bogdanp•2h ago•18 comments

The Dying Dream of a Decentralized Web

https://spectrum.ieee.org/web3-hardware-security
119•warrenm•4h ago•128 comments

We all dodged a bullet

https://xeiaso.net/notes/2025/we-dodged-a-bullet/
512•WhyNotHugo•8h ago•308 comments

Anthropic is endorsing SB 53

https://www.anthropic.com/news/anthropic-is-endorsing-sb-53
25•antfarm•2h ago•23 comments

Tomorrow's emoji today: Unicode 17.0

https://jenniferdaniel.substack.com/p/tomorrows-emoji-today-unicode-170
74•ChrisArchitect•5h ago•112 comments

Ask HN: Why is there no native SSH hook to run a local command before connecting

6•tetris11•2d ago•3 comments

A new experimental Go API for JSON

https://go.dev/blog/jsonv2-exp
165•darccio•8h ago•53 comments

US HS students lose ground in math and reading, continuing yearslong decline

https://apnews.com/article/naep-reading-math-scores-12th-grade-c18d6e3fbc125f12948cc70cb85a520a
162•bikenaga•8h ago•217 comments

Show HN: Bottlefire – Build single-executable microVMs from Docker images

https://bottlefire.dev/
27•losfair•2d ago•7 comments

Microsoft is officially sending employees back to the office

https://www.businessinsider.com/microsoft-send-employees-back-to-office-rto-remote-work-2025-9
185•alloyed•7h ago•326 comments

Weave (YC W25) is hiring a founding AI engineer

https://www.ycombinator.com/companies/weave-3/jobs/SqFnIFE-founding-ai-engineer
1•adchurch•6h ago

DuckDB NPM packages 1.3.3 and 1.29.2 compromised with malware

https://github.com/duckdb/duckdb-node/security/advisories/GHSA-w62p-hx95-gf2c
280•tosh•13h ago•215 comments

Building a DOOM-like multiplayer shooter in pure SQL

https://cedardb.com/blog/doomql/
125•lvogel•8h ago•28 comments

YouTube is a mysterious monopoly

https://anderegg.ca/2025/09/08/youtube-is-a-mysterious-monopoly
95•geerlingguy•18h ago•158 comments

An attacker’s blunder gave us a look into their operations

https://www.huntress.com/blog/rare-look-inside-attacker-operation
112•mellosouls•7h ago•75 comments

Inflation Erased U.S. Income Gains Last Year

https://www.wsj.com/economy/consumers/census-income-insurance-poverty-2024-31d82ad0
123•JumpCrisscross•3h ago•32 comments

Dropbox Paper mobile App Discontinuation

https://help.dropbox.com/installs/paper-mobile-discontinuation
113•mercenario•5h ago•94 comments

ICE is using fake cell towers to spy on people's phones

https://www.forbes.com/sites/the-wiretap/2025/09/09/how-ice-is-using-fake-cell-towers-to-spy-on-p...
430•coloneltcb•7h ago•183 comments

A clickable visual guide to the Rust type system

https://rustcurious.com/elements/
233•stmw•4d ago•39 comments

Go for Bash Programmers – Part II: CLI Tools

https://github.com/go-monk/from-bash-to-go-part-ii
75•reisinge•1d ago•3 comments

You too can run malware from NPM (I mean without consequences)

https://github.com/naugtur/running-qix-malware
172•naugtur•13h ago•97 comments

Cities obey the laws of living things

https://nautil.us/cities-obey-the-laws-of-living-things-1236057/
28•dnetesn•2d ago•8 comments