Source file src/encoding/json/indent.go

     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  
     5  package json
     6  
     7  import "bytes"
     8  
     9  // HTMLEscape appends to dst the JSON-encoded src with <, >, &, U+2028 and U+2029
    10  // characters inside string literals changed to \u003c, \u003e, \u0026, \u2028, \u2029
    11  // so that the JSON will be safe to embed inside HTML <script> tags.
    12  // For historical reasons, web browsers don't honor standard HTML
    13  // escaping within <script> tags, so an alternative JSON encoding must be used.
    14  func HTMLEscape(dst *bytes.Buffer, src []byte) {
    15  	dst.Grow(len(src))
    16  	dst.Write(appendHTMLEscape(dst.AvailableBuffer(), src))
    17  }
    18  
    19  func appendHTMLEscape(dst, src []byte) []byte {
    20  	// The characters can only appear in string literals,
    21  	// so just scan the string one byte at a time.
    22  	start := 0
    23  	for i, c := range src {
    24  		if c == '<' || c == '>' || c == '&' {
    25  			dst = append(dst, src[start:i]...)
    26  			dst = append(dst, '\\', 'u', '0', '0', hex[c>>4], hex[c&0xF])
    27  			start = i + 1
    28  		}
    29  		// Convert U+2028 and U+2029 (E2 80 A8 and E2 80 A9).
    30  		if c == 0xE2 && i+2 < len(src) && src[i+1] == 0x80 && src[i+2]&^1 == 0xA8 {
    31  			dst = append(dst, src[start:i]...)
    32  			dst = append(dst, '\\', 'u', '2', '0', '2', hex[src[i+2]&0xF])
    33  			start = i + len("\u2029")
    34  		}
    35  	}
    36  	return append(dst, src[start:]...)
    37  }
    38  
    39  // Compact appends to dst the JSON-encoded src with
    40  // insignificant space characters elided.
    41  func Compact(dst *bytes.Buffer, src []byte) error {
    42  	dst.Grow(len(src))
    43  	b := dst.AvailableBuffer()
    44  	b, err := appendCompact(b, src, false)
    45  	dst.Write(b)
    46  	return err
    47  }
    48  
    49  func appendCompact(dst, src []byte, escape bool) ([]byte, error) {
    50  	origLen := len(dst)
    51  	scan := newScanner()
    52  	defer freeScanner(scan)
    53  	start := 0
    54  	for i, c := range src {
    55  		if escape && (c == '<' || c == '>' || c == '&') {
    56  			dst = append(dst, src[start:i]...)
    57  			dst = append(dst, '\\', 'u', '0', '0', hex[c>>4], hex[c&0xF])
    58  			start = i + 1
    59  		}
    60  		// Convert U+2028 and U+2029 (E2 80 A8 and E2 80 A9).
    61  		if escape && c == 0xE2 && i+2 < len(src) && src[i+1] == 0x80 && src[i+2]&^1 == 0xA8 {
    62  			dst = append(dst, src[start:i]...)
    63  			dst = append(dst, '\\', 'u', '2', '0', '2', hex[src[i+2]&0xF])
    64  			start = i + len("\u2029")
    65  		}
    66  		v := scan.step(scan, c)
    67  		if v >= scanSkipSpace {
    68  			if v == scanError {
    69  				break
    70  			}
    71  			dst = append(dst, src[start:i]...)
    72  			start = i + 1
    73  		}
    74  	}
    75  	if scan.eof() == scanError {
    76  		return dst[:origLen], scan.err
    77  	}
    78  	dst = append(dst, src[start:]...)
    79  	return dst, nil
    80  }
    81  
    82  func appendNewline(dst []byte, prefix, indent string, depth int) []byte {
    83  	dst = append(dst, '\n')
    84  	dst = append(dst, prefix...)
    85  	for i := 0; i < depth; i++ {
    86  		dst = append(dst, indent...)
    87  	}
    88  	return dst
    89  }
    90  
    91  // indentGrowthFactor specifies the growth factor of indenting JSON input.
    92  // Empirically, the growth factor was measured to be between 1.4x to 1.8x
    93  // for some set of compacted JSON with the indent being a single tab.
    94  // Specify a growth factor slightly larger than what is observed
    95  // to reduce probability of allocation in appendIndent.
    96  // A factor no higher than 2 ensures that wasted space never exceeds 50%.
    97  const indentGrowthFactor = 2
    98  
    99  // Indent appends to dst an indented form of the JSON-encoded src.
   100  // Each element in a JSON object or array begins on a new,
   101  // indented line beginning with prefix followed by one or more
   102  // copies of indent according to the indentation nesting.
   103  // The data appended to dst does not begin with the prefix nor
   104  // any indentation, to make it easier to embed inside other formatted JSON data.
   105  // Although leading space characters (space, tab, carriage return, newline)
   106  // at the beginning of src are dropped, trailing space characters
   107  // at the end of src are preserved and copied to dst.
   108  // For example, if src has no trailing spaces, neither will dst;
   109  // if src ends in a trailing newline, so will dst.
   110  func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error {
   111  	dst.Grow(indentGrowthFactor * len(src))
   112  	b := dst.AvailableBuffer()
   113  	b, err := appendIndent(b, src, prefix, indent)
   114  	dst.Write(b)
   115  	return err
   116  }
   117  
   118  func appendIndent(dst, src []byte, prefix, indent string) ([]byte, error) {
   119  	origLen := len(dst)
   120  	scan := newScanner()
   121  	defer freeScanner(scan)
   122  	needIndent := false
   123  	depth := 0
   124  	for _, c := range src {
   125  		scan.bytes++
   126  		v := scan.step(scan, c)
   127  		if v == scanSkipSpace {
   128  			continue
   129  		}
   130  		if v == scanError {
   131  			break
   132  		}
   133  		if needIndent && v != scanEndObject && v != scanEndArray {
   134  			needIndent = false
   135  			depth++
   136  			dst = appendNewline(dst, prefix, indent, depth)
   137  		}
   138  
   139  		// Emit semantically uninteresting bytes
   140  		// (in particular, punctuation in strings) unmodified.
   141  		if v == scanContinue {
   142  			dst = append(dst, c)
   143  			continue
   144  		}
   145  
   146  		// Add spacing around real punctuation.
   147  		switch c {
   148  		case '{', '[':
   149  			// delay indent so that empty object and array are formatted as {} and [].
   150  			needIndent = true
   151  			dst = append(dst, c)
   152  		case ',':
   153  			dst = append(dst, c)
   154  			dst = appendNewline(dst, prefix, indent, depth)
   155  		case ':':
   156  			dst = append(dst, c, ' ')
   157  		case '}', ']':
   158  			if needIndent {
   159  				// suppress indent in empty object/array
   160  				needIndent = false
   161  			} else {
   162  				depth--
   163  				dst = appendNewline(dst, prefix, indent, depth)
   164  			}
   165  			dst = append(dst, c)
   166  		default:
   167  			dst = append(dst, c)
   168  		}
   169  	}
   170  	if scan.eof() == scanError {
   171  		return dst[:origLen], scan.err
   172  	}
   173  	return dst, nil
   174  }
   175  

View as plain text