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
{
62 v
:= scan
.step(&scan
, int(c
))
63 if v
== scanSkipSpace
{
69 if needIndent
&& v
!= scanEndObject
&& v
!= scanEndArray
{
72 newline(dst
, prefix
, indent
, depth
)
75 // Emit semantically uninteresting bytes
76 // (in particular, punctuation in strings) unmodified.
77 if v
== scanContinue
{
82 // Add spacing around real punctuation.
85 // delay indent so that empty object and array are formatted as {} and [].
91 newline(dst
, prefix
, indent
, depth
)
99 // suppress indent in empty object/array
103 newline(dst
, prefix
, indent
, depth
)
111 if scan
.eof() == scanError
{
112 dst
.Truncate(origLen
)