-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_test.go
61 lines (55 loc) · 1.05 KB
/
json_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 qweather
import (
"bytes"
stdjson "encoding/json"
"strings"
"testing"
"github.com/Equationzhao/qweather-go/internal/json"
)
func _myMarshal(v any) ([]byte, error) {
bb := bytes.Buffer{}
m, err := stdjson.Marshal(v)
if err != nil {
return nil, err
}
bb.WriteString("my Marshal")
bb.Write(m)
return bb.Bytes(), nil
}
func _myUnmarshal(b []byte, v any) error {
b, _ = bytes.CutPrefix(b, []byte("my Marshal"))
return stdjson.Unmarshal(b, v)
}
func TestSetJsonMarshal(t *testing.T) {
SetJsonMarshal(_myMarshal)
v := struct {
A string `json:"a"`
}{
A: "test",
}
res, err := json.Marshal(v)
if err != nil {
t.Fatal(err)
}
if strings.HasPrefix(string(res), "my Marshal") {
t.Log("success")
} else {
t.Fatal("fatal")
}
}
func TestSetJsonUnmarshal(t *testing.T) {
SetJsonUnmarshal(_myUnmarshal)
jsonString := "my Marshal{\"a\":\"test\"}"
v := struct {
A string `json:"a"`
}{}
err := json.Unmarshal([]byte(jsonString), &v)
if err != nil {
t.Fatal(err)
}
if v.A == "test" {
t.Log("success")
} else {
t.Fatal(v)
}
}