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

Support automatic camel case field key #371

Open
wants to merge 4 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
26 changes: 26 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2392,6 +2392,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
Expand Down
3 changes: 2 additions & 1 deletion internal/encoder/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"io"
)

type OptionFlag uint8
type OptionFlag uint16

const (
HTMLEscapeOption OptionFlag = 1 << iota
Expand All @@ -16,6 +16,7 @@ const (
ContextOption
NormalizeUTF8Option
FieldQueryOption
CamelCaseOption
)

type Option struct {
Expand Down
13 changes: 12 additions & 1 deletion internal/encoder/vm/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package vm
import (
"encoding/json"
"fmt"
"unicode"
"unsafe"

"github.com/goccy/go-json/internal/encoder"
Expand Down Expand Up @@ -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...)
}

Expand Down
7 changes: 7 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down