json.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package yu_proto
  2. import (
  3. yu_base64 "gogs.qqck.cn/s/tools/base64"
  4. yu_strconv "gogs.qqck.cn/s/tools/strconv"
  5. "strconv"
  6. )
  7. func (t *Protobuf) Json() string {
  8. t.tmp_buf = nil
  9. if t.list.id == 0 {
  10. return string(t.tmp_buf)
  11. }
  12. t.json_obj(t.list)
  13. return string(t.tmp_buf)
  14. }
  15. func (t *Protobuf) json_obj(list *node) {
  16. t.tmp_buf = append(t.tmp_buf, '{')
  17. j_end := false
  18. for {
  19. if list.id != 0 {
  20. j_end = true
  21. t.tmp_buf = append(t.tmp_buf, '"')
  22. t.tmp_buf = append(t.tmp_buf, yu_strconv.FormatUint64(list.id)...)
  23. t.tmp_buf = append(t.tmp_buf, '"', ':')
  24. t.json_value(list.value)
  25. t.tmp_buf = append(t.tmp_buf, ',')
  26. }
  27. if list._next == nil {
  28. break
  29. }
  30. list = list._next
  31. }
  32. if j_end {
  33. t.tmp_buf[len(t.tmp_buf)-1] = '}'
  34. } else {
  35. t.tmp_buf = append(t.tmp_buf, '}')
  36. }
  37. }
  38. func (t *Protobuf) json_arr(list *[]*node) {
  39. t.tmp_buf = append(t.tmp_buf, '[')
  40. j_end := false
  41. for _, j_value := range *list {
  42. j_end = true
  43. t.json_value(j_value)
  44. t.tmp_buf = append(t.tmp_buf, ',')
  45. }
  46. if j_end {
  47. t.tmp_buf[len(t.tmp_buf)-1] = ']'
  48. } else {
  49. t.tmp_buf = append(t.tmp_buf, ']')
  50. }
  51. }
  52. func (t *Protobuf) json_value(value any) {
  53. switch j_value := value.(type) {
  54. case bool:
  55. if j_value {
  56. t.tmp_buf = append(t.tmp_buf, 't', 'r', 'u', 'e')
  57. } else {
  58. t.tmp_buf = append(t.tmp_buf, 'f', 'a', 'l', 's', 'e')
  59. }
  60. case byte:
  61. t.tmp_buf = append(t.tmp_buf, yu_strconv.FormatByte(j_value)...)
  62. case int8:
  63. t.tmp_buf = append(t.tmp_buf, yu_strconv.FormatInt8(j_value)...)
  64. case int16:
  65. t.tmp_buf = append(t.tmp_buf, yu_strconv.FormatInt16(j_value)...)
  66. case uint16:
  67. t.tmp_buf = append(t.tmp_buf, yu_strconv.FormatUint16(j_value)...)
  68. case int32:
  69. t.tmp_buf = append(t.tmp_buf, yu_strconv.FormatInt32(j_value)...)
  70. case uint32:
  71. t.tmp_buf = append(t.tmp_buf, yu_strconv.FormatUint32(j_value)...)
  72. case int64:
  73. t.tmp_buf = append(t.tmp_buf, yu_strconv.FormatInt64(j_value)...)
  74. case uint64:
  75. t.tmp_buf = append(t.tmp_buf, yu_strconv.FormatUint64(j_value)...)
  76. case float32:
  77. t.tmp_buf = append(t.tmp_buf, strconv.FormatFloat(float64(j_value), 'f', -1, 32)...)
  78. case float64:
  79. t.tmp_buf = append(t.tmp_buf, strconv.FormatFloat(j_value, 'f', -1, 64)...)
  80. case []byte:
  81. t.tmp_buf = append(t.tmp_buf, '"')
  82. t.tmp_buf = append(t.tmp_buf, yu_base64.Std.Encode2S(j_value)...)
  83. t.tmp_buf = append(t.tmp_buf, '"')
  84. case *node:
  85. if j_value == nil {
  86. t.tmp_buf = append(t.tmp_buf, 'n', 'u', 'l', 'l')
  87. return
  88. } else if j_value.id == 0 {
  89. // 如果 id 等于 0 则是数组成员
  90. t.json_value(j_value.value)
  91. } else {
  92. t.json_obj(j_value)
  93. }
  94. case *[]*node:
  95. t.json_arr(j_value)
  96. default:
  97. panic("不支持的数据类型")
  98. }
  99. }