Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(395)

Delta Between Two Patch Sets: src/pkg/encoding/json/indent.go

Issue 97840044: code review 97840044: encoding/json: add example for Indent, clarify the docs.
Left Patch Set: diff -r 824f981dd4b7 https://code.google.com/p/go/ Created 9 years, 11 months ago
Right Patch Set: diff -r e3d533e55750 https://code.google.com/p/go/ Created 9 years, 10 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « src/pkg/encoding/json/example_test.go ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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
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
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 }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b