Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ,string option applying to non applicable value / types #660

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 17 additions & 6 deletions reflect_extension.go
Expand Up @@ -2,12 +2,13 @@ package jsoniter

import (
"fmt"
"github.com/modern-go/reflect2"
"reflect"
"sort"
"strings"
"unicode"
"unsafe"

"github.com/modern-go/reflect2"
)

var typeDecoders = map[string]ValDecoder{}
Expand Down Expand Up @@ -448,12 +449,22 @@ func processTags(structDescriptor *StructDescriptor, cfg *frozenConfig) {
if tagPart == "omitempty" {
shouldOmitEmpty = true
} else if tagPart == "string" {
if binding.Field.Type().Kind() == reflect.String {
binding.Decoder = &stringModeStringDecoder{binding.Decoder, cfg}
binding.Encoder = &stringModeStringEncoder{binding.Encoder, cfg}
} else {
fieldType := binding.Field.Type()
isPointer := false
if fieldType.Kind() == reflect.Pointer {
isPointer = true
fieldType = reflect2.Type2(fieldType.Type1().Elem())
}
switch fieldType.Kind() {
case reflect.String:
binding.Decoder = &stringModeStringDecoder{isPointer, binding.Decoder, cfg}
binding.Encoder = &stringModeStringEncoder{isPointer, binding.Encoder, cfg}
case reflect.Float32, reflect.Float64,
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr,
reflect.Bool:
binding.Decoder = &stringModeNumberDecoder{binding.Decoder}
binding.Encoder = &stringModeNumberEncoder{binding.Encoder}
binding.Encoder = &stringModeNumberEncoder{isPointer, binding.Encoder}
}
}
}
Expand Down
47 changes: 45 additions & 2 deletions reflect_struct_decoder.go
Expand Up @@ -1058,16 +1058,59 @@ func (decoder *structFieldDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
}

type stringModeStringDecoder struct {
isPointer bool // true indicates *string field type.
elemDecoder ValDecoder
cfg *frozenConfig
}

func (decoder *stringModeStringDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
decoder.elemDecoder.Decode(ptr, iter)
str := *((*string)(ptr))

if iter.Error != nil {
return
}

var str string
if decoder.isPointer {
if *((**string)(ptr)) == nil {
return
}
str = **((**string)(ptr))
} else {
str = *((*string)(ptr))
}

if len(str) < 2 {
iter.ReportError(
"stringModeStringDecoder",
`expect len(s) >= 2, but found `+str,
)
return
}
if str[0] != '"' || str[len(str)-1] != '"' {
var idx int
if str[0] == '"' {
idx = len(str) - 1
}
iter.ReportError(
"stringModeStringDecoder",
`expect ", but found `+string(str[idx]),
)
return
}

tempIter := decoder.cfg.BorrowIterator([]byte(str))
defer decoder.cfg.ReturnIterator(tempIter)
*((*string)(ptr)) = tempIter.ReadString()

if decoder.isPointer {
**((**string)(ptr)) = tempIter.ReadString()
} else {
*((*string)(ptr)) = tempIter.ReadString()
}

if tempIter.Error != nil {
iter.Error = tempIter.Error
}
}

type stringModeNumberDecoder struct {
Expand Down
22 changes: 19 additions & 3 deletions reflect_struct_encoder.go
Expand Up @@ -2,10 +2,11 @@ package jsoniter

import (
"fmt"
"github.com/modern-go/reflect2"
"io"
"reflect"
"unsafe"

"github.com/modern-go/reflect2"
)

func encoderOfStruct(ctx *ctx, typ reflect2.Type) ValEncoder {
Expand Down Expand Up @@ -180,25 +181,40 @@ func (encoder *emptyStructEncoder) IsEmpty(ptr unsafe.Pointer) bool {
}

type stringModeNumberEncoder struct {
isPointer bool
elemEncoder ValEncoder
}

func (encoder *stringModeNumberEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
stream.writeByte('"')
shouldQuote := true
if encoder.isPointer && (ptr == nil || *(*unsafe.Pointer)(ptr) == nil) {
shouldQuote = false
}
if shouldQuote {
stream.writeByte('"')
}
encoder.elemEncoder.Encode(ptr, stream)
stream.writeByte('"')
if shouldQuote {
stream.writeByte('"')
}
}

func (encoder *stringModeNumberEncoder) IsEmpty(ptr unsafe.Pointer) bool {
return encoder.elemEncoder.IsEmpty(ptr)
}

type stringModeStringEncoder struct {
isPointer bool
elemEncoder ValEncoder
cfg *frozenConfig
}

func (encoder *stringModeStringEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
if encoder.isPointer && (ptr == nil || *(*unsafe.Pointer)(ptr) == nil) {
encoder.elemEncoder.Encode(ptr, stream)
return
}

tempStream := encoder.cfg.BorrowStream(nil)
tempStream.Attachment = stream.Attachment
defer encoder.cfg.ReturnStream(tempStream)
Expand Down
22 changes: 22 additions & 0 deletions type_tests/struct_tags_test.go
Expand Up @@ -146,6 +146,28 @@ func init() {
(*struct {
Field bool `json:",omitempty,string"`
})(nil),
(*struct {
Str *string `json:",string"`
F32 *float32 `json:",string"`
F64 *float64 `json:",string"`
Int *int `json:",string"`
Uint *uint `json:",string"`
I16 *int16 `json:",string"`
I32 *int32 `json:",string"`
I64 *int64 `json:",string"`
U8 *uint8 `json:",string"`
U16 *uint16 `json:",string"`
U32 *uint32 `json:",string"`
U64 *uint64 `json:",string"`
Uptr *uintptr `json:",string"`
Bool *bool `json:",string"`
})(nil),
(*struct {
Struct struct{ Foo string } `json:",string"`
Arr [2]int `json:",string"`
Slice []string `json:",string"`
Map map[int]int `json:",string"`
})(nil),
(*struct {
Field bool `json:"中文"`
})(nil),
Expand Down
40 changes: 40 additions & 0 deletions value_tests/struct_test.go
Expand Up @@ -105,6 +105,18 @@ func init() {
Asks [][2]float64 `json:"asks"`
})(nil),
input: `{"key_string": "KEYSTRING","type": "TYPE","asks": [[1e+66,1]]}`,
}, unmarshalCase{
ptr: (*quote)(nil),
input: `{"Str":null,"F32":null,"F64":null,"Int":null,"Uint":null,"I16":null,"I32":null,"I64":null,"U8":null,"U16":null,"U32":null,"U64":null,"Uptr":null,"Bool":null}`,
}, unmarshalCase{
ptr: (*quote)(nil),
input: `{"Str":"\"foo\""}`,
}, unmarshalCase{
ptr: (*struct {
AnyStr interface{} `json:",string"`
AnyInt interface{} `json:",string"`
})(nil),
input: `{"AnyStr":"foo","AnyInt":123}`,
})
marshalCases = append(marshalCases,
struct {
Expand Down Expand Up @@ -204,6 +216,14 @@ func init() {
}{
"should not marshal",
},
quote{},
struct {
AnyStr interface{} `json:",string"`
AnyInt interface{} `json:",string"`
}{
AnyStr: "foo",
AnyInt: 123,
},
)
}

Expand Down Expand Up @@ -245,3 +265,23 @@ type structOrder struct {
orderB
Field7 string
}

type quote struct {
// The ,string option applies only to fields of string, floating point, integer,
// or boolean types as per https://pkg.go.dev/encoding/json@go1.20.1.
// It is poorly or not totally documented that json.Marshal does not quote null.
Str *string `json:",string"`
F32 *float32 `json:",string"`
F64 *float64 `json:",string"`
Int *int `json:",string"`
Uint *uint `json:",string"`
I16 *int16 `json:",string"`
I32 *int32 `json:",string"`
I64 *int64 `json:",string"`
U8 *uint8 `json:",string"`
U16 *uint16 `json:",string"`
U32 *uint32 `json:",string"`
U64 *uint64 `json:",string"`
Uptr *uintptr `json:",string"`
Bool *bool `json:",string"`
}