diff options
author | Ryan Johnson <rjohnson@mozilla.com> | 2021-04-29 16:16:42 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-29 16:16:42 -0700 |
commit | 95aca4b4d8fa62815d4bd412fff1a364f842814a (patch) | |
tree | 5e57661720fe9058d5c7db637e764800b50f9060 /files/tr/web/javascript/reference/global_objects/json/stringify/index.html | |
parent | ee3b1c87e3c8e72ca130943eed260ad642246581 (diff) | |
download | translated-content-95aca4b4d8fa62815d4bd412fff1a364f842814a.tar.gz translated-content-95aca4b4d8fa62815d4bd412fff1a364f842814a.tar.bz2 translated-content-95aca4b4d8fa62815d4bd412fff1a364f842814a.zip |
remove retired locales (#699)
Diffstat (limited to 'files/tr/web/javascript/reference/global_objects/json/stringify/index.html')
-rw-r--r-- | files/tr/web/javascript/reference/global_objects/json/stringify/index.html | 313 |
1 files changed, 0 insertions, 313 deletions
diff --git a/files/tr/web/javascript/reference/global_objects/json/stringify/index.html b/files/tr/web/javascript/reference/global_objects/json/stringify/index.html deleted file mode 100644 index 541e87d523..0000000000 --- a/files/tr/web/javascript/reference/global_objects/json/stringify/index.html +++ /dev/null @@ -1,313 +0,0 @@ ---- -title: JSON.stringify() -slug: Web/JavaScript/Reference/Global_Objects/JSON/stringify -translation_of: Web/JavaScript/Reference/Global_Objects/JSON/stringify ---- -<div>{{JSRef}}</div> - -<p><strong><code>JSON.stringify()</code></strong> metodu(fonksyionu) Javascript objesinin değerlerini JSON string'ine çevirir. Bu javascript nesnesi üzerinde değişiklik yapabilecek bir fonksiyon tanımlı ise nesne üzerinde çevirme işlemi yanında bunlar da yapılabilir.</p> - -<div>{{EmbedInteractiveExample("pages/js/json-stringify.html")}}</div> - - - -<h2 id="Syntax">Syntax</h2> - -<pre class="syntaxbox"><code>JSON.stringify(<var>value</var>[, <var>replacer</var>[, <var>space</var>]])</code></pre> - -<h3 id="Parameters">Parameters</h3> - -<dl> - <dt><code>value</code></dt> - <dd>The value to convert to a JSON string.</dd> - <dt><code>replacer</code> {{optional_inline}}</dt> - <dd>A function that alters the behavior of the stringification process, or an array of {{jsxref("String")}} and {{jsxref("Number")}} objects that serve as a whitelist for selecting/filtering the properties of the value object to be included in the JSON string. If this value is null or not provided, all properties of the object are included in the resulting JSON string.</dd> - <dt><code>space</code> {{optional_inline}}</dt> - <dd>A {{jsxref("String")}} or {{jsxref("Number")}} object that's used to insert white space into the output JSON string for readability purposes. If this is a <code>Number</code>, it indicates the number of space characters to use as white space; this number is capped at 10 (if it is greater, the value is just 10). Values less than 1 indicate that no space should be used. If this is a <code>String</code>, the string (or the first 10 characters of the string, if it's longer than that) is used as white space. If this parameter is not provided (or is null), no white space is used.</dd> -</dl> - -<h3 id="Return_value">Return value</h3> - -<p>A JSON string representing the given value.</p> - -<h3 id="Exceptions">Exceptions</h3> - -<p>Throws a {{jsxref("TypeError")}} ("cyclic object value") exception when a circular reference is found.</p> - -<h2 id="Description">Description</h2> - -<p><code>JSON.stringify()</code> converts a value to JSON notation representing it:</p> - -<ul> - <li>If the value has a <a href="#toJSON()_behavior">toJSON()</a> method, it's responsible to define what data will be serialized.</li> - <li>{{jsxref("Boolean")}}, {{jsxref("Number")}}, and {{jsxref("String")}} objects are converted to the corresponding primitive values during stringification, in accord with the traditional conversion semantics.</li> - <li>If {{jsxref("undefined")}}, a {{jsxref("Function")}}, or a {{jsxref("Symbol")}} is encountered during conversion it is either omitted (when it is found in an object) or censored to {{jsxref("null")}} (when it is found in an array). <code>JSON.stringify()</code> can also just return <code>undefined</code> when passing in "pure" values like <code>JSON.stringify(function(){})</code> or <code>JSON.stringify(undefined)</code>.</li> - <li>All {{jsxref("Symbol")}}-keyed properties will be completely ignored, even when using the <code>replacer</code> function.</li> - <li>The instances of {{jsxref("Date")}} implement the <code>toJSON()</code> function by returning a string (the same as <code>date.toISOString()</code>), thus they are treated as strings.</li> - <li>The numbers {{jsxref("Infinity")}} and {{jsxref("NaN")}} as well as the object {{jsxref("null")}} are all considered as <code>null</code>.</li> - <li>For all the other {{jsxref("Object")}} instances (including {{jsxref("Map")}}, {{jsxref("Set")}}, {{jsxref("WeakMap")}} and {{jsxref("WeakSet")}}), only their enumerable properties will be serialized.</li> -</ul> - -<pre class="brush: js">JSON.stringify({}); // '{}' -JSON.stringify(true); // 'true' -JSON.stringify('foo'); // '"foo"' -JSON.stringify([1, 'false', false]); // '[1,"false",false]' -JSON.stringify([NaN, null, Infinity]); // '[null,null,null]' -JSON.stringify({ x: 5 }); // '{"x":5}' - -JSON.stringify(new Date(2006, 0, 2, 15, 4, 5)) -// '"2006-01-02T15:04:05.000Z"' - -JSON.stringify({ x: 5, y: 6 }); -// '{"x":5,"y":6}' -JSON.stringify([new Number(3), new String('false'), new Boolean(false)]); -// '[3,"false",false]' - -// String-keyed array elements are not enumerable and make no sense in JSON -let a = ['foo', 'bar']; -a['baz'] = 'quux'; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ] -JSON.stringify(a); -// '["foo","bar"]' - -JSON.stringify({ x: [10, undefined, function(){}, Symbol('')] }); -// '{"x":[10,null,null,null]}' - -// Standard data structures -JSON.stringify([new Set([1]), new Map([[1, 2]]), new WeakSet([{a: 1}]), new WeakMap([[{a: 1}, 2]])]); -// '[{},{},{},{}]' - -// TypedArray -JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]); -// '[{"0":1},{"0":1},{"0":1}]' -JSON.stringify([new Uint8Array([1]), new Uint8ClampedArray([1]), new Uint16Array([1]), new Uint32Array([1])]); -// '[{"0":1},{"0":1},{"0":1},{"0":1}]' -JSON.stringify([new Float32Array([1]), new Float64Array([1])]); -// '[{"0":1},{"0":1}]' - -// toJSON() -JSON.stringify({ x: 5, y: 6, toJSON(){ return this.x + this.y; } }); -// '11' - -// Symbols: -JSON.stringify({ x: undefined, y: Object, z: Symbol('') }); -// '{}' -JSON.stringify({ [Symbol('foo')]: 'foo' }); -// '{}' -JSON.stringify({ [Symbol.for('foo')]: 'foo' }, [Symbol.for('foo')]); -// '{}' -JSON.stringify({ [Symbol.for('foo')]: 'foo' }, function(k, v) { - if (typeof k === 'symbol') { - return 'a symbol'; - } -}); -// undefined - -// Non-enumerable properties: -JSON.stringify( Object.create(null, { x: { value: 'x', enumerable: false }, y: { value: 'y', enumerable: true } }) ); -// '{"y":"y"}' -</pre> - -<h3 id="The_replacer_parameter"><a id="The replacer parameter" name="The replacer parameter"></a>The <code>replacer</code> parameter</h3> - -<p>The <code>replacer</code> parameter can be either a function or an array. As a function, it takes two parameters, the key and the value being stringified. The object in which the key was found is provided as the replacer's <code>this</code> parameter. Initially it gets called with an empty string as key representing the object being stringified, and it then gets called for each property on the object or array being stringified. It should return the value that should be added to the JSON string, as follows:</p> - -<ul> - <li>If you return a {{jsxref("Number")}}, the string corresponding to that number is used as the value for the property when added to the JSON string.</li> - <li>If you return a {{jsxref("String")}}, that string is used as the property's value when adding it to the JSON string.</li> - <li>If you return a {{jsxref("Boolean")}}, "true" or "false" is used as the property's value, as appropriate, when adding it to the JSON string.</li> - <li>If you return <code>null</code>, <code>null</code> will be added to the JSON string.</li> - <li>If you return any other object, the object is recursively stringified into the JSON string, calling the <code>replacer</code> function on each property, unless the object is a function, in which case nothing is added to the JSON string.</li> - <li>If you return <code>undefined</code>, the property is not included (i.e., filtered out) in the output JSON string.</li> -</ul> - -<div class="note"><strong>Note:</strong> You cannot use the <code>replacer</code> function to remove values from an array. If you return <code>undefined</code> or a function then <code>null</code> is used instead.</div> - -<div class="note"><strong>Note:</strong> If you wish the replacer to distinguish an initial object from a key with an empty string property (since both would give the empty string as key and potentially an object as value), you will have to keep track of the iteration count (if it is beyond the first iteration, it is a genuine empty string key).</div> - -<h4 id="Example_with_a_function">Example with a function</h4> - -<pre class="brush: js">function replacer(key, value) { - // Filtering out properties - if (typeof value === 'string') { - return undefined; - } - return value; -} - -var foo = {foundation: 'Mozilla', model: 'box', week: 45, transport: 'car', month: 7}; -JSON.stringify(foo, replacer); -// '{"week":45,"month":7}' -</pre> - -<h4 id="Example_with_an_array">Example with an array</h4> - -<p>If <code>replacer</code> is an array, the array's values indicate the names of the properties in the object that should be included in the resulting JSON string.</p> - -<pre class="brush: js">JSON.stringify(foo, ['week', 'month']); -// '{"week":45,"month":7}', only keep "week" and "month" properties -</pre> - -<h3 id="The_space_argument"><a id="The space argument" name="The space argument"></a>The <code>space</code> argument</h3> - -<p>The <code>space</code> argument may be used to control spacing in the final string. If it is a number, successive levels in the stringification will each be indented by this many space characters (up to 10). If it is a string, successive levels will be indented by this string (or the first ten characters of it).</p> - -<pre class="brush: js">JSON.stringify({ a: 2 }, null, ' '); -// '{ -// "a": 2 -// }' -</pre> - -<p>Using a tab character mimics standard pretty-print appearance:</p> - -<pre class="brush: js">JSON.stringify({ uno: 1, dos: 2 }, null, '\t'); -// returns the string: -// '{ -// "uno": 1, -// "dos": 2 -// }' -</pre> - -<h3 id="toJSON()_behavior"><code>toJSON()</code> behavior</h3> - -<p>If an object being stringified has a property named <code>toJSON</code> whose value is a function, then the <code>toJSON()</code> method customizes JSON stringification behavior: instead of the object being serialized, the value returned by the <code>toJSON()</code> method when called will be serialized. <code>JSON.stringify()</code> calls <code>toJSON</code> with one parameter:</p> - -<ul> - <li>if this object is a property value, the property name</li> - <li>if it is in an array, the index in the array, as a string</li> - <li>an empty string if <code>JSON.stringify()</code> was directly called on this object</li> -</ul> - -<p>For example:</p> - -<pre class="brush: js">var obj = { - data: 'data', - - toJSON(key){ - if(key) - return `Now I am a nested object under key '${key}'`; - - else - return this; - } -}; - -JSON.stringify(obj); -// '{"data":"data"}' - -JSON.stringify({ obj }) -// '{"obj":"Now I am a nested object under key 'obj'"}' - -JSON.stringify([ obj ]) -// '["Now I am a nested object under key '0'"]' -</pre> - -<h3 id="Issue_with_JSON.stringify()_when_serializing_circular_references">Issue with <code>JSON.stringify()</code> when serializing circular references</h3> - -<p>Note that since the <a href="https://www.json.org/">JSON format</a> doesn't support object references (although an <a href="http://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03">IETF draft exists</a>), a {{jsxref("TypeError")}} will be thrown if one attempts to encode an object with circular references.</p> - -<pre class="brush: js example-bad">const circularReference = {}; -circularReference.myself = circularReference; - -// Serializing circular references throws "TypeError: cyclic object value" -JSON.stringify(circularReference); -</pre> - -<p>To serialize circular references you can use a library that supports them (e.g. <a href="https://github.com/douglascrockford/JSON-js/blob/master/cycle.js">cycle.js</a> by Douglas Crockford) or implement a solution by yourself, which will require finding and replacing (or removing) the cyclic references by serializable values.</p> - -<h3 id="Issue_with_plain_JSON.stringify_for_use_as_JavaScript">Issue with plain <code>JSON.stringify</code> for use as JavaScript</h3> - -<p>Note that JSON is <a href="http://timelessrepo.com/json-isnt-a-javascript-subset">not a completely strict subset of JavaScript</a>, with two line terminators (Line separator and Paragraph separator) not needing to be escaped in JSON but needing to be escaped in JavaScript. Therefore, if the JSON is meant to be evaluated or directly utilized within <a href="https://en.wikipedia.org/wiki/JSONP">JSONP</a>, the following utility can be used:</p> - -<pre class="brush: js">function jsFriendlyJSONStringify (s) { - return JSON.stringify(s). - replace(/\u2028/g, '\\u2028'). - replace(/\u2029/g, '\\u2029'); -} - -var s = { - a: String.fromCharCode(0x2028), - b: String.fromCharCode(0x2029) -}; -try { - eval('(' + JSON.stringify(s) + ')'); -} catch (e) { - console.log(e); // "SyntaxError: unterminated string literal" -} - -// No need for a catch -eval('(' + jsFriendlyJSONStringify(s) + ')'); - -// console.log in Firefox unescapes the Unicode if -// logged to console, so we use alert -alert(jsFriendlyJSONStringify(s)); // {"a":"\u2028","b":"\u2029"}</pre> - -<h3 id="Example_of_using_JSON.stringify()_with_localStorage">Example of using <code>JSON.stringify()</code> with <code>localStorage</code></h3> - -<p>In a case where you want to store an object created by your user and allowing it to be restored even after the browser has been closed, the following example is a model for the applicability of <code>JSON.stringify()</code>:</p> - -<pre class="brush: js">// Creating an example of JSON -var session = { - 'screens': [], - 'state': true -}; -session.screens.push({ 'name': 'screenA', 'width': 450, 'height': 250 }); -session.screens.push({ 'name': 'screenB', 'width': 650, 'height': 350 }); -session.screens.push({ 'name': 'screenC', 'width': 750, 'height': 120 }); -session.screens.push({ 'name': 'screenD', 'width': 250, 'height': 60 }); -session.screens.push({ 'name': 'screenE', 'width': 390, 'height': 120 }); -session.screens.push({ 'name': 'screenF', 'width': 1240, 'height': 650 }); - -// Converting the JSON string with JSON.stringify() -// then saving with localStorage in the name of session -localStorage.setItem('session', JSON.stringify(session)); - -// Example of how to transform the String generated through -// JSON.stringify() and saved in localStorage in JSON object again -var restoredSession = JSON.parse(localStorage.getItem('session')); - -// Now restoredSession variable contains the object that was saved -// in localStorage -console.log(restoredSession); -</pre> - -<h2 id="Specifications">Specifications</h2> - -<table class="standard-table"> - <tbody> - <tr> - <th scope="col">Specification</th> - <th scope="col">Status</th> - <th scope="col">Comment</th> - </tr> - <tr> - <td>{{SpecName('ES5.1', '#sec-15.12.3', 'JSON.stringify')}}</td> - <td>{{Spec2('ES5.1')}}</td> - <td>Initial definition. Implemented in JavaScript 1.7.</td> - </tr> - <tr> - <td>{{SpecName('ES6', '#sec-json.stringify', 'JSON.stringify')}}</td> - <td>{{Spec2('ES6')}}</td> - <td> </td> - </tr> - <tr> - <td>{{SpecName('ESDraft', '#sec-json.stringify', 'JSON.stringify')}}</td> - <td>{{Spec2('ESDraft')}}</td> - <td> </td> - </tr> - </tbody> -</table> - -<h2 id="Browser_compatibility">Browser compatibility</h2> - -<div> - - -<p>{{Compat("javascript.builtins.JSON.stringify")}}</p> -</div> - -<h2 id="See_also">See also</h2> - -<ul> - <li>{{jsxref("JSON.parse()")}}</li> - <li><a href="https://github.com/douglascrockford/JSON-js/blob/master/cycle.js">cycle.js</a> – Introduces two functions, <code>JSON.decycle</code> and <code>JSON.retrocycle</code>, which makes it possible to encode and decode cyclical structures and dags into an extended and retrocompatible JSON format.</li> -</ul> |