@@ -2,6 +2,7 @@ package test
22
33import (
44 "bytes"
5+ "fmt"
56 "github.com/json-iterator/go"
67 "github.com/stretchr/testify/require"
78 "strconv"
@@ -47,6 +48,38 @@ func Test_customize_byte_array_encoder(t *testing.T) {
4748 should .Equal (`"abc"` , str )
4849}
4950
51+ type CustomEncoderAttachmentTestStruct struct {
52+ Value int32 `json:"value"`
53+ }
54+
55+ type CustomEncoderAttachmentTestStructEncoder struct {}
56+
57+ func (c * CustomEncoderAttachmentTestStructEncoder ) Encode (ptr unsafe.Pointer , stream * jsoniter.Stream ) {
58+ attachVal , ok := stream .Attachment .(int )
59+ stream .WriteRaw (`"` )
60+ stream .WriteRaw (fmt .Sprintf ("%t %d" , ok , attachVal ))
61+ stream .WriteRaw (`"` )
62+ }
63+
64+ func (c * CustomEncoderAttachmentTestStructEncoder ) IsEmpty (ptr unsafe.Pointer ) bool {
65+ return false
66+ }
67+
68+ func Test_custom_encoder_attachment (t * testing.T ) {
69+
70+ jsoniter .RegisterTypeEncoder ("test.CustomEncoderAttachmentTestStruct" , & CustomEncoderAttachmentTestStructEncoder {})
71+ expectedValue := 17
72+ should := require .New (t )
73+ buf := & bytes.Buffer {}
74+ stream := jsoniter .NewStream (jsoniter.Config {SortMapKeys : true }.Froze (), buf , 4096 )
75+ stream .Attachment = expectedValue
76+ val := map [string ]CustomEncoderAttachmentTestStruct {"a" : {}}
77+ stream .WriteVal (val )
78+ stream .Flush ()
79+ should .Nil (stream .Error )
80+ should .Equal ("{\" a\" :\" true 17\" }" , buf .String ())
81+ }
82+
5083func Test_customize_field_decoder (t * testing.T ) {
5184 type Tom struct {
5285 field1 string
0 commit comments