Adding upstream version 0.10.5.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
104c0c203d
commit
e733edafba
141 changed files with 102352 additions and 0 deletions
1862
test/cover/cover_array_test.go
Normal file
1862
test/cover/cover_array_test.go
Normal file
File diff suppressed because it is too large
Load diff
2675
test/cover/cover_bool_test.go
Normal file
2675
test/cover/cover_bool_test.go
Normal file
File diff suppressed because it is too large
Load diff
1829
test/cover/cover_bytes_test.go
Normal file
1829
test/cover/cover_bytes_test.go
Normal file
File diff suppressed because it is too large
Load diff
2356
test/cover/cover_float32_test.go
Normal file
2356
test/cover/cover_float32_test.go
Normal file
File diff suppressed because it is too large
Load diff
2356
test/cover/cover_float64_test.go
Normal file
2356
test/cover/cover_float64_test.go
Normal file
File diff suppressed because it is too large
Load diff
55
test/cover/cover_helper_test.go
Normal file
55
test/cover/cover_helper_test.go
Normal file
|
@ -0,0 +1,55 @@
|
|||
package json_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
stdjson "encoding/json"
|
||||
|
||||
"github.com/goccy/go-json"
|
||||
)
|
||||
|
||||
func intptr(v int) *int { return &v }
|
||||
func intptr3(v int) ***int { vv := &v; vvv := &vv; return &vvv }
|
||||
func int8ptr(v int8) *int8 { return &v }
|
||||
func int8ptr3(v int8) ***int8 { vv := &v; vvv := &vv; return &vvv }
|
||||
func int16ptr(v int16) *int16 { return &v }
|
||||
func int16ptr3(v int16) ***int16 { vv := &v; vvv := &vv; return &vvv }
|
||||
func int32ptr(v int32) *int32 { return &v }
|
||||
func int32ptr3(v int32) ***int32 { vv := &v; vvv := &vv; return &vvv }
|
||||
func int64ptr(v int64) *int64 { return &v }
|
||||
func int64ptr3(v int64) ***int64 { vv := &v; vvv := &vv; return &vvv }
|
||||
func uptr(v uint) *uint { return &v }
|
||||
func uintptr3(v uint) ***uint { vv := &v; vvv := &vv; return &vvv }
|
||||
func uint8ptr(v uint8) *uint8 { return &v }
|
||||
func uint8ptr3(v uint8) ***uint8 { vv := &v; vvv := &vv; return &vvv }
|
||||
func uint16ptr(v uint16) *uint16 { return &v }
|
||||
func uint16ptr3(v uint16) ***uint16 { vv := &v; vvv := &vv; return &vvv }
|
||||
func uint32ptr(v uint32) *uint32 { return &v }
|
||||
func uint32ptr3(v uint32) ***uint32 { vv := &v; vvv := &vv; return &vvv }
|
||||
func uint64ptr(v uint64) *uint64 { return &v }
|
||||
func uint64ptr3(v uint64) ***uint64 { vv := &v; vvv := &vv; return &vvv }
|
||||
func float32ptr(v float32) *float32 { return &v }
|
||||
func float32ptr3(v float32) ***float32 { vv := &v; vvv := &vv; return &vvv }
|
||||
func float64ptr(v float64) *float64 { return &v }
|
||||
func float64ptr3(v float64) ***float64 { vv := &v; vvv := &vv; return &vvv }
|
||||
func stringptr(v string) *string { return &v }
|
||||
func stringptr3(v string) ***string { vv := &v; vvv := &vv; return &vvv }
|
||||
func boolptr(v bool) *bool { return &v }
|
||||
func boolptr3(v bool) ***bool { vv := &v; vvv := &vv; return &vvv }
|
||||
func bytesptr(v []byte) *[]byte { return &v }
|
||||
func bytesptr3(v []byte) ***[]byte { vv := &v; vvv := &vv; return &vvv }
|
||||
func numberptr(v json.Number) *json.Number { return &v }
|
||||
func numberptr3(v json.Number) ***json.Number { vv := &v; vvv := &vv; return &vvv }
|
||||
func sliceptr(v []int) *[]int { return &v }
|
||||
func arrayptr(v [2]int) *[2]int { return &v }
|
||||
func mapptr(v map[string]int) *map[string]int { return &v }
|
||||
|
||||
func encodeByEncodingJSON(data interface{}, indent, escape bool) string {
|
||||
var buf bytes.Buffer
|
||||
enc := stdjson.NewEncoder(&buf)
|
||||
enc.SetEscapeHTML(escape)
|
||||
if indent {
|
||||
enc.SetIndent("", " ")
|
||||
}
|
||||
enc.Encode(data)
|
||||
return buf.String()
|
||||
}
|
1833
test/cover/cover_int16_test.go
Normal file
1833
test/cover/cover_int16_test.go
Normal file
File diff suppressed because it is too large
Load diff
1810
test/cover/cover_int32_test.go
Normal file
1810
test/cover/cover_int32_test.go
Normal file
File diff suppressed because it is too large
Load diff
1810
test/cover/cover_int64_test.go
Normal file
1810
test/cover/cover_int64_test.go
Normal file
File diff suppressed because it is too large
Load diff
1810
test/cover/cover_int8_test.go
Normal file
1810
test/cover/cover_int8_test.go
Normal file
File diff suppressed because it is too large
Load diff
2432
test/cover/cover_int_test.go
Normal file
2432
test/cover/cover_int_test.go
Normal file
File diff suppressed because it is too large
Load diff
1903
test/cover/cover_map_test.go
Normal file
1903
test/cover/cover_map_test.go
Normal file
File diff suppressed because it is too large
Load diff
3712
test/cover/cover_marshal_json_test.go
Normal file
3712
test/cover/cover_marshal_json_test.go
Normal file
File diff suppressed because it is too large
Load diff
3549
test/cover/cover_marshal_text_test.go
Normal file
3549
test/cover/cover_marshal_text_test.go
Normal file
File diff suppressed because it is too large
Load diff
2356
test/cover/cover_number_test.go
Normal file
2356
test/cover/cover_number_test.go
Normal file
File diff suppressed because it is too large
Load diff
2099
test/cover/cover_slice_test.go
Normal file
2099
test/cover/cover_slice_test.go
Normal file
File diff suppressed because it is too large
Load diff
2392
test/cover/cover_string_test.go
Normal file
2392
test/cover/cover_string_test.go
Normal file
File diff suppressed because it is too large
Load diff
1810
test/cover/cover_uint16_test.go
Normal file
1810
test/cover/cover_uint16_test.go
Normal file
File diff suppressed because it is too large
Load diff
1810
test/cover/cover_uint32_test.go
Normal file
1810
test/cover/cover_uint32_test.go
Normal file
File diff suppressed because it is too large
Load diff
1810
test/cover/cover_uint64_test.go
Normal file
1810
test/cover/cover_uint64_test.go
Normal file
File diff suppressed because it is too large
Load diff
1810
test/cover/cover_uint8_test.go
Normal file
1810
test/cover/cover_uint8_test.go
Normal file
File diff suppressed because it is too large
Load diff
2356
test/cover/cover_uint_test.go
Normal file
2356
test/cover/cover_uint_test.go
Normal file
File diff suppressed because it is too large
Load diff
74
test/example/example_marshaling_test.go
Normal file
74
test/example/example_marshaling_test.go
Normal file
|
@ -0,0 +1,74 @@
|
|||
// Copyright 2016 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package json_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"github.com/goccy/go-json"
|
||||
)
|
||||
|
||||
type Animal int
|
||||
|
||||
const (
|
||||
Unknown Animal = iota
|
||||
Gopher
|
||||
Zebra
|
||||
)
|
||||
|
||||
func (a *Animal) UnmarshalJSON(b []byte) error {
|
||||
var s string
|
||||
if err := json.Unmarshal(b, &s); err != nil {
|
||||
return err
|
||||
}
|
||||
switch strings.ToLower(s) {
|
||||
default:
|
||||
*a = Unknown
|
||||
case "gopher":
|
||||
*a = Gopher
|
||||
case "zebra":
|
||||
*a = Zebra
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a Animal) MarshalJSON() ([]byte, error) {
|
||||
var s string
|
||||
switch a {
|
||||
default:
|
||||
s = "unknown"
|
||||
case Gopher:
|
||||
s = "gopher"
|
||||
case Zebra:
|
||||
s = "zebra"
|
||||
}
|
||||
|
||||
return json.Marshal(s)
|
||||
}
|
||||
|
||||
func Example_customMarshalJSON() {
|
||||
blob := `["gopher","armadillo","zebra","unknown","gopher","bee","gopher","zebra"]`
|
||||
var zoo []Animal
|
||||
if err := json.Unmarshal([]byte(blob), &zoo); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
census := make(map[Animal]int)
|
||||
for _, animal := range zoo {
|
||||
census[animal] += 1
|
||||
}
|
||||
|
||||
fmt.Printf("Zoo Census:\n* Gophers: %d\n* Zebras: %d\n* Unknown: %d\n",
|
||||
census[Gopher], census[Zebra], census[Unknown])
|
||||
|
||||
// Output:
|
||||
// Zoo Census:
|
||||
// * Gophers: 3
|
||||
// * Zebras: 2
|
||||
// * Unknown: 3
|
||||
}
|
96
test/example/example_query_test.go
Normal file
96
test/example/example_query_test.go
Normal file
|
@ -0,0 +1,96 @@
|
|||
package json_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/goccy/go-json"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
ID int64
|
||||
Name string
|
||||
Age int
|
||||
Address UserAddressResolver
|
||||
}
|
||||
|
||||
type UserAddress struct {
|
||||
UserID int64
|
||||
PostCode string
|
||||
City string
|
||||
Address1 string
|
||||
Address2 string
|
||||
}
|
||||
|
||||
type UserRepository struct {
|
||||
uaRepo *UserAddressRepository
|
||||
}
|
||||
|
||||
func NewUserRepository() *UserRepository {
|
||||
return &UserRepository{
|
||||
uaRepo: NewUserAddressRepository(),
|
||||
}
|
||||
}
|
||||
|
||||
type UserAddressRepository struct{}
|
||||
|
||||
func NewUserAddressRepository() *UserAddressRepository {
|
||||
return &UserAddressRepository{}
|
||||
}
|
||||
|
||||
type UserAddressResolver func(context.Context) (*UserAddress, error)
|
||||
|
||||
func (resolver UserAddressResolver) MarshalJSON(ctx context.Context) ([]byte, error) {
|
||||
address, err := resolver(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return json.MarshalContext(ctx, address)
|
||||
}
|
||||
|
||||
func (r *UserRepository) FindByID(ctx context.Context, id int64) (*User, error) {
|
||||
user := &User{ID: id, Name: "Ken", Age: 20}
|
||||
// resolve relation from User to UserAddress
|
||||
user.Address = func(ctx context.Context) (*UserAddress, error) {
|
||||
return r.uaRepo.FindByUserID(ctx, user.ID)
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (*UserAddressRepository) FindByUserID(ctx context.Context, id int64) (*UserAddress, error) {
|
||||
return &UserAddress{
|
||||
UserID: id,
|
||||
City: "A",
|
||||
Address1: "foo",
|
||||
Address2: "bar",
|
||||
}, nil
|
||||
}
|
||||
|
||||
func Example_fieldQuery() {
|
||||
ctx := context.Background()
|
||||
userRepo := NewUserRepository()
|
||||
user, err := userRepo.FindByID(ctx, 1)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
query, err := json.BuildFieldQuery(
|
||||
"Name",
|
||||
"Age",
|
||||
json.BuildSubFieldQuery("Address").Fields(
|
||||
"City",
|
||||
),
|
||||
)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
ctx = json.SetFieldQueryToContext(ctx, query)
|
||||
b, err := json.MarshalContext(ctx, user)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println(string(b))
|
||||
|
||||
// Output:
|
||||
// {"Name":"Ken","Age":20,"Address":{"City":"A"}}
|
||||
}
|
313
test/example/example_test.go
Normal file
313
test/example/example_test.go
Normal file
|
@ -0,0 +1,313 @@
|
|||
// Copyright 2011 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package json_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/goccy/go-json"
|
||||
)
|
||||
|
||||
func ExampleMarshal() {
|
||||
type ColorGroup struct {
|
||||
ID int
|
||||
Name string
|
||||
Colors []string
|
||||
}
|
||||
group := ColorGroup{
|
||||
ID: 1,
|
||||
Name: "Reds",
|
||||
Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
|
||||
}
|
||||
b, err := json.Marshal(group)
|
||||
if err != nil {
|
||||
fmt.Println("error:", err)
|
||||
}
|
||||
os.Stdout.Write(b)
|
||||
// Output:
|
||||
// {"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}
|
||||
}
|
||||
|
||||
func ExampleUnmarshal() {
|
||||
var jsonBlob = []byte(`[
|
||||
{"Name": "Platypus", "Order": "Monotremata"},
|
||||
{"Name": "Quoll", "Order": "Dasyuromorphia"}
|
||||
]`)
|
||||
type Animal struct {
|
||||
Name string
|
||||
Order string
|
||||
}
|
||||
var animals []Animal
|
||||
err := json.Unmarshal(jsonBlob, &animals)
|
||||
if err != nil {
|
||||
fmt.Println("error:", err)
|
||||
}
|
||||
fmt.Printf("%+v", animals)
|
||||
// Output:
|
||||
// [{Name:Platypus Order:Monotremata} {Name:Quoll Order:Dasyuromorphia}]
|
||||
}
|
||||
|
||||
// This example uses a Decoder to decode a stream of distinct JSON values.
|
||||
func ExampleDecoder() {
|
||||
const jsonStream = `
|
||||
{"Name": "Ed", "Text": "Knock knock."}
|
||||
{"Name": "Sam", "Text": "Who's there?"}
|
||||
{"Name": "Ed", "Text": "Go fmt."}
|
||||
{"Name": "Sam", "Text": "Go fmt who?"}
|
||||
{"Name": "Ed", "Text": "Go fmt yourself!"}
|
||||
`
|
||||
type Message struct {
|
||||
Name, Text string
|
||||
}
|
||||
dec := json.NewDecoder(strings.NewReader(jsonStream))
|
||||
for {
|
||||
var m Message
|
||||
if err := dec.Decode(&m); err == io.EOF {
|
||||
break
|
||||
} else if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Printf("%s: %s\n", m.Name, m.Text)
|
||||
}
|
||||
// Output:
|
||||
// Ed: Knock knock.
|
||||
// Sam: Who's there?
|
||||
// Ed: Go fmt.
|
||||
// Sam: Go fmt who?
|
||||
// Ed: Go fmt yourself!
|
||||
}
|
||||
|
||||
// This example uses a Decoder to decode a stream of distinct JSON values.
|
||||
func ExampleDecoder_Token() {
|
||||
const jsonStream = `
|
||||
{"Message": "Hello", "Array": [1, 2, 3], "Null": null, "Number": 1.234}
|
||||
`
|
||||
dec := json.NewDecoder(strings.NewReader(jsonStream))
|
||||
for {
|
||||
t, err := dec.Token()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Printf("%T: %v", t, t)
|
||||
if dec.More() {
|
||||
fmt.Printf(" (more)")
|
||||
}
|
||||
fmt.Printf("\n")
|
||||
}
|
||||
// Output:
|
||||
// json.Delim: { (more)
|
||||
// string: Message (more)
|
||||
// string: Hello (more)
|
||||
// string: Array (more)
|
||||
// json.Delim: [ (more)
|
||||
// float64: 1 (more)
|
||||
// float64: 2 (more)
|
||||
// float64: 3
|
||||
// json.Delim: ] (more)
|
||||
// string: Null (more)
|
||||
// <nil>: <nil> (more)
|
||||
// string: Number (more)
|
||||
// float64: 1.234
|
||||
// json.Delim: }
|
||||
}
|
||||
|
||||
// This example uses a Decoder to decode a streaming array of JSON objects.
|
||||
func ExampleDecoder_Decode_stream() {
|
||||
const jsonStream = `
|
||||
[
|
||||
{"Name": "Ed", "Text": "Knock knock."},
|
||||
{"Name": "Sam", "Text": "Who's there?"},
|
||||
{"Name": "Ed", "Text": "Go fmt."},
|
||||
{"Name": "Sam", "Text": "Go fmt who?"},
|
||||
{"Name": "Ed", "Text": "Go fmt yourself!"}
|
||||
]
|
||||
`
|
||||
type Message struct {
|
||||
Name, Text string
|
||||
}
|
||||
dec := json.NewDecoder(strings.NewReader(jsonStream))
|
||||
|
||||
// read open bracket
|
||||
t, err := dec.Token()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Printf("%T: %v\n", t, t)
|
||||
|
||||
// while the array contains values
|
||||
for dec.More() {
|
||||
var m Message
|
||||
// decode an array value (Message)
|
||||
err := dec.Decode(&m)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Printf("%v: %v\n", m.Name, m.Text)
|
||||
}
|
||||
|
||||
// read closing bracket
|
||||
t, err = dec.Token()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Printf("%T: %v\n", t, t)
|
||||
|
||||
// Output:
|
||||
// json.Delim: [
|
||||
// Ed: Knock knock.
|
||||
// Sam: Who's there?
|
||||
// Ed: Go fmt.
|
||||
// Sam: Go fmt who?
|
||||
// Ed: Go fmt yourself!
|
||||
// json.Delim: ]
|
||||
}
|
||||
|
||||
// This example uses RawMessage to delay parsing part of a JSON message.
|
||||
func ExampleRawMessage_unmarshal() {
|
||||
type Color struct {
|
||||
Space string
|
||||
Point json.RawMessage // delay parsing until we know the color space
|
||||
}
|
||||
type RGB struct {
|
||||
R uint8
|
||||
G uint8
|
||||
B uint8
|
||||
}
|
||||
type YCbCr struct {
|
||||
Y uint8
|
||||
Cb int8
|
||||
Cr int8
|
||||
}
|
||||
|
||||
var j = []byte(`[
|
||||
{"Space": "YCbCr", "Point": {"Y": 255, "Cb": 0, "Cr": -10}},
|
||||
{"Space": "RGB", "Point": {"R": 98, "G": 218, "B": 255}}
|
||||
]`)
|
||||
var colors []Color
|
||||
err := json.Unmarshal(j, &colors)
|
||||
if err != nil {
|
||||
log.Fatalln("error:", err)
|
||||
}
|
||||
|
||||
for _, c := range colors {
|
||||
var dst interface{}
|
||||
switch c.Space {
|
||||
case "RGB":
|
||||
dst = new(RGB)
|
||||
case "YCbCr":
|
||||
dst = new(YCbCr)
|
||||
}
|
||||
err := json.Unmarshal(c.Point, dst)
|
||||
if err != nil {
|
||||
log.Fatalln("error:", err)
|
||||
}
|
||||
fmt.Println(c.Space, dst)
|
||||
}
|
||||
// Output:
|
||||
// YCbCr &{255 0 -10}
|
||||
// RGB &{98 218 255}
|
||||
}
|
||||
|
||||
// This example uses RawMessage to use a precomputed JSON during marshal.
|
||||
func ExampleRawMessage_marshal() {
|
||||
h := json.RawMessage(`{"precomputed": true}`)
|
||||
|
||||
c := struct {
|
||||
Header *json.RawMessage `json:"header"`
|
||||
Body string `json:"body"`
|
||||
}{Header: &h, Body: "Hello Gophers!"}
|
||||
|
||||
b, err := json.MarshalIndent(&c, "", "\t")
|
||||
if err != nil {
|
||||
fmt.Println("error:", err)
|
||||
}
|
||||
os.Stdout.Write(b)
|
||||
|
||||
// Output:
|
||||
// {
|
||||
// "header": {
|
||||
// "precomputed": true
|
||||
// },
|
||||
// "body": "Hello Gophers!"
|
||||
// }
|
||||
}
|
||||
|
||||
func ExampleIndent() {
|
||||
type Road struct {
|
||||
Name string
|
||||
Number int
|
||||
}
|
||||
roads := []Road{
|
||||
{"Diamond Fork", 29},
|
||||
{"Sheep Creek", 51},
|
||||
}
|
||||
|
||||
b, err := json.Marshal(roads)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
var out bytes.Buffer
|
||||
json.Indent(&out, b, "=", "\t")
|
||||
out.WriteTo(os.Stdout)
|
||||
// Output:
|
||||
// [
|
||||
// = {
|
||||
// = "Name": "Diamond Fork",
|
||||
// = "Number": 29
|
||||
// = },
|
||||
// = {
|
||||
// = "Name": "Sheep Creek",
|
||||
// = "Number": 51
|
||||
// = }
|
||||
// =]
|
||||
}
|
||||
|
||||
/*
|
||||
func ExampleMarshalIndent() {
|
||||
data := map[string]int{
|
||||
"a": 1,
|
||||
"b": 2,
|
||||
}
|
||||
|
||||
json, err := json.MarshalIndent(data, "<prefix>", "<indent>")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Println(string(json))
|
||||
// Output:
|
||||
// {
|
||||
// <prefix><indent>"a": 1,
|
||||
// <prefix><indent>"b": 2
|
||||
// <prefix>}
|
||||
}
|
||||
*/
|
||||
|
||||
func ExampleValid() {
|
||||
goodJSON := `{"example": 1}`
|
||||
badJSON := `{"example":2:]}}`
|
||||
|
||||
fmt.Println(json.Valid([]byte(goodJSON)), json.Valid([]byte(badJSON)))
|
||||
// Output:
|
||||
// true false
|
||||
}
|
||||
|
||||
func ExampleHTMLEscape() {
|
||||
var out bytes.Buffer
|
||||
json.HTMLEscape(&out, []byte(`{"Name":"<b>HTML content</b>"}`))
|
||||
out.WriteTo(os.Stdout)
|
||||
// Output:
|
||||
//{"Name":"\u003cb\u003eHTML content\u003c/b\u003e"}
|
||||
}
|
68
test/example/example_text_marshaling_test.go
Normal file
68
test/example/example_text_marshaling_test.go
Normal file
|
@ -0,0 +1,68 @@
|
|||
// Copyright 2018 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package json_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"github.com/goccy/go-json"
|
||||
)
|
||||
|
||||
type Size int
|
||||
|
||||
const (
|
||||
unrecognized Size = iota
|
||||
small
|
||||
large
|
||||
)
|
||||
|
||||
func (s *Size) UnmarshalText(text []byte) error {
|
||||
switch strings.ToLower(string(text)) {
|
||||
default:
|
||||
*s = unrecognized
|
||||
case "small":
|
||||
*s = small
|
||||
case "large":
|
||||
*s = large
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s Size) MarshalText() ([]byte, error) {
|
||||
var name string
|
||||
switch s {
|
||||
default:
|
||||
name = "unrecognized"
|
||||
case small:
|
||||
name = "small"
|
||||
case large:
|
||||
name = "large"
|
||||
}
|
||||
return []byte(name), nil
|
||||
}
|
||||
|
||||
func Example_textMarshalJSON() {
|
||||
blob := `["small","regular","large","unrecognized","small","normal","small","large"]`
|
||||
var inventory []Size
|
||||
if err := json.Unmarshal([]byte(blob), &inventory); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
counts := make(map[Size]int)
|
||||
for _, size := range inventory {
|
||||
counts[size] += 1
|
||||
}
|
||||
|
||||
fmt.Printf("Inventory Counts:\n* Small: %d\n* Large: %d\n* Unrecognized: %d\n",
|
||||
counts[small], counts[large], counts[unrecognized])
|
||||
|
||||
// Output:
|
||||
// Inventory Counts:
|
||||
// * Small: 3
|
||||
// * Large: 2
|
||||
// * Unrecognized: 3
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue