| 1 |
// Copyright (c) 2026, Maxime Soulé |
| 2 |
// All rights reserved. |
| 3 |
// |
| 4 |
// This source code is licensed under the BSD-style license found in the |
| 5 |
// LICENSE file in the root directory of this source tree. |
| 6 |
|
| 7 |
//go:build go1.27 |
| 8 |
// +build go1.27 |
| 9 |
|
| 10 |
package td_test |
| 11 |
|
| 12 |
import ( |
| 13 |
jsonv1 "encoding/json" |
| 14 |
"encoding/json/v2" |
| 15 |
"fmt" |
| 16 |
"strconv" |
| 17 |
"testing" |
| 18 |
|
| 19 |
"github.com/maxatome/go-testdeep/internal/test" |
| 20 |
"github.com/maxatome/go-testdeep/td" |
| 21 |
) |
| 22 |
|
| 23 |
func TestJSONv2(t *testing.T) { |
| 24 |
type MyStruct struct { |
| 25 |
Name string `json:"name"` |
| 26 |
Age uint `json:"age"` |
| 27 |
} |
| 28 |
|
| 29 |
got := MyStruct{Name: "Bob", Age: 42} |
| 30 |
|
| 31 |
checkOK(t, got, |
| 32 |
td.JSON(`{"name":$2,"age":"🕯42🕯"}`, |
| 33 |
json.WithMarshalers(json.MarshalFunc(func(n uint) ([]byte, error) { |
| 34 |
return fmt.Appendf(nil, `"🕯%d🕯"`, n), nil |
| 35 |
})), |
| 36 |
"Bob")) |
| 37 |
|
| 38 |
checkOK(t, got, |
| 39 |
td.JSON(`{"name":$1,"age":"🕯42🕯"}`, |
| 40 |
"Bob", |
| 41 |
json.WithMarshalers(json.MarshalFunc(func(n uint) ([]byte, error) { |
| 42 |
return fmt.Appendf(nil, `"🕯%d🕯"`, n), nil |
| 43 |
})))) |
| 44 |
|
| 45 |
checkOK(t, got, |
| 46 |
td.JSON(`{"name":$1,"age":$4}`, |
| 47 |
"Bob", |
| 48 |
json.WithMarshalers(json.MarshalFunc(func(n uint) ([]byte, error) { |
| 49 |
return fmt.Appendf(nil, `"%d"`, n), nil |
| 50 |
})), |
| 51 |
json.WithUnmarshalers(json.UnmarshalFunc(func(b []byte, v *int) error { |
| 52 |
_, err := fmt.Sscanf(string(b), `"%d"`, v) |
| 53 |
return err |
| 54 |
})), |
| 55 |
42)) |
| 56 |
|
| 57 |
// Pass jsonv2.Options to JSONPointer operator |
| 58 |
checkOK(t, map[string]any{"a": map[string]any{"b": map[string]any{"c": 42}}}, |
| 59 |
td.JSON(`{"a": JSONPointer("/b/c", 42)}`, |
| 60 |
json.WithUnmarshalers(json.UnmarshalFunc(func(b []byte, v *float64) error { |
| 61 |
var err error |
| 62 |
if b[0] != '"' { |
| 63 |
// Used by JSON to convert original `42` to a float64 |
| 64 |
*v, err = strconv.ParseFloat(string(b), 64) |
| 65 |
} else { |
| 66 |
// Used by JSONPointer to convert `"🕯42🕯"` to the expected float64 |
| 67 |
_, err = fmt.Sscanf(string(b), `"🕯%f🕯"`, v) |
| 68 |
} |
| 69 |
return err |
| 70 |
})), |
| 71 |
json.WithMarshalers(json.MarshalFunc(func(n float64) ([]byte, error) { |
| 72 |
// Used by JSON to convert float64(42) to JSON string `"🕯42🕯"` |
| 73 |
return fmt.Appendf(nil, `"🕯%.0f🕯"`, n), nil |
| 74 |
})), |
| 75 |
)) |
| 76 |
|
| 77 |
// With json/v2 unmarshaling can fail |
| 78 |
checkError(t, got, |
| 79 |
td.JSON(`{"name":"Bob","age":42}`, |
| 80 |
json.WithUnmarshalers(json.UnmarshalFunc(func(b []byte, v *float64) error { |
| 81 |
return fmt.Errorf("an error occurred") |
| 82 |
}))), |
| 83 |
expectedError{ |
| 84 |
Message: mustBe("json.Unmarshal failed"), |
| 85 |
Summary: mustContain(": an error occurred"), |
| 86 |
Under: mustContain("under operator JSON at td_json_127_test.go:"), |
| 87 |
}) |
| 88 |
|
| 89 |
test.EqualStr(t, |
| 90 |
td.JSON(`{"x":123}`, json.DefaultOptionsV2()).String(), |
| 91 |
`JSON({ |
| 92 |
"x": 123 |
| 93 |
})`) |
| 94 |
} |
| 95 |
|
| 96 |
func ExampleJSON_jsonv2() { |
| 97 |
t := &testing.T{} |
| 98 |
|
| 99 |
got := &struct { |
| 100 |
Fullname string `json:"fullname"` |
| 101 |
Age int `json:"age"` |
| 102 |
}{ |
| 103 |
Fullname: "Bob", |
| 104 |
Age: 42, |
| 105 |
} |
| 106 |
|
| 107 |
// Same as default |
| 108 |
ok := td.Cmp(t, got, td.JSON(`{"age":42,"fullname":"Bob"}`, jsonv1.DefaultOptionsV1())) |
| 109 |
fmt.Println("using json/v1:", ok, "(same as default)") |
| 110 |
|
| 111 |
ok = td.Cmp(t, got, td.JSON(`{"age":42,"fullname":"Bob"}`, json.DefaultOptionsV2())) |
| 112 |
fmt.Println("using json/v2:", ok) |
| 113 |
|
| 114 |
ok = td.Cmp(t, got, |
| 115 |
td.JSON(`{"fullname":$1,"age":"🕯42🕯"}`, |
| 116 |
"Bob", |
| 117 |
json.WithMarshalers(json.MarshalFunc(func(n int) ([]byte, error) { |
| 118 |
return fmt.Appendf(nil, `"🕯%d🕯"`, n), nil |
| 119 |
}))), |
| 120 |
) |
| 121 |
fmt.Println("with custom marshaler:", ok) |
| 122 |
|
| 123 |
ok = td.Cmp(t, got, |
| 124 |
td.JSON(`{"fullname":$1,"age":$4}`, |
| 125 |
"Bob", |
| 126 |
json.WithMarshalers(json.MarshalFunc(func(n int) ([]byte, error) { |
| 127 |
return fmt.Appendf(nil, `"%d"`, n), nil |
| 128 |
})), |
| 129 |
json.WithUnmarshalers(json.UnmarshalFunc(func(b []byte, v *int) error { |
| 130 |
_, err := fmt.Sscanf(string(b), `"%d"`, v) |
| 131 |
return err |
| 132 |
})), |
| 133 |
42), |
| 134 |
) |
| 135 |
fmt.Println("with custom marshaler & unmarshaler:", ok) |
| 136 |
|
| 137 |
// Output: |
| 138 |
// using json/v1: true (same as default) |
| 139 |
// using json/v2: true |
| 140 |
// with custom marshaler: true |
| 141 |
// with custom marshaler & unmarshaler: true |
| 142 |
} |