mirror of https://github.com/goccy/go-json.git
feat: support automatic camel case field key
Add camel case encoding option Add camel case key conversion Add camel case test
This commit is contained in:
parent
23bd66f4c0
commit
c1b23af63a
|
@ -2388,3 +2388,29 @@ func TestIssue324(t *testing.T) {
|
||||||
t.Fatalf("failed to encode. expected %q but got %q", expected, got)
|
t.Fatalf("failed to encode. expected %q but got %q", expected, got)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIssue271(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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
type OptionFlag uint8
|
type OptionFlag uint16
|
||||||
|
|
||||||
const (
|
const (
|
||||||
HTMLEscapeOption OptionFlag = 1 << iota
|
HTMLEscapeOption OptionFlag = 1 << iota
|
||||||
|
@ -16,6 +16,7 @@ const (
|
||||||
ContextOption
|
ContextOption
|
||||||
NormalizeUTF8Option
|
NormalizeUTF8Option
|
||||||
FieldQueryOption
|
FieldQueryOption
|
||||||
|
CamelCaseOption
|
||||||
)
|
)
|
||||||
|
|
||||||
type Option struct {
|
type Option struct {
|
||||||
|
|
|
@ -3,6 +3,7 @@ package vm
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"unicode"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
"github.com/goccy/go-json/internal/encoder"
|
"github.com/goccy/go-json/internal/encoder"
|
||||||
|
@ -184,7 +185,17 @@ func appendStructHead(_ *encoder.RuntimeContext, b []byte) []byte {
|
||||||
return append(b, '{')
|
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...)
|
return append(b, code.Key...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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
|
// 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.
|
// 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.
|
// This option disables this behaviour. You can expect faster speeds by applying this option, but be careful.
|
||||||
|
|
Loading…
Reference in New Issue