-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsoner_test.go
61 lines (57 loc) · 1.58 KB
/
jsoner_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package jsoner_test
import (
"testing"
"github.com/gomig/jsoner"
)
func TestJsoner(t *testing.T) {
type Book struct {
Title string
ISBN string `json:"isbn"`
}
type Author struct {
Name string `json:"name"`
Family string `json:"family"`
Age int `json:"age,string,omitempty"`
IsMariage bool `json:",string"`
Books []Book `json:"author_books"`
Skills []string
Address map[string]any
// ignored fields
PrivateField string `json:"-"`
ignored string
}
john := Author{
Name: "John",
Family: "Doe",
Age: 0,
IsMariage: false,
Books: []Book{
{Title: "Basics Of C", ISBN: "12345"},
{Title: "Golang", ISBN: "88888"},
},
Skills: []string{"Web dev", "System programming", "IOT"},
Address: map[string]any{
"state": map[string]string{
"country": "USA",
"county": "NY",
},
"city": "NY city",
"street": "ST. 23",
"no": 13,
},
PrivateField: "Some private information",
ignored: "i'm ignored",
}
options := map[string]jsoner.JsonerOption{
".": {Ignore: []string{"family"}},
"Address.state": {Ignore: []string{"country"}},
"author_books": {Only: []string{"Title"}},
}
bytes, _ := jsoner.Jsoner(&john, options)
excpt := `{"Address":{"city":"NY city","no":13,"state":{"county":"NY"},"street":"ST. 23"},"IsMariage":"false","Skills":["Web dev","System programming","IOT"],"author_books":[{"Title":"Basics Of C"},{"Title":"Golang"}],"name":"John"}`
if excpt != string(bytes) {
t.Log(`want: ` + excpt)
t.Log(`get: ` + string(bytes))
t.Fatal("not excepted result!")
}
}