indent.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Copyright 2010 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package json
  5. import "bytes"
  6. // Compact appends to dst the JSON-encoded src with
  7. // insignificant space characters elided.
  8. func Compact(dst *bytes.Buffer, src []byte) error {
  9. return compact(dst, src, false)
  10. }
  11. func compact(dst *bytes.Buffer, src []byte, escape bool) error {
  12. origLen := dst.Len()
  13. var scan Scanner
  14. scan.Reset()
  15. start := 0
  16. for i, c := range src {
  17. if escape && (c == '<' || c == '>' || c == '&') {
  18. if start < i {
  19. dst.Write(src[start:i])
  20. }
  21. dst.WriteString(`\u00`)
  22. dst.WriteByte(hex[c>>4])
  23. dst.WriteByte(hex[c&0xF])
  24. start = i + 1
  25. }
  26. // Convert U+2028 and U+2029 (E2 80 A8 and E2 80 A9).
  27. if c == 0xE2 && i+2 < len(src) && src[i+1] == 0x80 && src[i+2]&^1 == 0xA8 {
  28. if start < i {
  29. dst.Write(src[start:i])
  30. }
  31. dst.WriteString(`\u202`)
  32. dst.WriteByte(hex[src[i+2]&0xF])
  33. start = i + 3
  34. }
  35. v := scan.Step(&scan, int(c))
  36. if v >= ScanSkipSpace {
  37. if v == ScanError {
  38. break
  39. }
  40. if start < i {
  41. dst.Write(src[start:i])
  42. }
  43. start = i + 1
  44. }
  45. }
  46. if scan.EOF() == ScanError {
  47. dst.Truncate(origLen)
  48. return scan.Err
  49. }
  50. if start < len(src) {
  51. dst.Write(src[start:])
  52. }
  53. return nil
  54. }
  55. func newline(dst *bytes.Buffer, prefix, indent string, depth int) {
  56. dst.WriteByte('\n')
  57. dst.WriteString(prefix)
  58. for i := 0; i < depth; i++ {
  59. dst.WriteString(indent)
  60. }
  61. }
  62. // Indent appends to dst an indented form of the JSON-encoded src.
  63. // Each element in a JSON object or array begins on a new,
  64. // indented line beginning with prefix followed by one or more
  65. // copies of indent according to the indentation nesting.
  66. // The data appended to dst does not begin with the prefix nor
  67. // any indentation, and has no trailing newline, to make it
  68. // easier to embed inside other formatted JSON data.
  69. func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error {
  70. origLen := dst.Len()
  71. var scan Scanner
  72. scan.Reset()
  73. needIndent := false
  74. depth := 0
  75. for _, c := range src {
  76. scan.bytes++
  77. v := scan.Step(&scan, int(c))
  78. if v == ScanSkipSpace {
  79. continue
  80. }
  81. if v == ScanError {
  82. break
  83. }
  84. if needIndent && v != ScanEndObject && v != ScanEndArray {
  85. needIndent = false
  86. depth++
  87. newline(dst, prefix, indent, depth)
  88. }
  89. // Emit semantically uninteresting bytes
  90. // (in particular, punctuation in strings) unmodified.
  91. if v == ScanContinue {
  92. dst.WriteByte(c)
  93. continue
  94. }
  95. // Add spacing around real punctuation.
  96. switch c {
  97. case '{', '[':
  98. // delay indent so that empty object and array are formatted as {} and [].
  99. needIndent = true
  100. dst.WriteByte(c)
  101. case ',':
  102. dst.WriteByte(c)
  103. newline(dst, prefix, indent, depth)
  104. case ':':
  105. dst.WriteByte(c)
  106. dst.WriteByte(' ')
  107. case '}', ']':
  108. if needIndent {
  109. // suppress indent in empty object/array
  110. needIndent = false
  111. } else {
  112. depth--
  113. newline(dst, prefix, indent, depth)
  114. }
  115. dst.WriteByte(c)
  116. default:
  117. dst.WriteByte(c)
  118. }
  119. }
  120. if scan.EOF() == ScanError {
  121. dst.Truncate(origLen)
  122. return scan.Err
  123. }
  124. return nil
  125. }