News for hackers. Stuff that matters.
Re:Thoughts on Go after writing 3 websites by lucian303 on Tuesday January 01, 2013 @03:08AM (#4992159)
PHP has json_decode() and json_encode() which do all of this correctly ... GO is interesting, but production ready? Not for awhile.
Re:Thoughts on Go after writing 3 websites by agentS on Tuesday January 01, 2013 @03:41AM (#4992202)
Package encoding/json from the standard library can decode to objects ("Object" in the Java sense, "void *" in the C++ sense) like json_decode does.

It also can decode directly into typed structures. An example from the documentation (you'll have to open the linked example): http://golang.org/pkg/encoding/json/#example_Decoder

Anecdotally, a typed version of JSON decoding is much nicer to work with.

Re:Thoughts on Go after writing 3 websites by lucian303 on Tuesday January 01, 2013 @04:02AM (#4992235)
"Anecdotally, a typed version of JSON decoding is much nicer to work with."

How so? Considering that JSON isn't strongly typed.

Re:Thoughts on Go after writing 3 websites by agentS on Tuesday January 01, 2013 @04:30AM (#4992272)
Indeed JSON is not strongly typed. However, in practice, every API, every data exchange format I've ever seen or used has used JSON in a strongly typed fashion. i.e. they say "the returned object will have a key of 'foo' with a number as the value, and a key of 'bar' with an array of strings as the value.

Why is this nicer?

Let's say you are working with the Reddit API. You can, of course, do it the way you suggested, and just decode the result into an untyped object. However, I find this incredibly brittle. You end up with strings indexing into arrays all over the place.

On the other hand, when you decode into a Go struct, you use the regular dot syntax to access parameters. You can't misspell these or use the value inappropriately (trying to loop over a JSON number, for example). You can write comments on the fields to document what you expect various values to hold. It is nicer in the same sense that using classes is nicer than just using arrays for everything.

Another benefit is of course the improvement of performance. Accessing an string value in a JSON object by a key is the equivalent of a hash table lookup in Python or PHP. It is incrementing a pointer by a constant number in Go (assuming you use strongly typed decoding).

I'll say it again, Go's encoding/json can do what you want (the equivalent of json_decode). There are better alternatives available to users of that package, and the GP was discussing how to exploit one of those better alternatives without having to write the struct by hand (which I personally find a trivial one time cost).

Re:Thoughts on Go after writing 3 websites by lucian303 on Tuesday January 01, 2013 @05:39AM (#4992358)
json_decode() without the second param or with it as false will create an object for you that is typed, loosely only if you mess with it afterwards. So what is the advantage other than speed? If I'm CPU bound, then yes, it's worth a look just as many other alternatives are worth a look. But that is a whole different issue.
Re:Thoughts on Go after writing 3 websites by kkowalczyk on Tuesday January 01, 2013 @07:00AM (#4992456)
Speed is pretty important.

Compiler telling you about a bug because you have a typo in your code (as opposed to blowing up at runtime, sometimes in a way that you won't notice) is also important.

Struct definition is also a documentation of your data. Another developer (or you a month from now) doesn't have to go back and re-read, say, Redit API docs. He (or you) can just re-read definition of the struct and know all the data that a given JSON request provides. That's nice.

An editor can parse the struct definition and give you auto-complete, which is also nice.

Re:Thoughts on Go after writing 3 websites by lucian303 on Tuesday January 01, 2013 @01:49PM (#4993449)
Really? Another static vs dynamically typed language argument? Not this year.
Re:Thoughts on Go after writing 3 websites by agentS on Tuesday January 01, 2013 @07:17AM (#4992478)
> json_decode() without the second param or with it as false will create an object for you that is typed, loosely only if you mess with it afterwards.

First, let me point out that we have moved on to a slightly different topic. The tools that json_decode gives you is available in Go (it would be syntactically awkward, out of a necessity born of Go being statically typed). An example: http://play.golang.org/p/OvshdY1oVQ

However, I would write it as follows http://play.golang.org/p/pODFi9Jrve. This is merely a different way of doing the same thing.

Now, to the topic that we are talking about is the claim that the latter is nicer. I was not comparing PHP and Go when I said that it was nicer (although I have some experience with PHP). I was drawing from my experience with working with JSON in statically typed languages. In particular, working with JSON in C++, Objective-C and Go. In all of these languages, the JSON libraries I used were essentially dynamically typed. I claimed merely that having the JSON library use reflection to deserialize into typed structures is nicer than using NSDictionary's various methods or maps in C++.

In PHP, the syntactic overhead of walking dynamic structures is obviously much lower than the languages I was thinking about, so that criticism is blunted when referring to that language. But I still believe there is some advantage there. A large part of what I find nicer about the latter is indeed the same reason I prefer static typing to dynamic typing. It is impossible to mistype the latter (try changing the ".Foo" to ".Fo" and trying to run the snippet). Consider that doing the equivalent in PHP will simply return NULL (I think), as it is really just doing a dictionary lookup. I fully admit that this is a static versus dynamic thing, not about JSON in particular.

As to the performance thing, I think you too easily discount the importance of low latency. But regardless, I was really comparing to Go's statically typed brethren. They essentially use hashtables for representing parsed JSON objects which is far less than ideal. Note that they don't employ the clever tricks that dynamic languages use. V8, for example, makes JS's objects (which are essentially hashtables) perform significantly better than a mere hashtable.

Re:Thoughts on Go after writing 3 websites by lucian303 on Tuesday January 01, 2013 @01:59PM (#4993482)
All good points. It would be interesting to see the different JSON implementations in different languages benchmarked. As far as static vs. dynamic, both have their pros and cons.
Re:Thoughts on Go after writing 3 websites by FuzzyDunlop on Tuesday January 01, 2013 @08:36AM (#4992562)
I think it's nice that you can basically document the expected response, in the form of creating a type with a struct.

In pseudo-code-ish:

    type FacebookUser struct {
        ProfileID int
        Name string
        ....
    }

    json.Unmarshal(response, FacebookUser)
Compare it to the implementations that just give you a hash:

    json_decode($response);

    json.parse response
Yeah, you can document it with comments, or visit the API documentation, but you can fix the code and let your comments go stale. You fix both your documentation and your implementation in one go in the first example.

It's certainly why I find it more appealing, anyway. Makes me feel more secure in my code.

Re:Thoughts on Go after writing 3 websites by munificent on Tuesday January 01, 2013 @03:18AM (#4992171)
PHP is dynamically typed, Go isn't.
Re:Thoughts on Go after writing 3 websites by lucian303 on Tuesday January 01, 2013 @04:03AM (#4992237)
And this really has no bearing on a discussion of JSON, a data exchange format.
Re:Thoughts on Go after writing 3 websites by saraid216 on Tuesday January 01, 2013 @04:37AM (#4992275)
Which is odd, since I thought this discussion was about Go, not PHP.
Re:Thoughts on Go after writing 3 websites by lucian303 on Tuesday January 01, 2013 @05:41AM (#4992360)
And you confused yourself. This thread is about JSON support in GO.
Re:Thoughts on Go after writing 3 websites by jlgreco on Tuesday January 01, 2013 @05:46AM (#4992367)
FYI: Go is not an acronym.
Re:Thoughts on Go after writing 3 websites by lucian303 on Tuesday January 01, 2013 @01:37PM (#4993399)
FIY is an acronym. Your turn.
Re:Thoughts on Go after writing 3 websites by jlgreco on Tuesday January 01, 2013 @01:44PM (#4993437)
Fix It Yourself?

Anyway my point, in case you missed it, is that "GO" is not correct.