php8.5
Home/ Manual/ bson / packedarray/ MongoDB\BSON\PackedArray::toCanonicalExtendedJSON

MongoDB\BSON\PackedArray::toCanonicalExtendedJSON

PHP function Edit on GitHub ✎

(mongodb >=1.20.0)

Returns the Canonical Extended JSON representation of the BSON array

Description

MongoDB\BSON\PackedArray::toCanonicalExtendedJSON(): string

Converts the BSON array to its Canonical Extended JSON representation. The canonical format prefers type fidelity at the expense of concise output and is most suited for producing output that can be converted back to BSON without any loss of type information (e.g. numeric types will remain differentiated).

Parameters Parameters

Return Values

Returns a string containing the Canonical Extended JSON representation of the BSON array.

Examples

MongoDB\BSON\PackedArray::toCanonicalExtendedJSON example

php
<?php

$array = [
    'foo',
    123,
    4294967295,
    new MongoDB\BSON\ObjectId('56315a7c6118fd1b920270b1'),
];

$packedArray = MongoDB\BSON\PackedArray::fromPHP($array);
echo $packedArray->toCanonicalExtendedJSON(), "\n";

?>

The above example will output:

output
[ "foo", { "$numberInt" : "123" }, { "$numberLong" : "4294967295" }, { "$oid" : "56315a7c6118fd1b920270b1" } ]

See Also

Source: reference/mongodb/bson/packedarray/tocanonicalextendedjson.xml · from the official PHP manual (php/doc-en)