Closed ironpark closed 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}
Try switching dec.DecodeInt64 and dec.DecodeInt64 to dec.String and dec.Int64
dec.DecodeInt64
dec.String
dec.Int64
it works!
JSON
GOLANG
LOG