I get a string
through a rabbitmq message system. Before sending,
I use json.Marshal
, convert the outcome to string
and send t
You have encountered a typical json vs typed language problem! Since json is untyped and schemaless, it is not possible to infer what data is "under the string" without actually decoding it.
So your only option is to unmarshal into an interface{}
which always produces a map[string]interface{}
. You could do some reflection magic here to build the final struct, but that's a lot of manual work and error prone.
Here are some possible solutions:
Let the json
package do the reflection stuff. Attempt to unmarshal into every expected type:
func typeAssert(msg string) {
var thing1 Something1
err := json.Unmarshal([]byte(msg), &thing1)
if err == nil{
// do something with thing1
return
}
var thing2 Something2
err = json.Unmarshal([]byte(msg), &thing2)
if err == nil{
// do something with thing2
return
}
//handle unsupported type
}
Defer the encoding until you know what's inside. Use this struct as an intermediate representation of your data:
type TypedJson struct{
Type string
Data json.RawMessage
}
Marshal:
thing := Something1{"asd",123}
tempJson, _ := json.Marshal(thing)
typedThing := TypedJson{"something1", tempJson}
finalJson, _ := json.Marshal(typedThing)
Unmarshal:
func typeAssert(msg string) {
var input TypedJson
json.Unmarshal([]byte(msg), &input)
switch input.Type{
case "something1":
var thing Something1
json.Unmarshal(input.Data, &thing)
queueStatsRes(thing)
case "something2":
var thing Something2
json.Unmarshal(input.Data, &thing)
queueStatsRes(thing)
default:
//handle unsupported type
}