mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
encoding/json: add "overflow" struct tag option
Fixes #6213. R=golang-dev, dsymonds, bradfitz CC=golang-dev https://golang.org/cl/13180043
This commit is contained in:
parent
9169372749
commit
466001d05d
5 changed files with 240 additions and 49 deletions
|
|
@ -33,6 +33,29 @@ func ExampleMarshal() {
|
|||
// {"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}
|
||||
}
|
||||
|
||||
func ExampleMarshal_overflow() {
|
||||
type Record struct {
|
||||
ID string
|
||||
Seq int
|
||||
Meta map[string]string `json:",overflow"`
|
||||
}
|
||||
r := Record{
|
||||
ID: "CheeseWhiz",
|
||||
Seq: 42,
|
||||
Meta: map[string]string{
|
||||
"Created": "1980-06-20",
|
||||
"Destroyed": "1998-02-06",
|
||||
},
|
||||
}
|
||||
b, err := json.Marshal(r)
|
||||
if err != nil {
|
||||
fmt.Println("error:", err)
|
||||
}
|
||||
os.Stdout.Write(b)
|
||||
// Output:
|
||||
// {"ID":"CheeseWhiz","Seq":42,"Created":"1980-06-20","Destroyed":"1998-02-06"}
|
||||
}
|
||||
|
||||
func ExampleUnmarshal() {
|
||||
var jsonBlob = []byte(`[
|
||||
{"Name": "Platypus", "Order": "Monotremata"},
|
||||
|
|
@ -52,6 +75,29 @@ func ExampleUnmarshal() {
|
|||
// [{Name:Platypus Order:Monotremata} {Name:Quoll Order:Dasyuromorphia}]
|
||||
}
|
||||
|
||||
func ExampleUnmarshal_overflow() {
|
||||
var jsonBlob = []byte(`
|
||||
{
|
||||
"Token": "Kaip4uM1ieng6Eiw",
|
||||
"User": "bimmler",
|
||||
"Animal": "rabbit"
|
||||
}
|
||||
`)
|
||||
type Auth struct {
|
||||
Token string
|
||||
User string
|
||||
Extra map[string]string `json:",overflow"`
|
||||
}
|
||||
var auth Auth
|
||||
err := json.Unmarshal(jsonBlob, &auth)
|
||||
if err != nil {
|
||||
fmt.Println("error:", err)
|
||||
}
|
||||
fmt.Printf("%+v", auth)
|
||||
// Output:
|
||||
// {Token:Kaip4uM1ieng6Eiw User:bimmler Extra:map[Animal:rabbit]}
|
||||
}
|
||||
|
||||
// This example uses a Decoder to decode a stream of distinct JSON values.
|
||||
func ExampleDecoder() {
|
||||
const jsonStream = `
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue