Categorygithub.com/daqiancode/jsoniter
repositorypackage
0.0.0-20230302100208-d74e3c50e1fc
Repository: https://github.com/daqiancode/jsoniter.git
Documentation: pkg.go.dev

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# README

Sourcegraph GoDoc Build Status codecov rcard License Gitter chat

Auto decapitalize & snakecase json tools with high-performance 100% compatible drop-in replacement of "encoding/json"

New features:

  1. Decapitalize for each field of struct.
  2. Snakecase for each field of struct.

New features: Decapitalize & Snakecase

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":""}
}

Benchmark

benchmark

Source code: https://github.com/daqiancode/jsoniter-benchmark/blob/master/src/github.com/daqiancode/jsoniter-benchmark/benchmark_medium_payload_test.go

Raw Result (easyjson requires static code generation)

ns/opallocation bytesallocation times
std decode35510 ns/op1960 B/op99 allocs/op
easyjson decode8499 ns/op160 B/op4 allocs/op
jsoniter decode5623 ns/op160 B/op3 allocs/op
std encode2213 ns/op712 B/op5 allocs/op
easyjson encode883 ns/op576 B/op3 allocs/op
jsoniter encode837 ns/op384 B/op4 allocs/op

Always benchmark with your own workload. The result depends heavily on the data input.

Usage

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)

More documentation

How to get

go get github.com/daqiancode/jsoniter

Contribution Welcomed !

Contributors

Report issue or pull request, or email [email protected], or Gitter chat