package jsoniterimport ()// htmlSafeSet holds the value true if the ASCII character with the given// array position can be safely represented inside a JSON string, embedded// inside of HTML <script> tags, without any additional escaping.//// All values are true except for the ASCII control characters (0-31), the// double quote ("), the backslash character ("\"), HTML opening and closing// tags ("<" and ">"), and the ampersand ("&").var htmlSafeSet = [utf8.RuneSelf]bool{' ': true,'!': true,'"': false,'#': true,'$': true,'%': true,'&': false,'\'': true,'(': true,')': true,'*': true,'+': true,',': true,'-': true,'.': true,'/': true,'0': true,'1': true,'2': true,'3': true,'4': true,'5': true,'6': true,'7': true,'8': true,'9': true,':': true,';': true,'<': false,'=': true,'>': false,'?': true,'@': true,'A': true,'B': true,'C': true,'D': true,'E': true,'F': true,'G': true,'H': true,'I': true,'J': true,'K': true,'L': true,'M': true,'N': true,'O': true,'P': true,'Q': true,'R': true,'S': true,'T': true,'U': true,'V': true,'W': true,'X': true,'Y': true,'Z': true,'[': true,'\\': false,']': true,'^': true,'_': true,'`': true,'a': true,'b': true,'c': true,'d': true,'e': true,'f': true,'g': true,'h': true,'i': true,'j': true,'k': true,'l': true,'m': true,'n': true,'o': true,'p': true,'q': true,'r': true,'s': true,'t': true,'u': true,'v': true,'w': true,'x': true,'y': true,'z': true,'{': true,'|': true,'}': true,'~': true,'\u007f': true,}// safeSet holds the value true if the ASCII character with the given array// position can be represented inside a JSON string without any further// escaping.//// All values are true except for the ASCII control characters (0-31), the// double quote ("), and the backslash character ("\").var safeSet = [utf8.RuneSelf]bool{' ': true,'!': true,'"': false,'#': true,'$': true,'%': true,'&': true,'\'': true,'(': true,')': true,'*': true,'+': true,',': true,'-': true,'.': true,'/': true,'0': true,'1': true,'2': true,'3': true,'4': true,'5': true,'6': true,'7': true,'8': true,'9': true,':': true,';': true,'<': true,'=': true,'>': true,'?': true,'@': true,'A': true,'B': true,'C': true,'D': true,'E': true,'F': true,'G': true,'H': true,'I': true,'J': true,'K': true,'L': true,'M': true,'N': true,'O': true,'P': true,'Q': true,'R': true,'S': true,'T': true,'U': true,'V': true,'W': true,'X': true,'Y': true,'Z': true,'[': true,'\\': false,']': true,'^': true,'_': true,'`': true,'a': true,'b': true,'c': true,'d': true,'e': true,'f': true,'g': true,'h': true,'i': true,'j': true,'k': true,'l': true,'m': true,'n': true,'o': true,'p': true,'q': true,'r': true,'s': true,'t': true,'u': true,'v': true,'w': true,'x': true,'y': true,'z': true,'{': true,'|': true,'}': true,'~': true,'\u007f': true,}var hex = "0123456789abcdef"// WriteStringWithHTMLEscaped write string to stream with html special characters escapedfunc ( *Stream) ( string) { := len() .buf = append(.buf, '"')// write string, the fast path, without utf8 and escape support := 0for ; < ; ++ { := []if < utf8.RuneSelf && htmlSafeSet[] { .buf = append(.buf, ) } else {break } }if == { .buf = append(.buf, '"')return }writeStringSlowPathWithHTMLEscaped(, , , )}func writeStringSlowPathWithHTMLEscaped( *Stream, int, string, int) { := // for the remaining parts, we process them char by charfor < {if := []; < utf8.RuneSelf {ifhtmlSafeSet[] { ++continue }if < { .WriteRaw([:]) }switch {case'\\', '"': .writeTwoBytes('\\', )case'\n': .writeTwoBytes('\\', 'n')case'\r': .writeTwoBytes('\\', 'r')case'\t': .writeTwoBytes('\\', 't')default:// This encodes bytes < 0x20 except for \t, \n and \r. // If escapeHTML is set, it also escapes <, >, and & // because they can lead to security holes when // user-controlled strings are rendered into JSON // and served to some browsers. .WriteRaw(`\u00`) .writeTwoBytes(hex[>>4], hex[&0xF]) } ++ = continue } , := utf8.DecodeRuneInString([:])if == utf8.RuneError && == 1 {if < { .WriteRaw([:]) } .WriteRaw(`\ufffd`) ++ = continue }// U+2028 is LINE SEPARATOR. // U+2029 is PARAGRAPH SEPARATOR. // They are both technically valid characters in JSON strings, // but don't work in JSONP, which has to be evaluated as JavaScript, // and can lead to security holes there. It is valid JSON to // escape them, so we do so unconditionally. // See http://timelessrepo.com/json-isnt-a-javascript-subset for discussion.if == '\u2028' || == '\u2029' {if < { .WriteRaw([:]) } .WriteRaw(`\u202`) .writeByte(hex[&0xF]) += = continue } += }if < len() { .WriteRaw([:]) } .writeByte('"')}// WriteString write string to stream without html escapefunc ( *Stream) ( string) { := len() .buf = append(.buf, '"')// write string, the fast path, without utf8 and escape support := 0for ; < ; ++ { := []if > 31 && != '"' && != '\\' { .buf = append(.buf, ) } else {break } }if == { .buf = append(.buf, '"')return }writeStringSlowPath(, , , )}func writeStringSlowPath( *Stream, int, string, int) { := // for the remaining parts, we process them char by charfor < {if := []; < utf8.RuneSelf {ifsafeSet[] { ++continue }if < { .WriteRaw([:]) }switch {case'\\', '"': .writeTwoBytes('\\', )case'\n': .writeTwoBytes('\\', 'n')case'\r': .writeTwoBytes('\\', 'r')case'\t': .writeTwoBytes('\\', 't')default:// This encodes bytes < 0x20 except for \t, \n and \r. // If escapeHTML is set, it also escapes <, >, and & // because they can lead to security holes when // user-controlled strings are rendered into JSON // and served to some browsers. .WriteRaw(`\u00`) .writeTwoBytes(hex[>>4], hex[&0xF]) } ++ = continue } ++continue }if < len() { .WriteRaw([:]) } .writeByte('"')}
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.