
JSON, interfaces, and go generate - campoy
http://talks.golang.org/2015/json.slide#1
======
sago
In case anyone else is sitting looking at the first slide, clicking, scrolling
and so on, and seeing nothing else. Press left and right arrow keys.

~~~
jaytaylor
Thanks for that. Any tips on how to get past the 3rd slide? ;)

~~~
campoy
Press the right arrow a fourth time :)

~~~
jaytaylor
Ah-ha, I was attempting to swipe my way through with the trackpad. The arrow
keys work much better.

Thanks!

------
moe
This is something I really don't like about Go.

80 lines of code to parse some JSON?

Meanwhile, in ruby...

    
    
      foo = JSON.parse(STDIN.read)

~~~
jweir
It is no where near 80 lines required to parse JSON.

[http://play.golang.org/p/H5yFV6VYaT](http://play.golang.org/p/H5yFV6VYaT)

There is some boiler plate, since you have a typed system, so you must define
your types. Beyond that two lines of Go will parse a JSON blob.

    
    
        people := []Person{}
        json.Unmarshal(json, &people)
    

The article is giving examples of using an Unmarshal interface to convert the
JSON code into typed Go objects. Hence more lines of code.

Such as the example of converting the JSON time string into a Time type.

edit: updated the example link

~~~
Gurkenmaster
How does this deal with deserializing json arrays? Unless you have reified
generics it's impossible. This is a common headache when working with Jackson.

~~~
breakingcups
The example linked and described already deals with deserializing json arrays.
What trouble exactly are you experiencing?

~~~
Spiritus
Not the parent, but my biggest issue with arrays and nested objects is this:

Most JSON arrays come in the form

    
    
        {
            "objects": [{"foo": "bar"}, ...]
        }
    

Instead of `[{"foo": "bar"}, ...]`. So I need to create a temporary struct, it
would've been nice to be able to provide a starting point or similar. Like
`json.UnmarshalFrom(data, &foos, "objects")` (hmm, this gave me an idea!).

Another example is this:

    
    
        type Address struct {
            street string
            city   string
        }
    

And I want to parse that populate that struct with the data from

    
    
        {
            "name": "John Doe",
            "address": {
                "street": "Some street 1",
                "city": "Footown"
            },
            ...
        }
    

Again I have to use temporary struct.

~~~
bcgraham
For deeply nested structures, it'll always be a bit of a hassle. In your toy
example, you can use struct literals to get to the guts of the object quickly.

[https://play.golang.org/p/VE90fwT0sE](https://play.golang.org/p/VE90fwT0sE)

