pquerna / ffjson

faster JSON serialization for Go
Apache License 2.0
2.96k stars 235 forks source link

[]struct{} leads to incorrectly generated code #228

Open echlebek opened 7 years ago

echlebek commented 7 years ago

The attached example leads to code that does not build. (Go 1.9 on my machine, but almost certainly on all release versions of Go.)

package ffjsonbug

type Foo struct {
        Bar []struct{}
}

Here is the relevant section of generated code:

        if j.Bar != nil {
            buf.WriteString(`[`)
            for i, v := range j.Bar {
                if i != 0 {
                    buf.WriteString(`,`)
                }
                /* Inline struct. type=struct {} kind=struct */
                buf.WriteString(`{}`)
            }
            buf.WriteString(`]`)
        } else {
            buf.WriteString(`null`)
        }

The special case for struct{} causes v to be un-used.