Test the JSON serialization of Golang Marshal

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.