工具包
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
771 B

  1. package utils
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "regexp"
  6. )
  7. func JsonMarshal(interface{}) {
  8. }
  9. // 不科学计数法
  10. func JsonDecode(data []byte, v interface{}) error {
  11. d := json.NewDecoder(bytes.NewReader(data))
  12. d.UseNumber()
  13. return d.Decode(v)
  14. }
  15. // json字符串驼峰命名格式 转为 下划线命名格式
  16. // c :json字符串
  17. func MarshalJSONCamelCase2JsonSnakeCase(c string) []byte {
  18. // Regexp definitions
  19. var keyMatchRegex = regexp.MustCompile(`\"(\w+)\":`)
  20. var wordBarrierRegex = regexp.MustCompile(`(\w)([A-Z])`)
  21. marshalled := []byte(c)
  22. converted := keyMatchRegex.ReplaceAllFunc(
  23. marshalled,
  24. func(match []byte) []byte {
  25. return bytes.ToLower(wordBarrierRegex.ReplaceAll(
  26. match,
  27. []byte(`${1}_${2}`),
  28. ))
  29. },
  30. )
  31. return converted
  32. }