mirror of
https://github.com/42wim/matterbridge.git
synced 2024-12-24 04:02:37 +01:00
.. | ||
decode.go | ||
encode.go | ||
fold.go | ||
indent.go | ||
README.md | ||
scanner.go | ||
stream.go | ||
tables.go | ||
tags.go |
jsonx
It’s modified version of encoding/json which
enables extra map field (with jsonx:"true"
tag) to catch
all other fields not declared in the struct.
jsonx
is a code name for internal use and not related to
JSONx.
Example (Run on playgroud)
package main
import (
"encoding/json"
"fmt"
"github.com/yaegashi/msgraph.go/jsonx"
)
type Extra struct {
string
X int
Y map[string]interface{} `json:"-" jsonx:"true"`
Extra }
func main() {
var x1, x2 Extra
:= []byte(`{"X":"123","Y":123,"A":"123","B":123}`)
b .Printf("\nUnmarshal input: %s\n", string(b))
fmt.Unmarshal(b, &x1)
json.Printf(" json.Unmarshal: %#v\n", x1)
fmt.Unmarshal(b, &x2)
jsonx.Printf("jsonx.Unmarshal: %#v\n", x2)
fmt
:= Extra{X: "456", Y: 456, Extra: map[string]interface{}{"A": "456", "B": 456}}
x .Printf("\nMarshal input: %#v\n", x)
fmt, _ := json.Marshal(x)
b1.Printf(" json.Marshal: %s\n", string(b1))
fmt, _ := jsonx.Marshal(x)
b2.Printf("jsonx.Marshal: %s\n", string(b2))
fmt}
Result
Unmarshal input: {"X":"123","Y":123,"A":"123","B":123}
json.Unmarshal: main.Extra{X:"123", Y:123, Extra:map[string]interface {}(nil)}
jsonx.Unmarshal: main.Extra{X:"123", Y:123, Extra:map[string]interface {}{"A":"123", "B":123}}
Marshal input: main.Extra{X:"456", Y:456, Extra:map[string]interface {}{"A":"456", "B":456}}
json.Marshal: {"X":"456","Y":456}
jsonx.Marshal: {"X":"456","Y":456,"A":"456","B":456}