indent.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 (
  6. "bytes"
  7. )
  8. // Compact appends to dst the JSON-encoded src with
  9. // insignificant space characters elided.
  10. func Compact(dst *bytes.Buffer, src []byte) error {
  11. return compact(dst, src, false)
  12. }
  13. func compact(dst *bytes.Buffer, src []byte, escape bool) error {
  14. origLen := dst.Len()
  15. scan := newScanner()
  16. defer freeScanner(scan)
  17. start := 0
  18. for i, c := range src {
  19. if escape && (c == '<' || c == '>' || c == '&') {
  20. if start < i {
  21. dst.Write(src[start:i])
  22. }
  23. dst.WriteString(`\u00`)
  24. dst.WriteByte(hex[c>>4])
  25. dst.WriteByte(hex[c&0xF])
  26. start = i + 1
  27. }
  28. // Convert U+2028 and U+2029 (E2 80 A8 and E2 80 A9).
  29. if escape && c == 0xE2 && i+2 < len(src) && src[i+1] == 0x80 && src[i+2]&^1 == 0xA8 {
  30. if start < i {
  31. dst.Write(src[start:i])
  32. }
  33. dst.WriteString(`\u202`)
  34. dst.WriteByte(hex[src[i+2]&0xF])
  35. start = i + 3
  36. }
  37. v := scan.step(scan, c)
  38. if v >= scanSkipSpace {
  39. if v == scanError {
  40. break
  41. }
  42. if start < i {
  43. dst.Write(src[start:i])
  44. }
  45. start = i + 1
  46. }
  47. }
  48. if scan.eof() == scanError {
  49. dst.Truncate(origLen)
  50. return scan.err
  51. }
  52. if start < len(src) {
  53. dst.Write(src[start:])
  54. }
  55. return nil
  56. }
  57. func newline(dst *bytes.Buffer, prefix, indent string, depth int) {
  58. dst.WriteByte('\n')
  59. dst.WriteString(prefix)
  60. for i := 0; i < depth; i++ {
  61. dst.WriteString(indent)
  62. }
  63. }
  64. // Indent appends to dst an indented form of the JSON-encoded src.
  65. // Each element in a JSON object or array begins on a new,
  66. // indented line beginning with prefix followed by one or more
  67. // copies of indent according to the indentation nesting.
  68. // The data appended to dst does not begin with the prefix nor
  69. // any indentation, to make it easier to embed inside other formatted JSON data.
  70. // Although leading space characters (space, tab, carriage return, newline)
  71. // at the beginning of src are dropped, trailing space characters
  72. // at the end of src are preserved and copied to dst.
  73. // For example, if src has no trailing spaces, neither will dst;
  74. // if src ends in a trailing newline, so will dst.
  75. func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error {
  76. origLen := dst.Len()
  77. scan := newScanner()
  78. defer freeScanner(scan)
  79. needIndent := false
  80. depth := 0
  81. for _, c := range src {
  82. scan.bytes++
  83. v := scan.step(scan, c)
  84. if v == scanSkipSpace {
  85. continue
  86. }
  87. if v == scanError {
  88. break
  89. }
  90. if needIndent && v != scanEndObject && v != scanEndArray {
  91. needIndent = false
  92. depth++
  93. newline(dst, prefix, indent, depth)
  94. }
  95. // Emit semantically uninteresting bytes
  96. // (in particular, punctuation in strings) unmodified.
  97. if v == scanContinue {
  98. dst.WriteByte(c)
  99. continue
  100. }
  101. // Add spacing around real punctuation.
  102. switch c {
  103. case '{', '[':
  104. // delay indent so that empty object and array are formatted as {} and [].
  105. needIndent = true
  106. dst.WriteByte(c)
  107. case ',':
  108. dst.WriteByte(c)
  109. newline(dst, prefix, indent, depth)
  110. case ':':
  111. dst.WriteByte(c)
  112. dst.WriteByte(' ')
  113. case '}', ']':
  114. if needIndent {
  115. // suppress indent in empty object/array
  116. needIndent = false
  117. } else {
  118. depth--
  119. newline(dst, prefix, indent, depth)
  120. }
  121. dst.WriteByte(c)
  122. default:
  123. dst.WriteByte(c)
  124. }
  125. }
  126. if scan.eof() == scanError {
  127. dst.Truncate(origLen)
  128. return scan.err
  129. }
  130. return nil
  131. }