|
- package utils
-
- import (
- "bytes"
- "encoding/json"
- "log"
- "regexp"
- "strconv"
- "strings"
- "unicode"
- )
-
- func JsonMarshal(interface{}) {
-
- }
-
- // 不科学计数法
- func JsonDecode(data []byte, v interface{}) error {
- d := json.NewDecoder(bytes.NewReader(data))
- d.UseNumber()
- return d.Decode(v)
- }
-
- /*************************************** 下划线json ***************************************/
- type JsonSnakeCase struct {
- Value interface{}
- }
-
- func (c JsonSnakeCase) MarshalJSON() ([]byte, error) {
- // Regexp definitions
- var keyMatchRegex = regexp.MustCompile(`\"(\w+)\":`)
- var wordBarrierRegex = regexp.MustCompile(`(\w)([A-Z])`)
- marshalled, err := json.Marshal(c.Value)
- converted := keyMatchRegex.ReplaceAllFunc(
- marshalled,
- func(match []byte) []byte {
- return bytes.ToLower(wordBarrierRegex.ReplaceAll(
- match,
- []byte(`${1}_${2}`),
- ))
- },
- )
- return converted, err
- }
-
- /*************************************** 驼峰json ***************************************/
- type JsonCamelCase struct {
- Value interface{}
- }
-
- func (c JsonCamelCase) 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(Case2Camel(key))
- return []byte(`"` + 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 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
- }
-
- // json字符串驼峰命名格式 转为 下划线命名格式
- // c :json字符串
- func MarshalJSONCamelCase2JsonSnakeCase(c string) []byte {
- // Regexp definitions
- var keyMatchRegex = regexp.MustCompile(`\"(\w+)\":`)
- var wordBarrierRegex = regexp.MustCompile(`(\w)([A-Z])`)
- marshalled := []byte(c)
- converted := keyMatchRegex.ReplaceAllFunc(
- marshalled,
- func(match []byte) []byte {
- return bytes.ToLower(wordBarrierRegex.ReplaceAll(
- match,
- []byte(`${1}_${2}`),
- ))
- },
- )
- return converted
- }
|