Live data from Hacker News

A new experimental Go API for JSON

go.dev

51–60 of 110 posts

Re: A new experimental Go API for JSON

#51

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.

Two v2s in 15 years seems pretty good given the breadth of the stdlib.

Re: A new experimental Go API for JSON

#52

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?

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.

Re: A new experimental Go API for JSON

#53
post #41

Benchmark Analysis: Sonic vs Standard JSON vs JSON v2 in Go https://github.com/centralci/go-benchmarks/tree/b647c45272c7...

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

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.

Re: A new experimental Go API for JSON

#54
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.

Re: A new experimental Go API for JSON

#55
post #28

> Since encoding/json marshals a nil slice or map as a JSON null How did that make it into the v1 design?

how is a nil map not null? It certainly isn’t a zero-valued map, that would be {}.

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)

Re: A new experimental Go API for JSON

#56

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.

> 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).

Re: A new experimental Go API for JSON

#57
post #11
post #10

Earlier quoted context omitted.

What does "highly optimized" have to do with whether it's in the standard library? Highly-optimized cryptography is in the standard library.

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.

It's amusing to see assembly considered more portable than C.

Re: A new experimental Go API for JSON

#58

Earlier quoted context omitted.

how is a nil map not null? It certainly isn’t a zero-valued map, that would be {}.

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)

Ok, true!

Re: A new experimental Go API for JSON

#59
post #6

Earlier quoted context omitted.

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…

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.

You’re assuming request-response round trip between each call to encode/decode. Streaming large objects/NDJSON would still have serialization bottleneck. (See elasticsearch/opensearch for a real life use case)

But in that case your last point still stands: pick a better format

Re: A new experimental Go API for JSON

#60
post #27

Earlier quoted context omitted.

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 th…

gjson/sjson is probably for you if you need to work with 100MB JSONs.

This is cool. I wouldn't have thought to use Go for stuff that size.
Post reply on HN