2024 年 PHP Conference Japan

MongoDB\BSON\toCanonicalExtendedJSON

(mongodb >=1.3.0)

MongoDB\BSON\toCanonicalExtendedJSON傳回 BSON 值的規範擴展 JSON 表示法

警告

此函數自擴充功能版本 1.20.0 起已遭棄用,並將於 2.0 版中移除。應用程式應改用 MongoDB\BSON\Document::toCanonicalExtendedJSON()

說明

MongoDB\BSON\toCanonicalExtendedJSON(字串 $bson): 字串

將 BSON 字串轉換為其 » 標準擴展 JSON 表示法。標準格式優先考慮類型保真度,但會犧牲簡潔的輸出,最適合用於產生可轉換回 BSON 而不損失任何類型資訊的輸出(例如,數值類型將保持區分)。

參數

bson (字串)

要轉換的 BSON 值。

回傳值

已轉換的 JSON 值。

錯誤/例外

範例

範例 #1 MongoDB\BSON\toCanonicalExtendedJSON() 範例

<?php

$documents
= [
[
'null' => null ],
[
'boolean' => true ],
[
'string' => 'foo' ],
[
'int32' => 123 ],
[
'int64' => 4294967295 ],
[
'double' => 1.0, ],
[
'nan' => NAN ],
[
'pos_inf' => INF ],
[
'neg_inf' => -INF ],
[
'array' => [ 'foo', 'bar' ]],
[
'document' => [ 'foo' => 'bar' ]],
[
'oid' => new MongoDB\BSON\ObjectId('56315a7c6118fd1b920270b1') ],
[
'dec128' => new MongoDB\BSON\Decimal128('1234.5678') ],
[
'binary' => new MongoDB\BSON\Binary('foo', MongoDB\BSON\Binary::TYPE_GENERIC) ],
[
'date' => new MongoDB\BSON\UTCDateTime(1445990400000) ],
[
'timestamp' => new MongoDB\BSON\Timestamp(1234, 5678) ],
[
'regex' => new MongoDB\BSON\Regex('pattern', 'i') ],
[
'code' => new MongoDB\BSON\Javascript('function() { return 1; }') ],
[
'code_ws' => new MongoDB\BSON\Javascript('function() { return a; }', ['a' => 1]) ],
[
'minkey' => new MongoDB\BSON\MinKey ],
[
'maxkey' => new MongoDB\BSON\MaxKey ],
];

foreach (
$documents as $document) {
$bson = MongoDB\BSON\fromPHP($document);
echo
MongoDB\BSON\toCanonicalExtendedJSON($bson), "\n";
}

?>

上述範例將輸出

{ "null" : null }
{ "boolean" : true }
{ "string" : "foo" }
{ "int32" : { "$numberInt" : "123" } }
{ "int64" : { "$numberLong" : "4294967295"} }
{ "double" : { "$numberDouble" : "1.0" } }
{ "nan" : { "$numberDouble" : "NaN" } }
{ "pos_inf" : { "$numberDouble" : "Infinity" } }
{ "neg_inf" : { "$numberDouble" : "-Infinity" } }
{ "array" : [ "foo", "bar" ] }
{ "document" : { "foo" : "bar" } }
{ "oid" : { "$oid" : "56315a7c6118fd1b920270b1" } }
{ "dec128" : { "$numberDecimal" : "1234.5678" } }
{ "binary" : { "$binary" : { "base64": "Zm9v", "subType" : "00" } } }
{ "date" : { "$date" : { "$numberLong" : "1445990400000" } } }
{ "timestamp" : { "$timestamp" : { "t" : 5678, "i" : 1234 } } }
{ "regex" : { "$regularExpression" : { "pattern" : "pattern", "options" : "i" } } }
{ "code" : { "$code" : "function() { return 1; }" } }
{ "code_ws" : { "$code" : "function() { return a; }", "$scope" : { "a" : { "$numberInt" : "1" } } } }
{ "minkey" : { "$minKey" : 1 } }
{ "maxkey" : { "$maxKey" : 1 } }

另請參閱

新增註記

使用者貢獻的註記

此頁面沒有使用者貢獻的註記。
To Top