Live data from Hacker News

Show HN: Jason – A new JSON Library for Go

github.com

51–55 of 55 posts

Re: Show HN: Jason – A new JSON Library for Go

#51

I would say the most idiomatic way is having defined structs and using encoding/json. Even when you don't know the json schema in advance you can still parse it into an interface{}. Here's an example: http://play.golang.org/p/jrqBcPuQei

I use encoding/json for struct mapping when doing quick prototyping and simple applications.

Parsing gets much more interesting when you start supporting many clients, broad version ranges of protocols. At that point, a little flexibility starts to be a godsend. This is just my personal experience, but so far, in every language where I've tried direct struct mappers, they become less and less helpful as your product gains a real user base and you have to start doing compatibility "in the wild". You can certainly do it with encoding/json and custom marshallers, but this library seems like it might be helpful.

Protobufs have a lot of features that address this explicitly. I'm not a huge fan of protobufs (for a variety of off-topic reasons), but there's a great deal they got right: forcing consideration of backwards and forwards compatibility early is healthy for a project that's going to keep going for the long haul.

Re: Show HN: Jason – A new JSON Library for Go

#52

Earlier quoted context omitted.

It's definitely safer, but it's a living hell having to define a class for each possible json structure in Java with gson.

Not sure where Java came into the conversation but I found a service[1] which I find really helpful for generating Golang structs from JSON. There is also a CLI tool[2] that does the same. [1] http://mholt.github.io/json-to-go/ [2] https://github.com/ChimeraCoder/gojson

Another one.

https://github.com/str1ngs/jflect

Re: Show HN: Jason – A new JSON Library for Go

#53
post #40
post #32

Earlier quoted context omitted.

>For anyone wondering why this is necessary, as some others in this thread have mentioned, the standard go way to deserialize json is to pass a pointer to a struct I sometimes have trouble understanding Go. It kind of seems to retain a lot of the bad parts of C in terms of developer-friendliness, while still being relatively slower than C and garbage-collected.

What are you having trouble with in this instance? Pointers and structs? Both are very simple, well understood concepts. Their implementation in Go is also more syntactically clear than in C.

I understand pointers and structs just fine, it's just that you would think a modern language like Go would avoid use of pointers to structs and "bring-your-own-buffer" type calling conventions except wherever possible. It makes for ugly developer APIs, in my opinion.

That is, in a "friendly" language I expect to generate JSON like this (in pseudocode):

    HashMap parsed = JSON::parse("{1: 2}");
not

    JSONObj obj;
    JSON::populate(&obj, "{1: 2}");

Re: Show HN: Jason – A new JSON Library for Go

#54
post #53
post #40

Earlier quoted context omitted.

What are you having trouble with in this instance? Pointers and structs? Both are very simple, well understood concepts. Their implementation in Go is also more syntactically clear than in C.

I understand pointers and structs just fine, it's just that you would think a modern language like Go would avoid use of pointers to structs and "bring-your-own-buffer" type calling conventions except wherever possible. It makes for ugly developer APIs, in my opinion. That is, in a "friendly" language I expect to generate JSON like this (in pseudocode): HashMap parsed = JSON::parse("{1: 2}"); not JSONObj obj; JSON::p…

The former is how it might work in a dynamically typed language. In a statically typed language, like Go, there are great benefits to decoding into the fields of struct with fields of known types.

Re: Show HN: Jason – A new JSON Library for Go

#55
post #12

This looks like a good approach to dealing with heterogeneous JSON! (ie, JSON that has an unknown and/or variable structure.) I've always found encoding/json a little clunky for this. Shameless but relevant plug - if you do know the structure of your JSON in advance, I wrote a tool for automatically generating the appropriate struct definition for it: https://github.com/ChimeraCoder/gojson (I see these as complimenta…

(Another plug here) I did too! Except it runs in the browser: https://mholt.github.io/json-to-go And I agree with you about the complementary aspect. Sometimes you can't control the fact that your JSON will have a widely varying structure... but when you can, these generators are nice.

Seems very handy! Thank you
Post reply on HN