MCPcopy
hub / github.com/tidwall/gjson / AppendJSONString

Function AppendJSONString

gjson.go:2037–2086  ·  view source on GitHub ↗

AppendJSONString is a convenience function that converts the provided string to a valid JSON string and appends it to dst.

(dst []byte, s string)

Source from the content-addressed store, hash-verified

2035// AppendJSONString is a convenience function that converts the provided string
2036// to a valid JSON string and appends it to dst.
2037func AppendJSONString(dst []byte, s string) []byte {
2038 dst = append(dst, make([]byte, len(s)+2)...)
2039 dst = append(dst[:len(dst)-len(s)-2], '"')
2040 for i := 0; i < len(s); i++ {
2041 if s[i] < ' ' {
2042 dst = append(dst, '\\')
2043 switch s[i] {
2044 case '\b':
2045 dst = append(dst, 'b')
2046 case '\f':
2047 dst = append(dst, 'f')
2048 case '\n':
2049 dst = append(dst, 'n')
2050 case '\r':
2051 dst = append(dst, 'r')
2052 case '\t':
2053 dst = append(dst, 't')
2054 default:
2055 dst = append(dst, 'u')
2056 dst = appendHex16(dst, uint16(s[i]))
2057 }
2058 } else if !DisableEscapeHTML &&
2059 (s[i] == '>' || s[i] == '<' || s[i] == '&') {
2060 dst = append(dst, '\\', 'u')
2061 dst = appendHex16(dst, uint16(s[i]))
2062 } else if s[i] == '\\' {
2063 dst = append(dst, '\\', '\\')
2064 } else if s[i] == '"' {
2065 dst = append(dst, '\\', '"')
2066 } else if s[i] > 127 {
2067 // read utf8 character
2068 r, n := utf8.DecodeRuneInString(s[i:])
2069 if n == 0 {
2070 break
2071 }
2072 if r == utf8.RuneError && n == 1 {
2073 dst = append(dst, `\ufffd`...)
2074 } else if r == '\u2028' || r == '\u2029' {
2075 dst = append(dst, `\u202`...)
2076 dst = append(dst, hexchars[r&0xF])
2077 } else {
2078 dst = append(dst, s[i:i+n]...)
2079 }
2080 i = i + n - 1
2081 } else {
2082 dst = append(dst, s[i])
2083 }
2084 }
2085 return append(dst, '"')
2086}
2087
2088type parseContext struct {
2089 json string

Callers 3

testJSONStringFunction · 0.85
GetFunction · 0.85
modToStrFunction · 0.85

Calls 1

appendHex16Function · 0.85

Tested by 1

testJSONStringFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…