// Copyright 2010 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.package jsonimport ()// Compact appends to dst the JSON-encoded src with// insignificant space characters elided.func ( *bytes.Buffer, []byte) error {returncompact(, , false)}func ( *bytes.Buffer, []byte, bool) error { := .Len() := newScanner()deferfreeScanner() := 0for , := range {if && ( == '<' || == '>' || == '&') {if < { .Write([:]) } .WriteString(`\u00`) .WriteByte(hex[>>4]) .WriteByte(hex[&0xF]) = + 1 }// Convert U+2028 and U+2029 (E2 80 A8 and E2 80 A9).if && == 0xE2 && +2 < len() && [+1] == 0x80 && [+2]&^1 == 0xA8 {if < { .Write([:]) } .WriteString(`\u202`) .WriteByte(hex[[+2]&0xF]) = + 3 } := .step(, )if >= scanSkipSpace {if == scanError {break }if < { .Write([:]) } = + 1 } }if .eof() == scanError { .Truncate()return .err }if < len() { .Write([:]) }returnnil}func ( *bytes.Buffer, , string, int) { .WriteByte('\n') .WriteString()for := 0; < ; ++ { .WriteString() }}// Indent appends to dst an indented form of the JSON-encoded src.// Each element in a JSON object or array begins on a new,// indented line beginning with prefix followed by one or more// copies of indent according to the indentation nesting.// The data appended to dst does not begin with the prefix nor// any indentation, to make it easier to embed inside other formatted JSON data.// Although leading space characters (space, tab, carriage return, newline)// at the beginning of src are dropped, trailing space characters// at the end of src are preserved and copied to dst.// For example, if src has no trailing spaces, neither will dst;// if src ends in a trailing newline, so will dst.func ( *bytes.Buffer, []byte, , string) error { := .Len() := newScanner()deferfreeScanner() := false := 0for , := range { .bytes++ := .step(, )if == scanSkipSpace {continue }if == scanError {break }if && != scanEndObject && != scanEndArray { = false ++newline(, , , ) }// Emit semantically uninteresting bytes // (in particular, punctuation in strings) unmodified.if == scanContinue { .WriteByte()continue }// Add spacing around real punctuation.switch {case'{', '[':// delay indent so that empty object and array are formatted as {} and []. = true .WriteByte()case',': .WriteByte()newline(, , , )case':': .WriteByte() .WriteByte(' ')case'}', ']':if {// suppress indent in empty object/array = false } else { --newline(, , , ) } .WriteByte()default: .WriteByte() } }if .eof() == scanError { .Truncate()return .err }returnnil}