I cannot get my head around this problem. I have a simple struct, and another struct that uses it twice:
type Range struct {
Position int `json:"position"`
Length int `json:"length"`
}
type TwoRanges struct {
From Range `json:"from"`
To Range `json:"to"`
}
and the following json sample in file "ranges.json":
{
"from:": {
"position": 348,
"length": 15
},
"to": {
"position": 737,
"length" : 10
}
}
I try unmarshalling the sample in the following code:
func main() {
buffer, err := ioutil.ReadFile("ranges.json")
if err != nil {
log.Fatal(err)
}
var sample TwoRanges
if err = json.Unmarshal(buffer, &sample); err != nil {
log.Fatal(err)
}
fmt.Printf("%v
", sample)
}
The output is
{{0 0} {737 10}}
Somehow, only the half of the struct is unpacked.
- Is it a bug, or am I doing something wrong?
- Is there a "verbose mode" for the encoding/json package which would tell me what went wrong with the unmarshaller? It's not the first time that I've had mysterious problems with that package.