Home >Backend Development >Golang >How Can I Efficiently Handle Type Assertion After Unmarshalling JSON into interface{} in Go?
In this scenario, JSON strings are transmitted via RabbitMQ. These strings represent instances of two different structs, Somthing1 and Somthing2. The goal is to unmarshal the JSON strings back into their respective structs and perform type assertions.
At first glance, it seems that unmarshalling into an interface{} and then applying type assertions should suffice. However, upon unmarshalling, the input variable is of type map[string]interface{}. This is not as expected, and attempts to switch on this type or reassign it to the desired structs fail.
The JSON library in Golang unmarshals into default types such as bool, float64, and map[string]interface{}. To obtain the desired structs, you need to unmarshal directly into them or manually convert from the map[string]interface{}.
The preferred approach is to unmarshal directly into the structs:
func typeAssert(msg string) { var job Somthing1 json.Unmarshal([]byte(msg), &job) // ... var stats Somthing2 json.Unmarshal([]byte(msg), &stats) // ... }
If unmarshalling directly is not feasible, you can use an Unpacker struct to handle the unmarshalling and provide an interface for accessing the data:
type Unpacker struct { Data interface{} } func (u *Unpacker) UnmarshalJSON(b []byte) error { // Attempt to unmarshal into both types smth1 := &Something1{} err := json.Unmarshal(b, smth1) if err == nil && smth1.Thing != "" { u.Data = smth1 return nil } smth2 := &Something2{} err = json.Unmarshal(b, smth2) if err != nil { return err } u.Data = smth2 return nil }
Then, you can use the Unpacker to access the unmarshalled data:
func typeAssert(msg string) { unpacker := &Unpacker{} json.Unmarshal([]byte(msg), unpacker) switch v := unpacker.Data.(type) { case Something1: // ... case Something2: // ... default: // Handle unknown type } }
The above is the detailed content of How Can I Efficiently Handle Type Assertion After Unmarshalling JSON into interface{} in Go?. For more information, please follow other related articles on the PHP Chinese website!