1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-18 00:22:41 +08:00

json相关

This commit is contained in:
2022-03-27 10:21:16 +08:00
parent 12460b06ad
commit 22f41aeeeb
4 changed files with 220 additions and 0 deletions

14
json/json.go Normal file
View File

@ -0,0 +1,14 @@
//go:build !jsoniter
// +build !jsoniter
package json
import "encoding/json"
var (
Marshal = json.Marshal
Unmarshal = json.Unmarshal
MarshalIndent = json.MarshalIndent
NewDecoder = json.NewDecoder
NewEncoder = json.NewEncoder
)

137
json/jsonconv.go Normal file
View File

@ -0,0 +1,137 @@
package json
import (
"bytes"
"log"
"regexp"
"strconv"
"strings"
"unicode"
"github.com/charlienet/go-mixed/bytesconv"
)
// 下划线
type SnakeCase struct {
Value interface{}
}
func (c SnakeCase) MarshalJSON() ([]byte, error) {
var keyMatchRegex = regexp.MustCompile(`\"(\w+)\":`)
var wordBarrierRegex = regexp.MustCompile(`(\w)([A-Z])`)
marshalled, err := Marshal(c.Value)
converted := keyMatchRegex.ReplaceAllFunc(
marshalled,
func(match []byte) []byte {
return bytes.ToLower(wordBarrierRegex.ReplaceAll(
match,
bytesconv.StringToBytes(`${1}_${2}`),
))
},
)
return converted, err
}
// 驼峰
type CamelCase struct {
Value interface{}
}
func (c CamelCase) MarshalJSON() ([]byte, error) {
var keyMatchRegex = regexp.MustCompile(`\"(\w+)\":`)
marshalled, err := Marshal(c.Value)
converted := keyMatchRegex.ReplaceAllFunc(
marshalled,
func(match []byte) []byte {
matchStr := bytesconv.BytesToString(match)
key := matchStr[1 : len(matchStr)-2]
resKey := Lcfirst(Case2Camel(key))
return bytesconv.StringToBytes(`"` + resKey + `":`)
},
)
return converted, err
}
// 驼峰式写法转为下划线写法
func Camel2Case(name string) string {
buffer := NewBuffer()
for i, r := range name {
if unicode.IsUpper(r) {
if i != 0 {
buffer.Append('_')
}
buffer.Append(unicode.ToLower(r))
} else {
buffer.Append(r)
}
}
return buffer.String()
}
// 下划线写法转为驼峰写法
func Case2Camel(name string) string {
name = strings.Replace(name, "_", " ", -1)
// name = strings.Title(name)
return strings.Replace(name, " ", "", -1)
}
func Case2CamelBytes(name []byte) []byte {
ret := Case2Camel(string(name))
return bytesconv.StringToBytes(ret)
}
// 首字母大写
func Ucfirst(str string) string {
for i, v := range str {
return string(unicode.ToUpper(v)) + str[i+1:]
}
return ""
}
// 首字母小写
func Lcfirst(str string) string {
for i, v := range str {
return string(unicode.ToLower(v)) + str[i+1:]
}
return ""
}
// 内嵌bytes.Buffer支持连写
type Buffer struct {
*bytes.Buffer
}
func NewBuffer() *Buffer {
return &Buffer{Buffer: new(bytes.Buffer)}
}
func (b *Buffer) Append(i interface{}) *Buffer {
switch val := i.(type) {
case int:
b.append(strconv.Itoa(val))
case int64:
b.append(strconv.FormatInt(val, 10))
case uint:
b.append(strconv.FormatUint(uint64(val), 10))
case uint64:
b.append(strconv.FormatUint(val, 10))
case string:
b.append(val)
case []byte:
b.Write(val)
case rune:
b.WriteRune(val)
}
return b
}
func (b *Buffer) append(s string) *Buffer {
defer func() {
if err := recover(); err != nil {
log.Println("*****内存不够了!******")
}
}()
b.WriteString(s)
return b
}

15
json/jsoniter.go Normal file
View File

@ -0,0 +1,15 @@
//go:build jsoniter
// +build jsoniter
package json
import jsoniter "github.com/json-iterator/go"
var (
json = jsoniter.ConfigCompatibleWithStandardLibrary
Marshal = json.Marshal
Unmarshal = json.Unmarshal
MarshalIndent = json.MarshalIndent
NewDecoder = json.NewDecoder
NewEncoder = json.NewEncoder
)

54
json/structConvert.go Normal file
View File

@ -0,0 +1,54 @@
package json
import (
"reflect"
"github.com/charlienet/go-mixed/bytesconv"
)
// 结构转换为json字符串
func StructToJsonIndent(obj interface{}) string {
b, _ := MarshalIndent(obj, "", " ")
return bytesconv.BytesToString(b)
}
// 结构转换为json字符串
func StructToJson(obj interface{}) string {
b, _ := Marshal(obj)
return bytesconv.BytesToString(b)
}
func StructToMap(obj interface{}) map[string]interface{} {
typ := reflect.TypeOf(obj)
kind := typ.Kind()
if kind == reflect.Map {
return toMap(obj)
}
val := reflect.ValueOf(obj)
m := make(map[string]interface{})
for i := 0; i < val.NumField(); i++ {
m[typ.Field(i).Name] = val.Field(i).Interface()
}
return m
}
func StructToMapViaJson(obj interface{}) map[string]interface{} {
m := make(map[string]interface{})
j, _ := Marshal(obj)
_ = Unmarshal(j, &m)
return m
}
func toMap(obj interface{}) map[string]interface{} {
if h, ok := obj.(map[string]interface{}); ok {
return h
}
return StructToMapViaJson(obj)
}