QUESTION
Is there a way to marshall JSON data in such a way that it can be unmarshalled in parts / sections?
Let's say that the top half of data is a "code" which would signal what to do with the bottom half ... such as unmarshall the bottom half into a specific struct depending on the "code".
There are two structs that may be sent as the bottom half ...
type Range Struct {
Start int
End int
}
type User struct {
ID int
Pass int
}
PSEUDO CODE EXAMPLE
It may look like this ...
message := &Message{
Code: 4,
&Range {
Start: 1,
End: 10,
}
}
Itt may look like this ...
message := &Message{
Code: 3,
&User {
ID: 1,
Pass: 1234,
}
}
So, when unmarshalling that data I could ...
// get code from top half
m := Message{}
err = json.UnMarshallTopHalf(byteArray, &m)
if m.Code == 4 {
// ok, the code was four, lets unmarshall into type Range
r := Range{}
json.UnmarshalBottomHalf(byteArray, &r)
}
I have looked at JSON & Go to learn how to marshall and unmarshall defined structs. I can do this, but I cannot figure out a way for arbitrary data as in the example above ...