json_conv.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package utils
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "log"
  6. "regexp"
  7. "strconv"
  8. "strings"
  9. "unicode"
  10. )
  11. /*************************************** 下划线json ***************************************/
  12. type JsonSnakeCase struct {
  13. Value interface{}
  14. }
  15. func (c JsonSnakeCase) MarshalJSON() ([]byte, error) {
  16. // Regexp definitions
  17. keyMatchRegex := regexp.MustCompile(`\"(\w+)\":`)
  18. wordBarrierRegex := regexp.MustCompile(`(\w)([A-Z])`)
  19. marshalled, err := json.Marshal(c.Value)
  20. converted := keyMatchRegex.ReplaceAllFunc(
  21. marshalled,
  22. func(match []byte) []byte {
  23. return bytes.ToLower(wordBarrierRegex.ReplaceAll(
  24. match,
  25. []byte(`${1}_${2}`),
  26. ))
  27. },
  28. )
  29. return converted, err
  30. }
  31. /*************************************** 驼峰json ***************************************/
  32. type JsonCamelCase struct {
  33. Value interface{}
  34. }
  35. func (c JsonCamelCase) MarshalJSON() ([]byte, error) {
  36. keyMatchRegex := regexp.MustCompile(`\"(\w+)\":`)
  37. marshalled, err := json.Marshal(c.Value)
  38. converted := keyMatchRegex.ReplaceAllFunc(
  39. marshalled,
  40. func(match []byte) []byte {
  41. matchStr := string(match)
  42. key := matchStr[1 : len(matchStr)-2]
  43. resKey := Lcfirst(Case2Camel(key))
  44. return []byte(`"` + resKey + `":`)
  45. },
  46. )
  47. return converted, err
  48. }
  49. /*************************************** 其他方法 ***************************************/
  50. // 驼峰式写法转为下划线写法
  51. func Camel2Case(name string) string {
  52. buffer := NewBuffer()
  53. for i, r := range name {
  54. if unicode.IsUpper(r) {
  55. if i != 0 {
  56. buffer.Append('_')
  57. }
  58. buffer.Append(unicode.ToLower(r))
  59. } else {
  60. buffer.Append(r)
  61. }
  62. }
  63. return buffer.String()
  64. }
  65. // Case2Camel 下划线写法转为驼峰写法
  66. func Case2Camel(name string) string {
  67. name = strings.Replace(name, "_", " ", -1)
  68. name = strings.Title(name)
  69. return strings.Replace(name, " ", "", -1)
  70. }
  71. // Ucfirst first 首字母大写
  72. func Ucfirst(str string) string {
  73. for i, v := range str {
  74. return string(unicode.ToUpper(v)) + str[i+1:]
  75. }
  76. return ""
  77. }
  78. // Lcfirst 首字母小写
  79. func Lcfirst(str string) string {
  80. for i, v := range str {
  81. return string(unicode.ToLower(v)) + str[i+1:]
  82. }
  83. return ""
  84. }
  85. // Buffer 内嵌bytes.Buffer,支持连写
  86. type Buffer struct {
  87. *bytes.Buffer
  88. }
  89. func NewBuffer() *Buffer {
  90. return &Buffer{Buffer: new(bytes.Buffer)}
  91. }
  92. func (b *Buffer) Append(i interface{}) *Buffer {
  93. switch val := i.(type) {
  94. case int:
  95. b.append(strconv.Itoa(val))
  96. case int64:
  97. b.append(strconv.FormatInt(val, 10))
  98. case uint:
  99. b.append(strconv.FormatUint(uint64(val), 10))
  100. case uint64:
  101. b.append(strconv.FormatUint(val, 10))
  102. case string:
  103. b.append(val)
  104. case []byte:
  105. b.Write(val)
  106. case rune:
  107. b.WriteRune(val)
  108. }
  109. return b
  110. }
  111. func (b *Buffer) append(s string) *Buffer {
  112. defer func() {
  113. if err := recover(); err != nil {
  114. log.Println("*****内存不够了!******")
  115. }
  116. }()
  117. b.WriteString(s)
  118. return b
  119. }