README.md
1# jsonx
2
3It's modified version of [encoding/json](https://golang.org/pkg/encoding/json/)
4which 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
7and not related to [JSONx](https://tools.ietf.org/html/draft-rsalz-jsonx-00).
8
9Example ([Run on playgroud](https://play.golang.org/p/TZi0JeHYG69))
10```go
11package main
12
13import (
14 "encoding/json"
15 "fmt"
16
17 "github.com/yaegashi/msgraph.go/jsonx"
18)
19
20type Extra struct {
21 X string
22 Y int
23 Extra map[string]interface{} `json:"-" jsonx:"true"`
24}
25
26func 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
44Result
45
46```text
47Unmarshal input: {"X":"123","Y":123,"A":"123","B":123}
48 json.Unmarshal: main.Extra{X:"123", Y:123, Extra:map[string]interface {}(nil)}
49jsonx.Unmarshal: main.Extra{X:"123", Y:123, Extra:map[string]interface {}{"A":"123", "B":123}}
50
51Marshal input: main.Extra{X:"456", Y:456, Extra:map[string]interface {}{"A":"456", "B":456}}
52 json.Marshal: {"X":"456","Y":456}
53jsonx.Marshal: {"X":"456","Y":456,"A":"456","B":456}
54```
55
56