In Go, when working with JSON data, it is sometimes necessary to partially unmarshal the data in order to access unknown fields or handle heterogeneous JSON structures. However, this process can lead to double unmarshaling, which can be inefficient. This article presents a solution to avoid this issue using json.RawMessage.
Consider a scenario where a web socket connection sends different types of JSON messages, and you wish to unmarshal them into known structs. The traditional approach involves unmarshaling the JSON into a generic map[string]interface{}, searching for the desired key, and then attempting to cast the value to the desired type. However, this approach can fail when working with nested JSON structures.
To resolve this issue, instead of unmarshaling the values into interface{}, we can unmarshal them into json.RawMessage. This allows us to access the raw JSON data without losing the type information. Later, in the switch statement, we can directly unmarshal the raw JSON data into the desired struct without the need to re-marshal it.
import ( "encoding/json" "fmt" ) type Ping struct { Ping string `json:"ping"` } type Ack struct { Messages []Message `json:"messages"` } type Message string func main() { testJSON := []byte(`{"ack":{"messages":["Hi there","Hi again"]}}`) var myAck = Ack{} var myMap map[string]json.RawMessage err := json.Unmarshal(testJSON, &myMap) if err != nil { fmt.Println("error unmarshalling: ", err) } for k, v := range myMap { fmt.Printf("key: %s, value: %s \n", k, v) switch k { case "ping": fmt.Println(k, " is a ping", v) case "ack": fmt.Println(k, " is an ack containing a message list") err = json.Unmarshal(v, &myAck) if err != nil { fmt.Println("unmarshal error", err) } else { fmt.Println("New ack object: ", myAck) } default: fmt.Printf("%s is of a type (%T) I don't know how to handle", k, v) } } }
With this approach, we avoid the redundant step of marshalling and unmarshalling the JSON data, making it more efficient and straightforward to handle JSON data with heterogeneous structures.
The above is the detailed content of How Can json.RawMessage Prevent Double Unmarshaling in Go When Handling Heterogeneous JSON Structures?. For more information, please follow other related articles on the PHP Chinese website!