LEFT | RIGHT |
1 // Copyright 2010 The Go Authors. All rights reserved. | 1 // Copyright 2010 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package json | 5 package json |
6 | 6 |
7 import "bytes" | 7 import "bytes" |
8 | 8 |
9 // Compact appends to dst the JSON-encoded src with | 9 // Compact appends to dst the JSON-encoded src with |
10 // insignificant space characters elided. | 10 // insignificant space characters elided. |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 | 59 |
60 func newline(dst *bytes.Buffer, prefix, indent string, depth int) { | 60 func newline(dst *bytes.Buffer, prefix, indent string, depth int) { |
61 dst.WriteByte('\n') | 61 dst.WriteByte('\n') |
62 dst.WriteString(prefix) | 62 dst.WriteString(prefix) |
63 for i := 0; i < depth; i++ { | 63 for i := 0; i < depth; i++ { |
64 dst.WriteString(indent) | 64 dst.WriteString(indent) |
65 } | 65 } |
66 } | 66 } |
67 | 67 |
68 // Indent appends to dst an indented form of the JSON-encoded src. | 68 // Indent appends to dst an indented form of the JSON-encoded src. |
69 // Each element following the first in a JSON object or array begins | 69 // Each element in a JSON object or array begins on a new, |
70 // on a new, indented line beginning with prefix followed by one or | 70 // indented line beginning with prefix followed by one or more |
71 // more copies of indent according to the indentation nesting. The | 71 // copies of indent according to the indentation nesting. |
72 // data appended to dst does not begin with the prefix nor any | 72 // The data appended to dst does not begin with the prefix nor |
73 // indentation, and has no trailing newline, to make it easier to | 73 // any indentation, and has no trailing newline, to make it |
74 // embed inside other formatted JSON data. | 74 // easier to embed inside other formatted JSON data. |
75 func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error { | 75 func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error { |
76 origLen := dst.Len() | 76 origLen := dst.Len() |
77 var scan scanner | 77 var scan scanner |
78 scan.reset() | 78 scan.reset() |
79 needIndent := false | 79 needIndent := false |
80 depth := 0 | 80 depth := 0 |
81 for _, c := range src { | 81 for _, c := range src { |
82 scan.bytes++ | 82 scan.bytes++ |
83 v := scan.step(&scan, int(c)) | 83 v := scan.step(&scan, int(c)) |
84 if v == scanSkipSpace { | 84 if v == scanSkipSpace { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 default: | 128 default: |
129 dst.WriteByte(c) | 129 dst.WriteByte(c) |
130 } | 130 } |
131 } | 131 } |
132 if scan.eof() == scanError { | 132 if scan.eof() == scanError { |
133 dst.Truncate(origLen) | 133 dst.Truncate(origLen) |
134 return scan.err | 134 return scan.err |
135 } | 135 } |
136 return nil | 136 return nil |
137 } | 137 } |
LEFT | RIGHT |