MCPcopy
hub / github.com/pquerna/ffjson / WriteJson

Function WriteJson

fflib/v1/jsonstring.go:48–123  ·  view source on GitHub ↗

** * Function ported from encoding/json: func (e *encodeState) string(s string) (int, error) */

(buf JsonStringWriter, s []byte)

Source from the content-addressed store, hash-verified

46 * Function ported from encoding/json: func (e *encodeState) string(s string) (int, error)
47 */
48func WriteJson(buf JsonStringWriter, s []byte) {
49 buf.WriteByte('"')
50 start := 0
51 for i := 0; i < len(s); {
52 if b := s[i]; b < utf8.RuneSelf {
53 /*
54 if 0x20 <= b && b != '\\' && b != '"' && b != '<' && b != '>' && b != '&' {
55 i++
56 continue
57 }
58 */
59 if lt[b] == true {
60 i++
61 continue
62 }
63
64 if start < i {
65 buf.Write(s[start:i])
66 }
67 switch b {
68 case '\\', '"':
69 buf.WriteByte('\\')
70 buf.WriteByte(b)
71 case '\n':
72 buf.WriteByte('\\')
73 buf.WriteByte('n')
74 case '\r':
75 buf.WriteByte('\\')
76 buf.WriteByte('r')
77 default:
78 // This encodes bytes < 0x20 except for \n and \r,
79 // as well as < and >. The latter are escaped because they
80 // can lead to security holes when user-controlled strings
81 // are rendered into JSON and served to some browsers.
82 buf.WriteString(`\u00`)
83 buf.WriteByte(hex[b>>4])
84 buf.WriteByte(hex[b&0xF])
85 }
86 i++
87 start = i
88 continue
89 }
90 c, size := utf8.DecodeRune(s[i:])
91 if c == utf8.RuneError && size == 1 {
92 if start < i {
93 buf.Write(s[start:i])
94 }
95 buf.WriteString(`\ufffd`)
96 i += size
97 start = i
98 continue
99 }
100 // U+2028 is LINE SEPARATOR.
101 // U+2029 is PARAGRAPH SEPARATOR.
102 // They are both technically valid characters in JSON strings,
103 // but don't work in JSONP, which has to be evaluated as JavaScript,
104 // and can lead to security holes there. It is valid JSON to
105 // escape them, so we do so unconditionally.

Callers 3

WriteJsonStringFunction · 0.85
lexStringMethod · 0.85
scanFieldMethod · 0.85

Calls 3

WriteByteMethod · 0.80
WriteStringMethod · 0.65
WriteMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…