Compare commits

...

7 Commits

Author SHA1 Message Date
Erik Pellizzon 22aa2b4717
Merge d8d97b99b4 into 3e9769d637 2024-11-22 06:54:28 +00:00
Masaaki Goshima 3e9769d637
Update go.yml 2024-11-11 12:13:39 +09:00
Andrey Grazhdankov 65c8b28ca1
Fix encode []*time.Time - check nil (#524) 2024-11-11 11:10:21 +09:00
Erik Pellizzon d8d97b99b4
Update encode_test.go 2022-07-18 12:06:04 +02:00
Erik Pellizzon 1e3fdb0e3d
Merge branch 'master' into feat-case-271 2022-07-18 12:04:20 +02:00
Erik Pellizzon be494e932a
Update encode_test.go
Co-authored-by: Fulvio <fulviodenza823@gmail.com>
2022-06-02 15:50:10 +02:00
ErikPelli c1b23af63a
feat: support automatic camel case field key
Add camel case encoding option
Add camel case key conversion
Add camel case test
2022-05-25 16:36:37 +02:00
6 changed files with 58 additions and 3 deletions

View File

@ -12,7 +12,7 @@ jobs:
- name: checkout
uses: actions/checkout@v3
- name: build
run: docker-compose run go-json
run: docker compose run go-json
test:
name: Test

View File

@ -426,6 +426,11 @@ func Test_Marshal(t *testing.T) {
assertErr(t, err)
assertEq(t, "[]interface{}", `[1,2.1,"hello"]`, string(bytes))
})
t.Run("[]*time.Time", func(t *testing.T) {
bytes, err := json.Marshal([]*time.Time{nil})
assertErr(t, err)
assertEq(t, "[]*time.Time", `[null]`, string(bytes))
})
})
t.Run("array", func(t *testing.T) {
@ -2401,6 +2406,32 @@ func TestIssue324(t *testing.T) {
}
}
func TestCamelCaseField(t *testing.T) {
type T struct {
FieldA bool
}
type T2 struct {
FieldA bool `json:"fieldA"`
}
v := T{FieldA: true}
got, err := json.MarshalWithOption(v, json.EnableCamelCase())
if err != nil {
t.Fatal(err)
}
v2 := T2{FieldA: true}
expected, err := json.Marshal(v2)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(expected, got) {
t.Fatalf("failed to encode. expected %q but got %q", expected, got)
}
}
func TestIssue339(t *testing.T) {
type T1 struct {
*big.Int

View File

@ -406,6 +406,11 @@ func AppendMarshalJSON(ctx *RuntimeContext, code *Opcode, b []byte, v interface{
rv = newV
}
}
if rv.Kind() == reflect.Ptr && rv.IsNil() {
return AppendNull(ctx, b), nil
}
v = rv.Interface()
var bb []byte
if (code.Flags & MarshalerContextFlags) != 0 {

View File

@ -5,7 +5,7 @@ import (
"io"
)
type OptionFlag uint8
type OptionFlag uint16
const (
HTMLEscapeOption OptionFlag = 1 << iota
@ -16,6 +16,7 @@ const (
ContextOption
NormalizeUTF8Option
FieldQueryOption
CamelCaseOption
)
type Option struct {

View File

@ -3,6 +3,7 @@ package vm
import (
"encoding/json"
"fmt"
"unicode"
"unsafe"
"github.com/goccy/go-json/internal/encoder"
@ -184,7 +185,17 @@ func appendStructHead(_ *encoder.RuntimeContext, b []byte) []byte {
return append(b, '{')
}
func appendStructKey(_ *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte {
func appendStructKey(e *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte {
if e.Option.Flag&encoder.CamelCaseOption > 0 {
key := []rune(code.Key)
for i := range key {
if unicode.IsLetter(key[i]) {
key[i] = unicode.ToLower(key[i])
break
}
}
return append(b, string(key)...)
}
return append(b, code.Key...)
}

View File

@ -24,6 +24,13 @@ func DisableHTMLEscape() EncodeOptionFunc {
}
}
// EnableCamelCase convert the keys to camel case when encoding a struct.
func EnableCamelCase() EncodeOptionFunc {
return func(opt *EncodeOption) {
opt.Flag |= encoder.CamelCaseOption
}
}
// DisableNormalizeUTF8
// By default, when encoding string, UTF8 characters in the range of 0x80 - 0xFF are processed by applying \ufffd for invalid code and escaping for \u2028 and \u2029.
// This option disables this behaviour. You can expect faster speeds by applying this option, but be careful.