| Next revision | Previous revision |
| programming:collection.next_json [2016/06/21 16:31] – created jlucas | programming:collection.next_json [2016/11/03 00:04] (current) – jlucas |
|---|
| <html> | <html> |
| <head> | |
| <title>Collection.next+JSON - Document Format</title> | |
| <link rel="stylesheet" href="../css/document.css"> | |
| <!--[if lt IE 9]> | |
| <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> | |
| <![endif]--> | |
| </head> | |
| |
| <body> | <body> |
| |
| <header class="navbar"> | |
| <nav class="top-nav"> | |
| <ul> | |
| <li> | |
| <a href="http://code.ge">code.ge</a> | |
| <span>»</span> | |
| </li> | |
| <li> | |
| <a href="/media-types">media types</a> | |
| <span>»</span> | |
| </li> | |
| <li> | |
| <a href="/media-types/collection-next-json">collection.next+json</a> | |
| </li> | |
| </ul> | |
| </nav> | |
| </header> | |
| |
| <section> | <section> |
| <header> | <header> |
| </p> | </p> |
| | |
| <pre><div style="text-align:left;color:#000000; font-size:1em; font-family:monospace; "><em><span style="color:#236e25;">/** | <pre> |
| * encodeURIComponent() function is not 100% compatible with | /** |
| * RFC3986 http://www.faqs.org/rfcs/rfc3986.html | * encodeURIComponent() function is not 100% compatible with |
| */</span></em> | * RFC3986 http://www.faqs.org/rfcs/rfc3986.html |
| <span style="color:#881350;">function</span> <span style="color:#003369;">encodeRFC3986</span>(value) { | */ |
| <span style="color:#881350;">return</span> <span style="color:#003369;">encodeURIComponent</span>(value) | function</span> encodeRFC3986</span>(value) { |
| .<span style="color:#003369;">replace</span>(<span style="color:#661aa9;">/!/g</span>, <span style="color:#760f15;">"%21"</span>) | return</span> encodeURIComponent</span>(value) |
| .<span style="color:#003369;">replace</span>(<span style="color:#661aa9;">/</span><span style="color:#be73fd;">\*</span><span style="color:#661aa9;">/g</span>, <span style="color:#760f15;">"%2A"</span>) | . replace</span>( /!/g</span>, "%21"</span>) |
| .<span style="color:#003369;">replace</span>(<span style="color:#661aa9;">/</span><span style="color:#be73fd;">\(</span><span style="color:#661aa9;">/g</span>, <span style="color:#760f15;">"%28"</span>) | . replace</span>( /</span> \*</span> /g</span>, "%2A"</span>) |
| .<span style="color:#003369;">replace</span>(<span style="color:#661aa9;">/</span><span style="color:#be73fd;">\)</span><span style="color:#661aa9;">/g</span>, <span style="color:#760f15;">"%29"</span>) | . replace</span>( /</span> \(</span> /g</span>, "%28"</span>) |
| .<span style="color:#003369;">replace</span>(<span style="color:#661aa9;">/'/g</span>, <span style="color:#760f15;">"%27"</span>); | . replace</span>( /</span> \)</span> /g</span>, "%29"</span>) |
| | . replace</span>( /'/g</span>, "%27"</span>); |
| } | } |
| |
| <span style="color:#881350;">function</span> <span style="color:#003369;">translateToFormUrlEncoded</span>(data) { | function</span> translateToFormUrlEncoded</span>(data) { |
| <span style="color:#881350;">var</span> buffer = []; | var</span> buffer = []; |
| |
| <span style="color:#881350;">for</span><span style="color:#003369;"> </span>(<span style="color:#881350;">var</span> i = <span style="color:#0000ff;">0</span>; i < data.length; i++) { | for</span> </span>( var</span> i = 0</span>; i < data.length; i++) { |
| <span style="color:#881350;">var</span> input = data[i]; | var</span> input = data[i]; |
| <span style="color:#881350;">var</span> name = input.name; | var</span> name = input.name; |
| <span style="color:#881350;">var</span> value = input.value; | var</span> value = input.value; |
| | |
| <span style="color:#881350;">if</span><span style="color:#003369;"> </span>(value === null) { <em><span style="color:#236e25;">// convert null value to empty string | if</span> </span>(value === null) { <em> // convert null value to empty string |
| </span></em> value = <span style="color:#760f15;">''</span>; | </span></em> value = ''</span>; |
| } <span style="color:#881350;">else</span> <span style="color:#881350;">if</span><span style="color:#003369;"> </span>(<span style="color:#881350;">typeof</span> value === <span style="color:#760f15;">"boolean"</span>) { <em><span style="color:#236e25;">// convert boolean value to integer 0 or 1 | } else</span> if</span> </span>( typeof</span> value === "boolean"</span>) { <em> // convert boolean value to integer 0 or 1 |
| </span></em> value = <span style="color:#003369;">Number</span>(value); | </span></em> value = Number</span>(value); |
| } | } |
| |
| buffer.<span style="color:#003369;">push</span>(<span style="color:#003369;">encodeRFC3986</span>(name) + <span style="color:#760f15;">"="</span> + <span style="color:#003369;">encodeRFC3986</span>(value)); | buffer. push</span>( encodeRFC3986</span>(name) + "="</span> + encodeRFC3986</span>(value)); |
| } | } |
| |
| <span style="color:#881350;">return</span> buffer.<span style="color:#003369;">join</span>(<span style="color:#760f15;">"&"</span>); | return</span> buffer. join</span>( "&"</span>); |
| } | } |
| |
| <span style="color:#881350;">var</span> data = [ | var</span> data = [ |
| {<span style="color:#760f15;">"name"</span>: <span style="color:#760f15;">"first-name"</span>, <span style="color:#760f15;">"value"</span>: <span style="color:#760f15;">"John"</span>}, | { "name"</span>: "first-name"</span>, "value"</span>: "John"</span>}, |
| {<span style="color:#760f15;">"name"</span>: <span style="color:#760f15;">"last-name"</span>, <span style="color:#760f15;">"value"</span>: <span style="color:#760f15;">"Doe"</span>}, | { "name"</span>: "last-name"</span>, "value"</span>: "Doe"</span>}, |
| {<span style="color:#760f15;">"name"</span>: <span style="color:#760f15;">"email"</span>, <span style="color:#760f15;">"value"</span>: <span style="color:#760f15;">"john@doe.com"</span>}, | { "name"</span>: "email"</span>, "value"</span>: "john@doe.com"</span>}, |
| {<span style="color:#760f15;">"name"</span>: <span style="color:#760f15;">"website"</span>, <span style="color:#760f15;">"value"</span>: <span style="color:#760f15;">"http://john.doe.com"</span>}, | { "name"</span>: "website"</span>, "value"</span>: "http://john.doe.com"</span>}, |
| {<span style="color:#760f15;">"name"</span>: <span style="color:#760f15;">"age"</span>, <span style="color:#760f15;">"value"</span>: <span style="color:#0000ff;">37</span>}, | { "name"</span>: "age"</span>, "value"</span>: 37</span>}, |
| {<span style="color:#760f15;">"name"</span>: <span style="color:#760f15;">"interests"</span>, <span style="color:#760f15;">"value"</span>: <span style="color:#760f15;">"music"</span>}, | { "name"</span>: "interests"</span>, "value"</span>: "music"</span>}, |
| {<span style="color:#760f15;">"name"</span>: <span style="color:#760f15;">"interests"</span>, <span style="color:#760f15;">"value"</span>: <span style="color:#760f15;">"sports"</span>}, | { "name"</span>: "interests"</span>, "value"</span>: "sports"</span>}, |
| {<span style="color:#760f15;">"name"</span>: <span style="color:#760f15;">"interests"</span>, <span style="color:#760f15;">"value"</span>: <span style="color:#760f15;">"cars"</span>}, | { "name"</span>: "interests"</span>, "value"</span>: "cars"</span>}, |
| {<span style="color:#760f15;">"name"</span>: <span style="color:#760f15;">"subscribe"</span>, <span style="color:#760f15;">"value"</span>: <span style="color:#881350;">false</span>} | { "name"</span>: "subscribe"</span>, "value"</span>: false</span>} |
| ]; | ]; |
| |
| <em><span style="color:#236e25;">// convert data array to application/x-www-form-urlencoded | <em> // convert data array to application/x-www-form-urlencoded |
| </span></em><span style="color:#881350;">var</span> postData = <span style="color:#003369;">translateToFormUrlEncoded</span>(data); | </span></em> var</span> postData = translateToFormUrlEncoded</span>(data); |
| </div></div> | |
| </pre> | </pre> |
| |
| <pre><span class="comment">// encoded contents of resulting postData variable</span> | <pre> |
| <code> | // encoded contents of resulting postData variable |
| first-name=John&last-name=Doe&email=john%40doe.com&website=http%3A%2F%2Fjohn.doe.com&age=37&interests=music&interests=sports&interests=cars&subscribe=0 | first-name=John&last-name=Doe&email=john%40doe.com&website=http%3A%2F%2Fjohn.doe.com&age=37&interests=music&interests=sports&interests=cars&subscribe=0</pre> |
| </code></pre> | |
| |
| </section><!-- /translating to form urlencoded --> | </section><!-- /translating to form urlencoded --> |
| </ul> | </ul> |
| </section> <!-- /references --> | </section> <!-- /references --> |
| | |
| <section> | |
| <h3 id="history">9. Update History</h3> | |
| <dl> | |
| <dt>2012-04-17</dt> | |
| <dd> | |
| Modified <a href="#property-type" class="keyword">type</a> property description. | |
| Added the <a href="#object-method" class="keyword">method</a> object. | |
| </dd> | |
| <dt>2012-04-13</dt> | |
| <dd> | |
| Changed some wording. | |
| Updated <a href="#relation-form" class="keyword">form</a> link relation description. | |
| </dd> | |
| <dt>2012-04-12</dt> | |
| <dd> | |
| Added the <a href="#object-status" class="keyword">status</a> object. | |
| </dd> | |
| <dt>2012-04-10</dt> | |
| <dd> | |
| Added the <a href="#relation-form" class="keyword">form</a> link relation. | |
| </dd> | |
| <dt>2012-04-09</dt> | |
| <dd> | |
| Dropped more unnecessary stuff. Added | |
| the <a href="#object-status" class="keyword">status</a> object and | |
| the <a href="#property-required" class="keyword">reqruired</a> property. | |
| </dd> | |
| <dt>2012-04-04</dt> | |
| <dd>Dropped complicated stuff from spec. Refined | |
| the <a href="#property-type" class="keyword">type</a> property and added | |
| the <a href="#property-multiple" class="keyword">multiple</a> property. | |
| </dd> | |
| <dt>2012-03-31</dt> | |
| <dd>Dropped complicated stuff from spec. Added | |
| the <a href="#object-list" class="keyword">list</a> object, | |
| the <a href="#array-options" class="keyword">options</a> array and | |
| the <a href="#property-type" class="keyword">type</a> property. | |
| </dd> | |
| <dt>2012-03-26</dt> | |
| <dd>Created</dd> | |
| </dl> | |
| </section> <!-- /update history --> | |
| </section> | </section> |
| |
| </body> | </body> |
| </html> | </html> |