Show HN: Jason – A new JSON Library for Go
github.com
github.com
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 complimentary tools, rather than conflicting or competing, since they would have two different use cases).
If your main use case is JSON as a serialized representation of the ever same object (plus or minus a few optional fields), encoding/json is by far the fastest and most typesafe way to go.
(edit: Protip - for the really really fastest way to demarshal JSON, look here https://github.com/pquerna/ffjson for some reflection-free extra speed after your code has settled down)
But if your objects can be heterogenous for whatever reasons (think third-party systems, different languages, legacy stacks...) or you're dealing with different kinds of objects landing on the same controller, jason is the perfect complimentary device to deal with more dynamic structures. I've long sought for something like this, thanks to the OP!
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.
Here's an example: http://play.golang.org/p/jrqBcPuQei
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.
You mean like this? http://play.golang.org/p/mMh7HGuTbe
Well, you can use json.RawMessage to delay decoding until other values are known[0].
However, this still requires that the input set is known at compile-time, which may not always be the case (some APIs sadly use a highly variable JSON schema, which can only be determined at runtime.)
You also don't need to pass a pointer to a struct. For example, you can pass an interface{}, which allows you to decode data into a type that is specified by the calling function, which I made heavy use of in this Twitter client library, for example[1].
(This is more or less how encoding/json itself works under the surface, when you think about it, but oftentimes people forget to take advantage of this idiom in practice.)
[0] http://golang.org/pkg/encoding/json/#RawMessage
[1] https://github.com/ChimeraCoder/anaconda/blob/master/users.g...
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.
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}");I don't understand this. Can you clarify why you think this is a good idea?
My experience with languages that have implicit "references" (Python, Java, JavaScript) is that it's not clear when you're sharing and when you're copying data, leading to bugs and unnecessary allocations, respectively.
Out of context, you sound insane.
Hint: Not the be confused with ...
if anyone is interested i did some slight modifications to the project above which are keep in my personal fork: https://github.com/W4RH4WK/simplejson
Deleted comment
It's unfortunate that so many of these "Show HN" threads turn into this.
But ok, I will delete the post anyway.
That's what the Get-method in Jason is trying to solve. Similar to how bitly/go-simplejson does it, but with some fine tuning.
The author does seem to say the library is more focused on reading the data rather than writing it.
Whether or not this is a benefit or not is up to you and your use case I guess.