github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/src/pkg/encoding/json/indent.go (about)

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