Top
Best
New

Posted by darccio 5 days ago

A new experimental Go API for JSON(go.dev)
269 points | 101 commentspage 2
curtisszmania 5 days ago|
[dead]
afdbcreid 5 days 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.

ncruces 5 days 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.

oncallthrow 5 days 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...

jitl 5 days ago|||
I’d rather have 2 jsons in the stdlib after 15 years than 0 jsons in the stdlib
skywhopper 5 days ago|||
Two v2s in 15 years seems pretty good given the breadth of the stdlib.
int_19h 5 days ago|||
It's not like this is new. Look at Java and .NET collection APIs, for example - both languages have the OG 1.0 versions, and then the more modern ones. In a similar vein, .NET has four different ways to deal with XML, of which three (XmlDocument, XPathDocument, and XDocument) are basically redundant representations of XML trees, each one doing things differently based on lessons learned.

It's still better than the mess that is Node.js.

kiitos 5 days 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
gethly 5 days 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.
adamrt 5 days 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."

ycombinatrix 5 days ago|||
What is your preferred behavior for a nil map/slice? Feels weird that it doesn't map to null.
gethly 5 days 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}

Mogzol 5 days ago|||
Isn't that what the new 'omitzero' option is for?

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

kanbara 4 days ago||
you know you can make your case better if you don’t use disrespectful and offensive language
h1fra 5 days 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
9rx 5 days ago||
> I still don't get how a common thing like JSON is not solved in go.

Given that it is not even yet solved in its namesake language, Javascript, that's not saying much.

Thaxll 5 days ago|||
You should read that, it's still relevant: https://seriot.ch/projects/parsing_json.html