I have a stream of objects of different but known, types and I'm struggling to figure out how to unmarshal the JSON encoded stream into Go structs.
Here is a sample slice for illustration purpose
[
{
"uid": "xyz1",
"type": "person",
"name": "John",
"surname": "King"
},
{
"uid": "xyz2",
"type": "thing",
"car": "benz",
"shoes": "nike"
},
{
"uid": "xyz3",
"type": "person",
"name": "Mary",
"surname": "Queen"
}
]
As you can see there is a type
field that informs the receiver about what is the type of the object.
Data maps into the following Go types in the stream which "share" UID
and Type
fields:
type Person struct {
UID string
Type string
Name string
Surname string
}
type Thing struct {
UID string
Type string
Car string
Shoes string
}
I found a similar question here, though unanswered, it's a bit different from what I need to deal with. Instead of being guaranteed a transaction
keyspace as I've no guarantee what the payload should look like besides the type
and uid
fields.
Is there some way to this other than drowning deeply in reflect
package?