Created
July 9, 2019 04:57
-
-
Save reusee/8a13d522c281691258f4ed2d33d0b81b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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