You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The following code is working fine with "encoding/json" (msg fields are propagated after unmarshal). Unfortunately the "github.com/goccy/go-json" is unable to unmarshal.
package main
import (
"encoding/json"
"fmt"
"os"
)
type Message struct {
ChannelID int64
Data json.RawMessage
ChannelName string
Pair string
Sequence Seq
}
type Seq struct {
Value int64 `json:"sequence"`
}
func (msg *Message) UnmarshalJSON(data []byte) error {
var raw []json.RawMessage
if err := json.Unmarshal(data, &raw); err != nil {
return err
}
if len(raw) < 3 {
return fmt.Errorf("invalid data length: %#v", raw)
}
body := make([]interface{}, 0)
if len(raw) == 3 {
body = append(body, &msg.Data, &msg.ChannelName, &msg.Sequence)
}
body = append(body, &msg.ChannelID, &msg.Data, &msg.ChannelName, &msg.Pair)
return json.Unmarshal(data, &body)
}
func main() {
data := []byte(`[4688,{"as":[["28.15860","100.00000000","1621372849.562940"]],"bs":[["28.13020","1.28798426","1621372848.211647"]]},"book-10","DOT/GBP"]`)
var msg Message
if err := json.Unmarshal(data, &msg); err != nil {
fmt.Printf("error while unmarshaling JSON: %v", err)
os.Exit(0)
}
fmt.Printf("Channel name is %s", msg.ChannelName)
}
The following code is working fine with "encoding/json" (msg fields are propagated after unmarshal). Unfortunately the "github.com/goccy/go-json" is unable to unmarshal.
Go Playground
Thank you for your work!
The text was updated successfully, but these errors were encountered: