reference/json/functions/json-encode.xml
19e8122137a1d42ed60f17fe2c0c2b69b0b2d16b
...
...
@@ -9,14 +9,25 @@
9
9
<refsect1 role="description">
10
10
&reftitle.description;
11
11
<methodsynopsis>
12
-
<type>string</type><methodname>json_encode</methodname>
12
+
<type class="union"><type>string</type><type>false</type></type><methodname>json_encode</methodname>
13
13
<methodparam><type>mixed</type><parameter>value</parameter></methodparam>
14
-
<methodparam choice="opt"><type>int</type><parameter>options</parameter><initializer>0</initializer></methodparam>
14
+
<methodparam choice="opt"><type>int</type><parameter>flags</parameter><initializer>0</initializer></methodparam>
15
15
<methodparam choice="opt"><type>int</type><parameter>depth</parameter><initializer>512</initializer></methodparam>
16
16
</methodsynopsis>
17
17
<para>
18
-
Returns a string containing the JSON representation of
19
-
<parameter>value</parameter>.
18
+
Returns a string containing the JSON representation of the supplied
19
+
<parameter>value</parameter>. If the parameter is an &array; or &object;,
20
+
it will be serialized recursively.
21
+
</para>
22
+
<para>
23
+
If a value to be serialized is an object, then by default only publicly visible
24
+
properties will be included. Alternatively, a class may implement <interfacename>JsonSerializable</interfacename>
25
+
to control how its values are serialized to <acronym>JSON</acronym>.
26
+
</para>
27
+
<para>
28
+
The encoding is affected by the supplied <parameter>flags</parameter>
29
+
and additionally the encoding of float values depends on the value of
30
+
<link linkend="ini.serialize-precision">serialize_precision</link>.
20
31
</para>
21
32
</refsect1>
22
33

...
...
@@ -29,7 +40,7 @@
29
40
<listitem>
30
41
<para>
31
42
The <parameter>value</parameter> being encoded. Can be any type except
32
-
a <type>resource</type>.
43
+
a &resource;.
33
44
</para>
34
45
<para>
35
46
All string data must be UTF-8 encoded.
...
...
@@ -38,20 +49,27 @@
38
49
</listitem>
39
50
</varlistentry>
40
51
<varlistentry>
41
-
<term><parameter>options</parameter></term>
52
+
<term><parameter>flags</parameter></term>
42
53
<listitem>
43
54
<para>
44
-
Bitmask consisting of <constant>JSON_HEX_QUOT</constant>,
55
+
Bitmask consisting of
56
+
<constant>JSON_FORCE_OBJECT</constant>,
57
+
<constant>JSON_HEX_QUOT</constant>,
45
58
<constant>JSON_HEX_TAG</constant>,
46
59
<constant>JSON_HEX_AMP</constant>,
47
60
<constant>JSON_HEX_APOS</constant>,
61
+
<constant>JSON_INVALID_UTF8_IGNORE</constant>,
62
+
<constant>JSON_INVALID_UTF8_SUBSTITUTE</constant>,
48
63
<constant>JSON_NUMERIC_CHECK</constant>,
64
+
<constant>JSON_PARTIAL_OUTPUT_ON_ERROR</constant>,
65
+
<constant>JSON_PRESERVE_ZERO_FRACTION</constant>,
49
66
<constant>JSON_PRETTY_PRINT</constant>,
67
+
<constant>JSON_UNESCAPED_LINE_TERMINATORS</constant>,
50
68
<constant>JSON_UNESCAPED_SLASHES</constant>,
51
-
<constant>JSON_FORCE_OBJECT</constant>,
52
-
<constant>JSON_UNESCAPED_UNICODE</constant>. The behaviour of these
53
-
constants is described on
54
-
<link linkend="json.constants">the JSON constants page</link>.
69
+
<constant>JSON_UNESCAPED_UNICODE</constant>,
70
+
<constant>JSON_THROW_ON_ERROR</constant>.
71
+
The behaviour of these constants is described on the
72
+
<link linkend="json.constants">JSON constants</link> page.
55
73
</para>
56
74
</listitem>
57
75
</varlistentry>
...
...
@@ -70,7 +88,7 @@
70
88
<refsect1 role="returnvalues">
71
89
&reftitle.returnvalues;
72
90
<para>
73
-
Returns a JSON encoded <type>string</type> on success &return.falseforfailure;.
91
+
Returns a JSON encoded &string; on success &return.falseforfailure;.
74
92
</para>
75
93
</refsect1>
76
94

...
...
@@ -87,27 +105,33 @@
87
105
</thead>
88
106
<tbody>
89
107
<row>
90
-
<entry>5.5.0</entry>
108
+
<entry>7.3.0</entry>
91
109
<entry>
92
-
<parameter>depth</parameter> parameter was added.
110
+
<constant>JSON_THROW_ON_ERROR</constant>
111
+
<parameter>flags</parameter> was added.
93
112
</entry>
94
113
</row>
95
114
<row>
96
-
<entry>5.4.0</entry>
115
+
<entry>7.2.0</entry>
97
116
<entry>
98
-
<constant>JSON_PRETTY_PRINT</constant>, <constant>JSON_UNESCAPED_SLASHES</constant>, and <constant>JSON_UNESCAPED_UNICODE</constant> <parameter>options</parameter> were added.
117
+
<constant>JSON_INVALID_UTF8_IGNORE</constant>, and
118
+
<constant>JSON_INVALID_UTF8_SUBSTITUTE</constant>
119
+
<parameter>flags</parameter> were added.
99
120
</entry>
100
121
</row>
101
122
<row>
102
-
<entry>5.3.3</entry>
123
+
<entry>7.1.0</entry>
103
124
<entry>
104
-
<constant>JSON_NUMERIC_CHECK</constant> <parameter>option</parameter> was added.
125
+
<constant>JSON_UNESCAPED_LINE_TERMINATORS</constant>
126
+
<parameter>flags</parameter> was added.
105
127
</entry>
106
128
</row>
107
129
<row>
108
-
<entry>5.3.0</entry>
130
+
<entry>7.1.0</entry>
109
131
<entry>
110
-
The <parameter>options</parameter> parameter was added.
132
+
<link linkend="ini.serialize-precision">serialize_precision</link> is
133
+
used instead of <link linkend="ini.precision">precision</link> when
134
+
encoding <type>float</type> values.
111
135
</entry>
112
136
</row>
113
137
</tbody>
...
...
@@ -140,7 +164,7 @@ echo json_encode($arr);
140
164

141
165
<example>
142
166
<title>
143
-
A <function>json_encode</function> example showing some options in use
167
+
A <function>json_encode</function> example showing some flags in use
144
168
</title>
145
169
<programlisting role="php">
146
170
<![CDATA[
...
...
@@ -195,6 +219,52 @@ Associative array always output as object: {"foo":"bar","baz":"long"}
195
219
</screen>
196
220
</example>
197
221
<example>
222
+
<title>JSON_NUMERIC_CHECK option example</title>
223
+
<programlisting role="php">
224
+
<![CDATA[
225
+
<?php
226
+
echo "Strings representing numbers automatically turned into numbers".PHP_EOL;
227
+
$numbers = array('+123123', '-123123', '1.2e3', '0.00001');
228
+
var_dump(
229
+
$numbers,
230
+
json_encode($numbers, JSON_NUMERIC_CHECK)
231
+
);
232
+
echo "Strings containing improperly formatted numbers".PHP_EOL;
233
+
$strings = array('+a33123456789', 'a123');
234
+
var_dump(
235
+
$strings,
236
+
json_encode($strings, JSON_NUMERIC_CHECK)
237
+
);
238
+
?>
239
+
]]>
240
+
</programlisting>
241
+
&example.outputs.similar;
242
+
<screen>
243
+
<![CDATA[
244
+
Strings representing numbers automatically turned into numbers
245
+
array(4) {
246
+
[0]=>
247
+
string(7) "+123123"
248
+
[1]=>
249
+
string(7) "-123123"
250
+
[2]=>
251
+
string(5) "1.2e3"
252
+
[3]=>
253
+
string(7) "0.00001"
254
+
}
255
+
string(28) "[123123,-123123,1200,1.0e-5]"
256
+
Strings containing improperly formatted numbers
257
+
array(2) {
258
+
[0]=>
259
+
string(13) "+a33123456789"
260
+
[1]=>
261
+
string(4) "a123"
262
+
}
263
+
string(24) "["+a33123456789","a123"]"
264
+
]]>
265
+
</screen>
266
+
</example>
267
+
<example>
198
268
<title>Sequential versus non-sequential array example</title>
199
269
<programlisting role="php">
200
270
<![CDATA[
...
...
@@ -264,6 +334,24 @@ string(33) "{"0":"foo","2":"baz","3":"blong"}"
264
334
]]>
265
335
</screen>
266
336
</example>
337
+
<example>
338
+
<title><constant>JSON_PRESERVE_ZERO_FRACTION</constant> option example</title>
339
+
<programlisting role="php">
340
+
<![CDATA[
341
+
<?php
342
+
var_dump(json_encode(12.0, JSON_PRESERVE_ZERO_FRACTION));
343
+
var_dump(json_encode(12.0));
344
+
?>
345
+
]]>
346
+
</programlisting>
347
+
&example.outputs;
348
+
<screen>
349
+
<![CDATA[
350
+
string(4) "12.0"
351
+
string(2) "12"
352
+
]]>
353
+
</screen>
354
+
</example>
267
355
</para>
268
356
</refsect1>
269
357

...
...
@@ -286,8 +374,7 @@ string(33) "{"0":"foo","2":"baz","3":"blong"}"
286
374
<para>
287
375
Like the reference JSON encoder, <function>json_encode</function> will
288
376
generate JSON that is a simple value (that is, neither an object nor an
289
-
array) if given a <type>string</type>, <type>integer</type>,
290
-
<type>float</type> or <type>boolean</type> as an input
377
+
array) if given a &string;, &integer;, &float; or &boolean; as an input
291
378
<parameter>value</parameter>. While most decoders will accept these values
292
379
as valid JSON, some may not, as the specification is ambiguous on this
293
380
point.
...
...
@@ -311,7 +398,6 @@ string(33) "{"0":"foo","2":"baz","3":"blong"}"
311
398
</para>
312
399
</refsect1>
313
400
</refentry>
314
-

315
401
<!-- Keep this comment at the end of the file
316
402
Local variables:
317
403
mode: sgml
318
404