Skip to content

Commit 501f388

Browse files
committed
minor #14431 Setting options for json encode/decode in Serializer (dbrumann)
This PR was merged into the 4.4 branch. Discussion ---------- Setting options for json encode/decode in Serializer Adds a paragraph on how to set options for `json_encode`/`json_decode` in Serializer JsonEncoder by passing them as part of the context array. Commits ------- a5d1355 Add context for json encoding/decoding.
2 parents 7e9385d + a5d1355 commit 501f388

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

components/serializer.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,13 @@ The ``JsonEncoder``
810810
~~~~~~~~~~~~~~~~~~~
811811

812812
The ``JsonEncoder`` encodes to and decodes from JSON strings, based on the PHP
813-
:phpfunction:`json_encode` and :phpfunction:`json_decode` functions.
813+
:phpfunction:`json_encode` and :phpfunction:`json_decode` functions. It can be
814+
useful to modify how these functions operate in certain instances by providing
815+
options such as ``JSON_PRESERVE_ZERO_FRACTION``. You can use the serialization
816+
context to pass in these options using the key ``json_encode_options`` or
817+
``json_decode_options`` respectively::
818+
819+
$this->serializer->serialize($data, 'json', ['json_encode_options' => \JSON_PRESERVE_ZERO_FRACTION]);
814820

815821
The ``CsvEncoder``
816822
~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)