MCPcopy Create free account
hub / github.com/dolthub/doltgresql / encodeJSONString

Function encodeJSONString

postgres/parser/json/json.go:596–651  ·  view source on GitHub ↗

encodeJSONString writes a string literal to buf as a JSON string. Cribbed from https://github.com/golang/go/blob/7badae85f20f1bce4cc344f9202447618d45d414/src/encoding/json/encode.go.

(buf *bytes.Buffer, s string)

Source from the content-addressed store, hash-verified

594// encodeJSONString writes a string literal to buf as a JSON string.
595// Cribbed from https://github.com/golang/go/blob/7badae85f20f1bce4cc344f9202447618d45d414/src/encoding/json/encode.go.
596func encodeJSONString(buf *bytes.Buffer, s string) {
597 buf.WriteByte('"')
598 start := 0
599 for i := 0; i < len(s); {
600 if b := s[i]; b < utf8.RuneSelf {
601 if safeSet[b] {
602 i++
603 continue
604 }
605 if start < i {
606 buf.WriteString(s[start:i])
607 }
608 switch b {
609 case '\\', '"':
610 buf.WriteByte('\\')
611 buf.WriteByte(b)
612 case '\n':
613 buf.WriteByte('\\')
614 buf.WriteByte('n')
615 case '\r':
616 buf.WriteByte('\\')
617 buf.WriteByte('r')
618 case '\t':
619 buf.WriteByte('\\')
620 buf.WriteByte('t')
621 default:
622 // This encodes bytes < 0x20 except for \t, \n and \r.
623 // If escapeHTML is set, it also escapes <, >, and &
624 // because they can lead to security holes when
625 // user-controlled strings are rendered into JSON
626 // and served to some browsers.
627 buf.WriteString(`\u00`)
628 buf.WriteByte(hexAlphabet[b>>4])
629 buf.WriteByte(hexAlphabet[b&0xF])
630 }
631 i++
632 start = i
633 continue
634 }
635 c, size := utf8.DecodeRuneInString(s[i:])
636 if c == utf8.RuneError && size == 1 {
637 if start < i {
638 buf.WriteString(s[start:i])
639 }
640 buf.WriteString(`\ufffd`)
641 i += size
642 start = i
643 continue
644 }
645 i += size
646 }
647 if start < len(s) {
648 buf.WriteString(s[start:])
649 }
650 buf.WriteByte('"')
651}
652
653func (j jsonArray) Format(buf *bytes.Buffer) {

Callers 2

FormatMethod · 0.85
FormatMethod · 0.85

Calls 1

WriteStringMethod · 0.80

Tested by

no test coverage detected