Auto decapitalize & snakecase json tools with high-performance 100% compatible drop-in replacement of "encoding/json"
- Decapitalize for each field of struct.
- Snakecase for each field of struct.
type Struct struct {
Field string
FieldAge int
MyAddr string `json:"Addr"`
IPAddr string
ABC string
}
func Test_Capitalize(t *testing.T) {
s := Struct{Field: "field", FieldAge: 10, MyAddr: "earth"}
json := jsoniter.Decapitalized
bs, err := json.Marshal(s)
require.Nil(t, err)
fmt.Println(bs ,err)
var s1 Struct
jsoniter.Unmarshal(bs, &s1)
require.Equal(t, s1.Field, s.Field)
require.Equal(t, s1.MyAddr, s.MyAddr)
fmt.Println(string(bs))
//output: {"field":"field","fieldAge":10,"Addr":"earth" , "ipAddr":"", "abc":""}
}
func Test_Snake(t *testing.T) {
s := Struct{Field: "field", FieldAge: 10, MyAddr: "earth"}
json := jsoniter.SnakeCase
bs, err := json.Marshal(s)
require.Nil(t, err)
require.Equal(t, true, true)
var s1 Struct
json.Unmarshal(bs, &s1)
require.Equal(t, s1.Field, s.Field)
require.Equal(t, s1.MyAddr, s.MyAddr)
fmt.Println(string(bs))
//output: {"field":"field","field_age":10,"Addr":"earth", "ip_addr":"", "abc":""}
}
Raw Result (easyjson requires static code generation)
ns/op | allocation bytes | allocation times | |
---|---|---|---|
std decode | 35510 ns/op | 1960 B/op | 99 allocs/op |
easyjson decode | 8499 ns/op | 160 B/op | 4 allocs/op |
jsoniter decode | 5623 ns/op | 160 B/op | 3 allocs/op |
std encode | 2213 ns/op | 712 B/op | 5 allocs/op |
easyjson encode | 883 ns/op | 576 B/op | 3 allocs/op |
jsoniter encode | 837 ns/op | 384 B/op | 4 allocs/op |
Always benchmark with your own workload. The result depends heavily on the data input.
100% compatibility with standard lib
Replace
import "encoding/json"
json.Marshal(&data)
with
import "github.com/daqiancode/jsoniter"
var json = jsoniter.ConfigCompatibleWithStandardLibrary
json.Marshal(&data)
Replace
import "encoding/json"
json.Unmarshal(input, &data)
with
import "github.com/daqiancode/jsoniter"
var json = jsoniter.ConfigCompatibleWithStandardLibrary
json.Unmarshal(input, &data)
go get github.com/daqiancode/jsoniter
Contributors
Report issue or pull request, or email taowen@gmail.com, or