github.com/yaegashi/msgraph.go@v0.1.4/jsonx/README.md (about) 1 # jsonx 2 3 It's modified version of [encoding/json](https://golang.org/pkg/encoding/json/) 4 which enables extra map field (with `jsonx:"true"` tag) to catch all other fields not declared in the struct. 5 6 `jsonx` is a code name for internal use 7 and not related to [JSONx](https://tools.ietf.org/html/draft-rsalz-jsonx-00). 8 9 Example ([Run on playgroud](https://play.golang.org/p/TZi0JeHYG69)) 10 ```go 11 package main 12 13 import ( 14 "encoding/json" 15 "fmt" 16 17 "github.com/yaegashi/msgraph.go/jsonx" 18 ) 19 20 type Extra struct { 21 X string 22 Y int 23 Extra map[string]interface{} `json:"-" jsonx:"true"` 24 } 25 26 func main() { 27 var x1, x2 Extra 28 b := []byte(`{"X":"123","Y":123,"A":"123","B":123}`) 29 fmt.Printf("\nUnmarshal input: %s\n", string(b)) 30 json.Unmarshal(b, &x1) 31 fmt.Printf(" json.Unmarshal: %#v\n", x1) 32 jsonx.Unmarshal(b, &x2) 33 fmt.Printf("jsonx.Unmarshal: %#v\n", x2) 34 35 x := Extra{X: "456", Y: 456, Extra: map[string]interface{}{"A": "456", "B": 456}} 36 fmt.Printf("\nMarshal input: %#v\n", x) 37 b1, _ := json.Marshal(x) 38 fmt.Printf(" json.Marshal: %s\n", string(b1)) 39 b2, _ := jsonx.Marshal(x) 40 fmt.Printf("jsonx.Marshal: %s\n", string(b2)) 41 } 42 ``` 43 44 Result 45 46 ```text 47 Unmarshal input: {"X":"123","Y":123,"A":"123","B":123} 48 json.Unmarshal: main.Extra{X:"123", Y:123, Extra:map[string]interface {}(nil)} 49 jsonx.Unmarshal: main.Extra{X:"123", Y:123, Extra:map[string]interface {}{"A":"123", "B":123}} 50 51 Marshal input: main.Extra{X:"456", Y:456, Extra:map[string]interface {}{"A":"456", "B":456}} 52 json.Marshal: {"X":"456","Y":456} 53 jsonx.Marshal: {"X":"456","Y":456,"A":"456","B":456} 54 ``` 55