2 Star 0 Fork 0

403716045/gcore

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
JsonSnakeCase.go 664 Bytes
一键复制 编辑 原始数据 按行查看 历史
xingang 提交于 2023-02-17 18:19 +08:00 . init
package helper
import (
"encoding/json"
"regexp"
"unicode"
)
type JsonSnakeCase struct {
Value interface{}
}
func (c JsonSnakeCase) MarshalJSON() ([]byte, error) {
var keyMatchRegex = regexp.MustCompile(`\"(\w+)\":`)
marshalled, err := json.Marshal(c.Value)
converted := keyMatchRegex.ReplaceAllFunc(
marshalled,
func(match []byte) []byte {
matchStr := string(match)
key := matchStr[1 : len(matchStr)-2]
resKey := Lcfirst(key)
return []byte(`"` + resKey + `":`)
},
)
return converted, err
}
// 首字母小写
func Lcfirst(str string) string {
for i, v := range str {
return string(unicode.ToLower(v)) + str[i+1:]
}
return ""
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/lv_baobao/gcore.git
git@gitee.com:lv_baobao/gcore.git
lv_baobao
gcore
gcore
3bf2efb9b087

搜索帮助