golang 的 rabbitmq 消费项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

json.go 3.4 KiB

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