| 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 |
"testing" |
| 17 |
|
| 18 |
"github.com/maxatome/go-testdeep/td" |
| 19 |
) |
| 20 |
|
| 21 |
func TestJSONPointerv2(t *testing.T) { |
| 22 |
checkOK(t, map[string]any{"a": map[string]any{"b": map[string]any{"c": 42}}}, |
| 23 |
td.JSONPointer("/a/b/c", 42, json.DefaultOptionsV2())) |
| 24 |
|
| 25 |
checkOK(t, map[string]any{"a": map[string]any{"b": map[string]any{"c": "42"}}}, |
| 26 |
td.JSONPointer("/a/b/c", int8(42), |
| 27 |
json.DefaultOptionsV2(), |
| 28 |
json.WithUnmarshalers(json.UnmarshalFunc(func(b []byte, v *int8) error { |
| 29 |
_, err := fmt.Sscanf(string(b), `"%d"`, v) |
| 30 |
return err |
| 31 |
})), |
| 32 |
)) |
| 33 |
} |
| 34 |
|
| 35 |
func ExampleJSONPointer_jsonv2() { |
| 36 |
t := &testing.T{} |
| 37 |
|
| 38 |
got := jsonv1.RawMessage(`{"a": {"b": {"c": "42"}}}`) |
| 39 |
|
| 40 |
ok := td.Cmp(t, got, td.JSONPointer("/a/b/c", "42", jsonv1.DefaultOptionsV1())) |
| 41 |
fmt.Println("using json/v1:", ok, "(same as default)") |
| 42 |
|
| 43 |
ok = td.Cmp(t, got, td.JSONPointer("/a/b/c", "42", json.DefaultOptionsV2())) |
| 44 |
fmt.Println("using json/v2:", ok) |
| 45 |
|
| 46 |
ok = td.Cmp(t, got, |
| 47 |
td.JSONPointer("/a/b/c", int8(42), |
| 48 |
json.DefaultOptionsV2(), |
| 49 |
json.WithUnmarshalers(json.UnmarshalFunc(func(b []byte, v *int8) error { |
| 50 |
_, err := fmt.Sscanf(string(b), `"%d"`, v) |
| 51 |
return err |
| 52 |
})), |
| 53 |
)) |
| 54 |
fmt.Println("multiple json/v2.Options + custom unmarshaler:", ok) |
| 55 |
|
| 56 |
// Output: |
| 57 |
// using json/v1: true (same as default) |
| 58 |
// using json/v2: true |
| 59 |
// multiple json/v2.Options + custom unmarshaler: true |
| 60 |
} |