Func Test_json () {x, _: = json. Marshal ([]string{"aaa:123", "bbb:456"}) fmt. PRINTLN (x) var caps []stringjson. Unmarshal (x, &caps) fmt. Println (Caps)}//output Results-------------------------------[the "the" "The" "The" "The" "The" "The" "the" 98 98 98 bbb:456]//each character is converted to the corresponding Ascill value
A specific encoder is found by reflection, and this example corresponds to the encoder string
func (e *encodestate) string (s string) (int, error) {len0 := e. Len () e.writebyte (' "') Start := 0for i := 0; i < len (s); {if b := s[i]; b < utf8. runeself {if 0x20 <= b && b != ' \ \ ' && b != ' ' && b != ' < ' && b != ' > ' && b != ' & ' {i++continue}if start < i {e.writestring (s[ START:I])}switch b {case ' \ \ ', ' "': e.writebyte (' \ \ ') E.writebyte (b) case ' \ n ': E. WriteByte (' \ \ ') E.writebyte (' n ') case ' \ R ': e.writebyte (' \ \ ') e.writebyte (' R ') case ' \ t ': e.writebyte (' \ \ ') ) e.writebyte (' t ') default:// this encodes bytes < 0x20 except for \n and \r,// as well as <, > and &. the latter are escaped because they// can lead to Security holes when user-controlled strings// are rendered into json and served to some browsers.//This type hit the sign E. WriteString (' \u00 ') e.writebyte (hex[b>>4]) e.writebyte (hex[b&0xf])}i++start = icontinue}c, size := utf8. Decoderuneinstring (s[i:]) If c == utf8. Runeerror && size == 1 {if start < i {e.writestring (s[ START:I])}e.writestring (' \ufffd ') //This type hit the flag i += sizestart = icontinue}// 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&nbSp 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 c == ' \u2028 ' | | c == ' \u2029 ' {if start < i {e.writestring (s[start:i])}e. WriteString (' \u202 ') //This type hit the sign E. WriteByte (hex[c&0xf]) I += sizestart = icontinue}i += size}if start < len (s) {e.writestring (S[start:])}e.writebyte (' "') Return e.len () - len0, nil}
Test the JSON serialization of Golang Marshal