francoispqt / gojay

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

gojay can not unmarsharing some valid json #124

Closed ironpark closed 5 years ago

ironpark commented 5 years ago

JSON

{"table":"account","id":35,"type":"UPDATE"}

GOLANG

package publisher

import (
    "encoding/json"
    "github.com/francoispqt/gojay"
    "log"
    "strings"
    "testing"
)
type TableUpdateEvent struct {
    Table string `json:"table"`
    Id    int64 `json:"id"`
    Type  string `json:"type"`
}

func (t *TableUpdateEvent) UnmarshalJSONObject(dec *gojay.Decoder, key string) (err error) {
    log.Println("UnmarshalJSONObject",key)
    switch key {
    case "table":
        err := dec.DecodeString(&t.Table)
        if err != nil{
            log.Println("????")
        }
        return err
    case "id":
        return  dec.DecodeInt64(&t.Id)
    case "type":
        return dec.DecodeString(&t.Type)
    }
    return nil
}

func (t *TableUpdateEvent) NKeys() int {
    return 3
}

func TestA(t *testing.T){
    j := `{"table":"account","id":35,"type":"UPDATE"}`
    event := TableUpdateEvent{}
    err := gojay.UnmarshalJSONObject([]byte(j),&event)
    if err != nil{
        log.Println(err,strings.TrimSpace(j))
    }

    err = json.Unmarshal([]byte(j),&event)
    if err != nil{
        log.Println(err,strings.TrimSpace(j))
    }
    log.Println(event)
}

LOG

=== RUN   TestA
time="2019-08-18T07:34:27+09:00" level=info msg="UnmarshalJSONObject table"
2019/08/18 07:34:27 Invalid JSON, wrong char ':' found at position 23 {"table":"account","id":35,"type":"UPDATE"}
2019/08/18 07:34:27 {account 35 UPDATE}
noamt commented 5 years ago

Try switching dec.DecodeInt64 and dec.DecodeInt64 to dec.String and dec.Int64

ironpark commented 5 years ago

Try switching dec.DecodeInt64 and dec.DecodeInt64 to dec.String and dec.Int64

it works!