| 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. |
| 4 |
|
| 5 |
//go:build goexperiment.jsonv2 |
| 6 |
|
| 7 |
package json |
| 8 |
|
| 9 |
import ( |
| 10 |
"bytes" |
| 11 |
"strings" |
| 12 |
|
| 13 |
"encoding/json/jsontext" |
| 14 |
) |
| 15 |
|
| 16 |
// HTMLEscape appends to dst the JSON-encoded src with <, >, &, U+2028 and U+2029 |
| 17 |
// characters inside string literals changed to \u003c, \u003e, \u0026, \u2028, \u2029 |
| 18 |
// so that the JSON will be safe to embed inside HTML <script> tags. |
| 19 |
// For historical reasons, web browsers don't honor standard HTML |
| 20 |
// escaping within <script> tags, so an alternative JSON encoding must be used. |
| 21 |
func HTMLEscape(dst *bytes.Buffer, src []byte) { |
| 22 |
dst.Grow(len(src)) |
| 23 |
dst.Write(appendHTMLEscape(dst.AvailableBuffer(), src)) |
| 24 |
} |
| 25 |
|
| 26 |
func appendHTMLEscape(dst, src []byte) []byte { |
| 27 |
const hex = "0123456789abcdef" |
| 28 |
// The characters can only appear in string literals, |
| 29 |
// so just scan the string one byte at a time. |
| 30 |
start := 0 |
| 31 |
for i, c := range src { |
| 32 |
if c == '<' || c == '>' || c == '&' { |
| 33 |
dst = append(dst, src[start:i]...) |
| 34 |
dst = append(dst, '\\', 'u', '0', '0', hex[c>>4], hex[c&0xF]) |
| 35 |
start = i + 1 |
| 36 |
} |
| 37 |
// Convert U+2028 and U+2029 (E2 80 A8 and E2 80 A9). |
| 38 |
if c == 0xE2 && i+2 < len(src) && src[i+1] == 0x80 && src[i+2]&^1 == 0xA8 { |
| 39 |
dst = append(dst, src[start:i]...) |
| 40 |
dst = append(dst, '\\', 'u', '2', '0', '2', hex[src[i+2]&0xF]) |
| 41 |
start = i + len("\u2029") |
| 42 |
} |
| 43 |
} |
| 44 |
return append(dst, src[start:]...) |
| 45 |
} |
| 46 |
|
| 47 |
// Compact appends to dst the JSON-encoded src with |
| 48 |
// insignificant space characters elided. |
| 49 |
func Compact(dst *bytes.Buffer, src []byte) error { |
| 50 |
dst.Grow(len(src)) |
| 51 |
b := dst.AvailableBuffer() |
| 52 |
b, err := jsontext.AppendFormat(b, src, |
| 53 |
jsontext.AllowDuplicateNames(true), |
| 54 |
jsontext.AllowInvalidUTF8(true), |
| 55 |
jsontext.PreserveRawStrings(true)) |
| 56 |
if err != nil { |
| 57 |
return transformSyntacticError(err) |
| 58 |
} |
| 59 |
dst.Write(b) |
| 60 |
return nil |
| 61 |
} |
| 62 |
|
| 63 |
// indentGrowthFactor specifies the growth factor of indenting JSON input. |
| 64 |
// Empirically, the growth factor was measured to be between 1.4x to 1.8x |
| 65 |
// for some set of compacted JSON with the indent being a single tab. |
| 66 |
// Specify a growth factor slightly larger than what is observed |
| 67 |
// to reduce probability of allocation in appendIndent. |
| 68 |
// A factor no higher than 2 ensures that wasted space never exceeds 50%. |
| 69 |
const indentGrowthFactor = 2 |
| 70 |
|
| 71 |
// Indent appends to dst an indented form of the JSON-encoded src. |
| 72 |
// Each element in a JSON object or array begins on a new, |
| 73 |
// indented line beginning with prefix followed by one or more |
| 74 |
// copies of indent according to the indentation nesting. |
| 75 |
// The data appended to dst does not begin with the prefix nor |
| 76 |
// any indentation, to make it easier to embed inside other formatted JSON data. |
| 77 |
// Although leading space characters (space, tab, carriage return, newline) |
| 78 |
// at the beginning of src are dropped, trailing space characters |
| 79 |
// at the end of src are preserved and copied to dst. |
| 80 |
// For example, if src has no trailing spaces, neither will dst; |
| 81 |
// if src ends in a trailing newline, so will dst. |
| 82 |
func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error { |
| 83 |
dst.Grow(indentGrowthFactor * len(src)) |
| 84 |
b := dst.AvailableBuffer() |
| 85 |
b, err := appendIndent(b, src, prefix, indent) |
| 86 |
dst.Write(b) |
| 87 |
return err |
| 88 |
} |
| 89 |
|
| 90 |
func appendIndent(dst, src []byte, prefix, indent string) ([]byte, error) { |
| 91 |
// In v2, only spaces and tabs are allowed, while v1 allowed any character. |
| 92 |
dstLen := len(dst) |
| 93 |
if len(strings.Trim(prefix, " \t"))+len(strings.Trim(indent, " \t")) > 0 { |
| 94 |
// Use placeholder spaces of correct length, and replace afterwards. |
| 95 |
invalidPrefix, invalidIndent := prefix, indent |
| 96 |
prefix = strings.Repeat(" ", len(prefix)) |
| 97 |
indent = strings.Repeat(" ", len(indent)) |
| 98 |
defer func() { |
| 99 |
b := dst[dstLen:] |
| 100 |
for i := bytes.IndexByte(b, '\n'); i >= 0; i = bytes.IndexByte(b, '\n') { |
| 101 |
b = b[i+len("\n"):] |
| 102 |
n := len(b) - len(bytes.TrimLeft(b, " ")) // len(prefix)+n*len(indent) |
| 103 |
spaces := b[:n] |
| 104 |
spaces = spaces[copy(spaces, invalidPrefix):] |
| 105 |
for len(spaces) > 0 { |
| 106 |
spaces = spaces[copy(spaces, invalidIndent):] |
| 107 |
} |
| 108 |
b = b[n:] |
| 109 |
} |
| 110 |
}() |
| 111 |
} |
| 112 |
|
| 113 |
dst, err := jsontext.AppendFormat(dst, src, |
| 114 |
jsontext.AllowDuplicateNames(true), |
| 115 |
jsontext.AllowInvalidUTF8(true), |
| 116 |
jsontext.PreserveRawStrings(true), |
| 117 |
jsontext.Multiline(true), |
| 118 |
jsontext.WithIndentPrefix(prefix), |
| 119 |
jsontext.WithIndent(indent)) |
| 120 |
if err != nil { |
| 121 |
return dst[:dstLen], transformSyntacticError(err) |
| 122 |
} |
| 123 |
|
| 124 |
// In v2, trailing whitespace is discarded, while v1 preserved it. |
| 125 |
if n := len(src) - len(bytes.TrimRight(src, " \n\r\t")); n > 0 { |
| 126 |
dst = append(dst, src[len(src)-n:]...) |
| 127 |
} |
| 128 |
return dst, nil |
| 129 |
} |