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.
12 // Compact appends to dst the JSON-encoded src with
13 // insignificant space characters elided.
14 func Compact(dst *bytes.Buffer, src []byte) os.Error {
19 for i, c := range src {
20 v := scan.step(&scan, int(c))
21 if v >= scanSkipSpace {
26 dst.Write(src[start:i])
31 if scan.eof() == scanError {
36 dst.Write(src[start:])
41 func newline(dst *bytes.Buffer, prefix, indent string, depth int) {
43 dst.WriteString(prefix)
44 for i := 0; i < depth; i++ {
45 dst.WriteString(indent)
49 // Indent appends to dst an indented form of the JSON-encoded src.
50 // Each element in a JSON object or array begins on a new,
51 // indented line beginning with prefix followed by one or more
52 // copies of indent according to the indentation nesting.
53 // The data appended to dst has no trailing newline, to make it easier
54 // to embed inside other formatted JSON data.
55 func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) os.Error {
61 for _, c := range src {
63 v := scan.step(&scan, int(c))
64 if v == scanSkipSpace {
70 if needIndent && v != scanEndObject && v != scanEndArray {
73 newline(dst, prefix, indent, depth)
76 // Emit semantically uninteresting bytes
77 // (in particular, punctuation in strings) unmodified.
78 if v == scanContinue {
83 // Add spacing around real punctuation.
86 // delay indent so that empty object and array are formatted as {} and [].
92 newline(dst, prefix, indent, depth)
100 // suppress indent in empty object/array
104 newline(dst, prefix, indent, depth)
112 if scan.eof() == scanError {
113 dst.Truncate(origLen)