File: jsonhooks_test.go

package info (click to toggle)
golang-github-akamai-akamaiopen-edgegrid-golang 1.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,408 kB
  • sloc: sh: 532; makefile: 3
file content (210 lines) | stat: -rw-r--r-- 4,300 bytes parent folder | download
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package jsonhooks

import (
	"encoding/json"
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
)

type Optionals struct {
	Sr string `json:"sr"`
	So string `json:"so,omitempty"`
	Sw string `json:"-"`

	Ir int `json:"omitempty"` // actually named omitempty, not an option
	Io int `json:"io,omitempty"`

	Slr []string `json:"slr,random"`
	Slo []string `json:"slo,omitempty"`

	Mr map[string]interface{} `json:"mr"`
	Mo map[string]interface{} `json:",omitempty"`

	Fr float64 `json:"fr"`
	Fo float64 `json:"fo,omitempty"`

	Br bool `json:"br"`
	Bo bool `json:"bo,omitempty"`

	Ur uint `json:"ur"`
	Uo uint `json:"uo,omitempty"`

	Str struct{} `json:"str"`
	Sto struct{} `json:"sto,omitempty"`
}

type MixedTypes struct {
	I  int     `json:"I"`
	B  bool    `json:"B"`
	F  float64 `json:"F"`
	S  string  `json:"S"`
	St struct {
		Foo int    `json:"Foo"`
		Bar string `json:"Bar"`
	} `json:"St"`
	A []string `json:"A"`
}

type WithHooks MixedTypes

func (hooks *WithHooks) PreMarshalJSON() error {
	hooks.I *= 1000
	hooks.B = !hooks.B
	hooks.F *= 1.1
	hooks.S = strings.ToUpper(hooks.S)
	hooks.St.Foo *= 2000
	hooks.St.Bar = strings.ToUpper(hooks.St.Bar)
	for key, val := range hooks.A {
		hooks.A[key] = strings.ToUpper(val)
	}

	return nil
}

func (hooks *WithHooks) PostUnmarshalJSON() error {
	hooks.I /= 1000
	hooks.B = !hooks.B
	hooks.F /= 1.1
	hooks.S = strings.ToLower(hooks.S)
	hooks.St.Foo /= 2000
	hooks.St.Bar = strings.ToLower(hooks.St.Bar)
	for key, val := range hooks.A {
		hooks.A[key] = strings.ToLower(val)
	}

	return nil
}

func TestMarshalCompat(t *testing.T) {
	var o Optionals
	o.Sw = "something"
	o.Mr = map[string]interface{}{}
	o.Mo = map[string]interface{}{}

	expected, _ := json.Marshal(o)
	actual, err := Marshal(o)

	assert.NoError(t, err)
	assert.Equal(
		t,
		expected,
		actual,
	)
}

func TestUnmarshalCompat(t *testing.T) {
	data := `{
		"sr": "",
				"omitempty": 0,
				"slr": null,
				"mr": {},
		"fr": 0,
				"br": false,
				"ur": 0,
				"str": {},
		"sto": {}
	}`

	expected := &Optionals{}
	_ = json.Unmarshal([]byte(data), expected)

	actual := &Optionals{}
	err := Unmarshal([]byte(data), actual)

	assert.NoError(t, err)
	assert.Equal(
		t,
		expected,
		actual,
	)
}

func TestImplementsPreJSONMarshaler(t *testing.T) {
	noHooks := &MixedTypes{}
	withHooks := &WithHooks{}

	assert.False(t, ImplementsPreJSONMarshaler(noHooks))
	assert.True(t, ImplementsPreJSONMarshaler(withHooks))
}

func TestImplementsPostJSONUnmarshaler(t *testing.T) {
	noHooks := &MixedTypes{}
	withHooks := &WithHooks{}

	assert.False(t, ImplementsPostJSONUnmarshaler(noHooks))
	assert.True(t, ImplementsPostJSONUnmarshaler(withHooks))
}

func TestPreJSONMarshal(t *testing.T) {
	noHooks := &MixedTypes{
		I: 1,
		B: true,
		F: 1.0,
		S: "testing",
		St: struct {
			Foo int    `json:"Foo"`
			Bar string `json:"Bar"`
		}{2, "test"},
		A: []string{"one", "two", "three"},
	}

	withHooks := (*WithHooks)(noHooks)

	expected, _ := json.Marshal(&MixedTypes{
		I: 1 * 1000,
		B: !true,
		F: 1.0 * 1.1,
		S: "TESTING",
		St: struct {
			Foo int    `json:"Foo"`
			Bar string `json:"Bar"`
		}{2 * 2000, "TEST"},
		A: []string{"ONE", "TWO", "THREE"},
	})

	gojson, _ := json.Marshal(withHooks)
	actualNoHooks, _ := Marshal(noHooks)
	actualWithHooks, err := Marshal(withHooks)

	assert.NoError(t, err)
	assert.Equal(t, string(gojson), string(actualNoHooks))
	assert.NotEqual(t, string(gojson), string(actualWithHooks))
	assert.NotEqual(t, string(actualNoHooks), string(actualWithHooks))
	assert.Equal(t, string(expected), string(actualWithHooks))
}

func TestPostJSONUnmarshal(t *testing.T) {
	expected := &WithHooks{
		I: 1,
		B: true,
		F: 1.0,
		S: "testing",
		St: struct {
			Foo int    `json:"Foo"`
			Bar string `json:"Bar"`
		}{2, "test"},
		A: []string{"one", "two", "three"},
	}

	data := []byte(`{"I":1000,"B":false,"F":1.1,"S":"TESTING","St":{"Foo":4000,"Bar":"TEST"},"A":["ONE","TWO","THREE"]}`)

	gojson := &MixedTypes{}
	_ = json.Unmarshal(
		data,
		gojson,
	)

	withHooks := &WithHooks{}
	err := Unmarshal(data, withHooks)
	assert.NoError(t, err)

	withoutHooks := &MixedTypes{}
	err = Unmarshal(data, withoutHooks)

	assert.NoError(t, err)
	assert.NotEqual(t, gojson, withHooks)
	assert.NotEqual(t, expected, withoutHooks)
	assert.Equal(t, expected, withHooks)
}