(the Application/json Media Type for JavaScript Object Notation (JSON)) rfc4627-json format definition

Source: Internet
Author: User
Tags control characters format definition rfc javascript eval
<span id="Label3"></p><p><p>Original http://laichendong.com/rfc4627-zh_cn/</p></p><p><p></p></p><p><p>Summary</p></p><p><p>JavaScript Object Notation (JSON) is a lightweight, text-based, cross-language data interchange Format. It derives from the ECMAScript programming language Standards (ECMAScript programming Language standard). JSON defines a set of portable formatting rules that are used to represent structured Data.</p></p><p><p>1. Introduction</p></p><p><p>JavaScript Object Notation (JSON) is a text format for structured data Serialization. It is based on the ECMAScript programming Language standard, Third Edition [ECMA] defines the JavaScript object literal.</p></p><p><p>JSON contains 4 underlying types (string, number, boolean, and Null) and two types of structures (objects and Arrays)</p></p><p><p>A string is a sequence of 0 or more Unicode Characters.</p></p><p><p>An object is an unordered collection of 0 or more name/value pairs in which the name/value pair is a string type, and the value can be a string, a number, a boolean, a null, an object, or an array type.</p></p><p><p>An array is an ordered sequence that consists of 0 or more Values.</p></p><p><p>The term "object" and "array" are called from Javascript's customary terminology.</p></p><p><p>The design goal of JSON is that it should be as small as possible, portable, textual, and can be a subset of Javascript.</p></p><p><p>1.1. conventions</p></p><p><p>The "must" in this article, "must not", "REQUIRED", "shall", "shall not", "should", "should not", "RECOMMENDED", "may", and "OPTIONAL" keyword meaning compliance From the definition in [RFC2119].</p></p><p><p>The syntax rules in this article follow the definitions in [RFC4234].</p></p><p><p>2. JSON syntax</p></p><p><p>A JSON literal is a token sequence that contains 6 structural characters, a string, a number, and 3 literals.</p></p><p><p>A JSON literal is a serialized result of an object or an array.</p></p><pre><pre>Json-text = Object/array</pre></pre><p><p>Here are 6 structure characters:</p></p><pre><pre>Begin-array = ws%<span style="color: #000000;">x5b ws ; [left bracket begin</span>-object = ws%<span style="color: #000000;">x7b ws ; {left Curly brace End</span>-array = ws%x5d ws ;] Right Bracket</pre></pre><p><p>You can add meaningless whitespace characters before and after 6 structure Characters.</p></p><pre><pre>WS = *<span style="color: #000000;"> ( </span>% x20/<span style="color: #000000;"> ; space </span> % x09/<span style="color: #000000;"> ; \ t horizontal tab </span> % x0a/<span style="color: #000000;"> ; \ n line break </span> % <span style="color: #000000;">x0d; \ r return character)</span></pre></pre><p><p>2.1. value</p></p><p><p>The JSON value must is an object, array, number, string, or one of the following three literals:</p></p><pre><pre><span style="color: #0000ff;">false</span> <span style="color: #0000ff;">NULL</span> <span style="color: #0000ff;">true</span></pre></pre><p><p>The literal must is lowercase, and any other literal is not allowed.</p></p><pre><pre><span style="color: #0000ff;">false</span> <span style="color: #0000ff;">NULL</span> <span style="color: #0000ff;">true</span> /object/array/number/<span style="color: #000000;"> string</span><span style="color: #0000ff;">false</span><span style="color: #0000ff;"></span><span style="color: #0000ff;">null</span>null <span style="color: #0000ff;"> </span> <span style="color: #0000ff;">true</span> <span style="color: #0000ff;">true</span></pre></pre><p><p>2.2. objects</p></p><p><p>The object structure is represented as a pair of curly braces wrapped around 0 to multiple name/value pairs (or called members). The name in the Name/value pair is a string followed by a colon that separates the name and Value. The value is followed by a comma to delimit the value and the name of the next Name/value pair. The name should within an object is Unique.</p></p><pre><pre>Object = Begin-object [member * (value-separator member)] end-<span style="color: #000000;"></span>= string Name-separator value</pre></pre><p><p>2.3. arrays</p></p><p><p>The array structure is represented by a pair of brackets enclosing 0 or more values (or elements). Values are separated by Commas.</p></p><pre><pre>Array = Begin-array [value * (value-separator value)] End-array</pre></pre><p><p>2.4. Digital</p></p><p><p>The representation of a number is similar to most other languages. The number contains an integer part prefixed with an optional minus sign, which<br>The following can be followed by a fractional part and/or exponential portion.</p></p><p><p>octal or hexadecimal form is not allowed. Starting with 0 is also not allowed.</p></p><p><p>The fractional part is a decimal point followed by one or more digits.</p></p><p><p>The exponential section begins with an open letter e, followed by a plus or minus SIGN. E and optional symbols can be<br>With one or more digits.</p></p><p><p>A numeric value that cannot be represented as a sequence of numbers (for example, Infinity and Nan) is not allowed.</p></p><pre><pre><span style="color: #0000ff;">int</span> <span style="color: #000000;">[frac] [exp] decimal</span>-point =%<span style="color: #000000;">x2e ;. digit1</span>-9 =%x31-39 ; 1-9<span style="color: #000000;"></span>=%x65/%<span style="color: #000000;">x45 <c7 /></span>= e [minus/plus] 1*<span style="color: #000000;"></span>= decimal-point 1*<span style="color: #000000;"></span><span style="color: #0000ff;">int</span> = zero/(digit1-9 *<span style="color: #000000;"></span>=%x2d <c1 2 />; -<span style="color: #000000;"></span>=%x2b ; +<span style="color: #000000;"></span>=%x30 ; 0</pre></pre><p><p>2.5. string</p></p><p><p>The representation of a string is similar to the way the C family is Handled. The string is quoted as the start and end. All Unicode characters, except for the following characters that must be escaped, can be placed directly in the string: quotation marks ("or"), backslashes (\), and control characters (u+0000 to u+001f).</p></p><p><p>Any character can be escaped. If it is within the basic Multilingual Plane (basic multilingual Plane (u+0000 to u+ffff)), it should be represented as a 6-character sequence: a backslash followed by a lowercase U, followed by a 4-bit 16-digit number representing the position of the Character. The letters in the 16 binary numbers can be in uppercase or lowercase a-f. For example, a string consisting of only one backslash can be represented as "\u005c".</p></p><p><p>In addition, there are some popular characters that can be escaped with a two-character sequence, for example: a string consisting of only one backslash can be represented as "\ \".</p></p><p><p>To escape characters that are not within the base multi-language plane (basic multilingual Plane), Use a 12-character sequence that represents the UTF-16 encoding proxy pair (encoding the UTF-16 pair). For example, a string containing only the g-spectral character (u+1d11e) can be represented as "\ud834\udd1e"</p></p><pre><pre>String = Quotation-mark *<span style="color: #0000ff;">char</span> quotation-<span style="color: #000000;">Mark</span><span style="color: #0000ff;">char</span> = unescaped/<span style="color: #000000;"> escape ( </span>% x22/ ; "<span style="color: #000000;"> quotation mark u+0022 %x5c/ ; \ Reverse Solidus u+005c %x2f/ ;/ solidus u+002f %x62/ ; b backspace u+0008 %x66/ ; F Form feed u+000c %x6e/ ; n line feed u+000a % x72/ ; r Carriage return u+000d %x74/ ; t tab u+0009 %x75 4HEXDIG) ; uxxxx< C38/>u+xxxxescape =%x5c ; quotation-mark =%x22 </span>"<span style="color: #000000;"></span>=%x20-21/%x23-5b/%x5d-10ffff</pre></pre><p><p>3. encoding</p></p><p><p>JSON text shall uses Unicode Encoding. The default encoding method is UTF-8.</p></p><p><p>Because the first two characters of the JSON text must be ASCII characters [RFC0020], it is possible to determine whether the byte stream is utf-8,utf-16 (be or Le) or UTF-32 (be or Le) by observing the first set of 4 8-bit Bytes.</p></p><pre><pre>The xx utf-<span style="color: #000000;">32BE</span><span style="color: #000000;"></span>xx. xx utf-xx utf-<span style="color: #000000;"></span>xx. xx utf-<span style="color: #000000;">16BExx xx</span>xx xx UTF-8</pre></pre><p><p>4. Parser</p></p><p><p>The JSON parser can convert JSON text to other Representations. The JSON interpreter must can accept all text that conforms to the JSON syntax. The JSON parser may accept text that is not in JSON form.</p></p><p><p>5. Generator</p></p><p><p>The JSON generator is capable of generating JSON text. The resulting result must strictly conforms to the JSON syntax.</p></p><p><p>6. The IANA (internet Digital distribution Agency) Considerations</p></p><p><p>The MIME media type for JSON text is Application/json.</p></p><p><p>Type Name: Application</p></p><p><p>Subtype Name: JSON</p></p><p><p>Required Parameters: N/A</p></p><p><p>Optional parameters: n/a</p></p><p><p>Coding Considerations: If the UTF-8 is a 8-bit byte, if it is UTF-16 and UTF-32 is the binary</p></p><p><p>JSON can be expressed in utf-8,utf-16 and UTF-32 encodings. If you use UTF-8, JSON is 8-byte compatible. In the case of UTF-16 or UTF-32, you must use binary content transfer encoding.</p></p><p><p>Security Considerations:</p></p><p><p>In general, scripting languages have security issues, and JSON is a subset of javascript, but because it excludes allocations and calls, it is safe.</p></p><p><p>If the character in the JSON text that is removed from the string part is a JSON token (token) character, It can be safely passed to the JavaScript eval () method (the method used to compile and execute a string). The test and replace methods in JavaScript that call two regular expressions, respectively, can quickly determine if the condition is met.</p></p><pre><pre><span style="color: #0000ff;">var</span> my_json_object =! (/[^,:{}\[\]0-9.\-+eaeflnr-u \n\r\t]/.test (text.replace (/"(\\.| [^"\\]) * "/g,")) && eval (' (' + text + ') ');</pre></pre><p><p>Interoperability Considerations: N/A</p></p><p><p>Release Specification: RFC 4627</p></p><p><p>Applications that use this media type:</p></p><p><p>JSON has been used to pass data between applications written in all of the following programming languages: ActionScript, c, C #, ColdFusion, Common Lisp, E, Erlang, Java, JavaScript, Lua, Objective CA ML, Perl, PHP, Python, Rebol, Ruby, and Scheme.</p></p><p><p>Additional information:</p></p><p><p>Magic number: N/A<br>File Extension:. JSON<br>Code for the Macintosh file Type: TEXT</p></p><p><p>For further information, please contact:</p></p><p><p>Douglas Crockford<br>[email protected]</p></p><p><p>Intended usage: COMMON</p></p><p><p>Restricted usage: None</p></p><p><p></p></p><p><p>Douglas Crockford<br>[email protected]</p></p><p><p>To modify the Controller:</p></p><p><p>Douglas Crockford<br>[email protected]</p></p><p><p>7. Security Considerations</p></p><p><p>Refer to "security considerations" in section Sixth.</p></p><p><p>8. For example</p></p><p><p>This is a JSON object:</p></p><pre><pre><span style="color: #000000;">{ </span>"Image"<span style="color: #000000;">: { </span>"Width": +<span style="color: #000000;">, </span>"Height": +<span style="color: #000000;">, </span>"Title": "View from 15th Floor "<span style="color: #000000;">, </span>" Thumbnail "<span style="color: #000000;">: { </span>" Url ":" http://www.example.com/image/481989943 "<span style="color: #000000;">, </span> "Height": "max"<span style="color: #000000;">, </span>"Width": "<span style="color: #000000;"> {" },</span>"IDs": [943, 234, 38793<span style="color: #000000;">] }}</span></pre></pre><p><p> This is an array containing two objects: </p> </p><pre><pre> <span style="color: #000000;">[{</span> "precision": "zip" <span style="color: #000000;" ", < span>" Latitude ": 37.7668<span style="color: #000000;" , < span> "longitude": -122.3959<span style="color: #000000;">, </span> " Address ":" "<span style=" color: #000000; " , < span>" city ":" SAN FRANCISCO "<span style=" color: #000000; ">, </span>" S Tate ":" CA "<span style=" color: #000000; " ", < span> "Zip": "94107" <span style="color: #000000;" ", < span>" Country ":" US "<span style=" color: #000000; ">},{</span>" precision ":" zip "<span style=" color: #000000; ">, </span>" Latitude ": 37.371991<span style="color: #000000;" , < span> "longitude": -122.026020<span style="color: #000000;">, </span> " Address ":" "<span style=" color: #000000; " , < span>" city ":" SUNNYVALE "<span style=" color: #000000; ">, </span>" State ":" CA "<span style=" color: #000000; " ", < span> "Zip": "94085" <span style="color: #000000;" ", < span>" Country ": "US" <span style="color: #000000;" "}] < span> </span></span></span></span></span></span></span></span></span></span></pre></pre><p><p>9. References</p></p><p><p>9.1. Reference Specifications</p></p><p><p>[ECMA] European Computer Manufacturers association, "ECMAScript<br>Language Specification 3rd Edition ", December 1999,<br>

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.