francoispqt / gojay

high performance JSON encoder/decoder with stream API for Golang
MIT License
2.11k stars 112 forks source link

Decoder.Interface does not work for booleans #72

Closed ymmt2005 closed 6 years ago

ymmt2005 commented 6 years ago

The following code should print:

&main.anyMap{"a":123, "b":"foo"}
&main.anyMap{"a":true}

However, currently it fails for boolean values as follows:

&main.anyMap{"a":123, "b":"foo"}
2018/09/05 09:57:40 invalid character ' ' in literal true (expecting 'e')
exit status 1
package main

import (
    "fmt"
    "log"

    "github.com/francoispqt/gojay"
)

type anyMap map[string]interface{}

func (a *anyMap) UnmarshalJSONObject(dec *gojay.Decoder, key string) error {
    var i interface{}
    err := dec.Interface(&i)
    if err != nil {
        return err
    }
    (*a)[key] = i
    return nil
}

func (a *anyMap) NKeys() int {
    return 0
}

func main() {
    m := &anyMap{}
    d := []byte(`{"a": 123, "b": "foo"}`)

    err := gojay.UnmarshalJSONObject(d, m)
    if err != nil {
        log.Fatal(err.Error())
    }
    fmt.Printf("%#v\n", m)

    m2 := &anyMap{}
    d2 := []byte(`{"a": true}`)
    err = gojay.UnmarshalJSONObject(d2, m2)
    if err != nil {
        log.Fatal(err.Error())
    }
    fmt.Printf("%#v\n", m)
}
kpbird commented 6 years ago

@ymmt2005 I could not find the method "err := dec.Interface(&i)"

screen shot 2018-09-05 at 10 38 31 am

ymmt2005 commented 6 years ago

@kpbird Interface is available in the master branch: https://godoc.org/github.com/francoispqt/gojay#Decoder.Interface

Update your copy with go get -u github.com/francoispqt/gojay then retry.

francoispqt commented 6 years ago

Hi, Seems it's an error on the assertTrue, assertFalse, assertNull decrementing the counter which causes the error. Will push a fix tomorrow.

francoispqt commented 6 years ago

Also, @ymmt2005 you don't need a pointer to a map, maps are references already.

ymmt2005 commented 6 years ago

Right. Thank you!

francoispqt commented 6 years ago

You're welcome ;) I just merged the fix to master, closing the issue. Thanks.

ymmt2005 commented 6 years ago

@francoispqt Confirmed that the issue is resolved. Thank you! But the commit included some debug logs: https://github.com/francoispqt/gojay/commit/4bfe9a336de8b0f1a350f53bcb5df3d327e6f42b#diff-3a8da64ea46826721c59c499b1184de1R34

Would you mind removing them?