Skip to content

Instantly share code, notes, and snippets.

@reusee
Created July 9, 2019 04:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reusee/8a13d522c281691258f4ed2d33d0b81b to your computer and use it in GitHub Desktop.
Save reusee/8a13d522c281691258f4ed2d33d0b81b to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"fmt"
)
type Item struct {
Raw json.RawMessage
Values map[string][]int
}
var _ json.Unmarshaler = new(Item)
func (i *Item) UnmarshalJSON(raw []byte) error {
i.Raw = raw
return json.Unmarshal(raw, &i.Values)
}
func main() {
raw := []byte(`
{
"foo": [1, 2, 3],
"bar": [4, 5, 6]
}
`)
var i Item
if err := json.Unmarshal(raw, &i); err != nil {
panic(err)
}
fmt.Printf("%s\n", i.Raw)
fmt.Printf("%v\n", i.Values)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment