W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
如果數(shù)組是打包數(shù)組,即空數(shù)組或 鍵從 0 開始,是連續(xù)的,沒有間隙:BSON 數(shù)組。
如果數(shù)組未打包(即具有關(guān)聯(lián)(字符串)鍵,則 鍵不從 0 開始,或者當(dāng)有間隙時(shí):: BSON 對(duì)象
頂級(jí)(根)文檔,始終序列化為 BSON文件。
它們序列化為 BSON 數(shù)組:
[ 8, 5, 2, 3 ] => [ 8, 5, 2, 3 ] [ 0 => 4, 1 => 9 ] => [ 4, 9 ]
這些序列化為 BSON 文檔:
[ 0 => 1, 2 => 8, 3 => 12 ] => { "0" : 1, "2" : 8, "3" : 12 } [ "foo" => 42 ] => { "foo" : 42 } [ 1 => 9, 0 => 10 ] => { "1" : 9, "0" : 10 }
請(qǐng)注意,這五個(gè)示例是完整 文檔,并且僅表示 公文。
如果對(duì)象屬于 stdClass 類,則序列化 作為 BSON 文檔。
如果對(duì)象是實(shí)現(xiàn) MongoDB\BSON\Type 的受支持類,則使用 BSON 該特定類型的序列化邏輯。MongoDB\BSON\Type 實(shí)例(不包括 MongoDB\BSON\Serializable)可能只有 序列化為文檔字段值。嘗試序列化這樣的 對(duì)象作為根文檔將拋出 MongoDB\Driver\Exception\UnexpectedValueException
如果對(duì)象屬于實(shí)現(xiàn) MongoDB\BSON\Type 接口的未知類,則拋出 MongoDB\Driver\Exception\UnexpectedValueException
如果對(duì)象屬于任何其他類,則不實(shí)現(xiàn)任何特殊 接口,序列化為BSON文檔。僅保留公共屬性,忽略受保護(hù)的私有屬性。
如果對(duì)象屬于實(shí)現(xiàn) MongoDB\BSON\Serializable 接口的類,請(qǐng)調(diào)用 MongoDB\BSON\Serializable::bsonSerialize() 并使用 返回的數(shù)組或 stdClass 序列化為 BSON 文檔或數(shù)組。BSON 類型將由以下因素決定:
如果對(duì)象屬于實(shí)現(xiàn) MongoDB\BSON\Persistable 接口的類( 表示 MongoDB\BSON\Serializable),獲取 屬性以與前面段落類似的方式,但也添加了一個(gè)附加屬性__pclass作為二進(jìn)制值,其中子類型和數(shù)據(jù)帶有完全限定的類名 正在序列化的對(duì)象。0x80
將 __pclass 屬性添加到數(shù)組中,或者 MongoDB\BSON\Serializable::bsonSerialize() 返回的對(duì)象,其中 表示它將覆蓋任何__pclass鍵/屬性 MongoDB\BSON\Serializable::bsonSerialize() 返回值。如果要避免此行為并設(shè)置自己的 __pclass 值,則不能實(shí)現(xiàn) MongoDB\BSON\Persistable 和 應(yīng)該直接實(shí)現(xiàn) MongoDB\BSON\Serializable。
<?php
class stdClass {
public $foo = 42;
} // => { "foo" : 42 }
class MyClass {
public $foo = 42;
protected $prot = "wine";
private $fpr = "cheese";
} // => { "foo" : 42 }
class AnotherClass1 implements MongoDB\BSON\Serializable {
public $foo = 42;
protected $prot = "wine";
private $fpr = "cheese";
function bsonSerialize(): array {
return [ 'foo' => $this->foo, 'prot' => $this->prot ];
}
} // => { "foo" : 42, "prot" : "wine" }
class AnotherClass2 implements MongoDB\BSON\Serializable {
public $foo = 42;
function bsonSerialize(): self {
return $this;
}
} // => MongoDB\Driver\Exception\UnexpectedValueException("bsonSerialize() did not return an array or stdClass")
class AnotherClass3 implements MongoDB\BSON\Serializable {
private $elements = [ 'foo', 'bar' ];
function bsonSerialize(): array {
return $this->elements;
}
} // => { "0" : "foo", "1" : "bar" }
class ContainerClass implements MongoDB\BSON\Serializable {
public $things = AnotherClass4 implements MongoDB\BSON\Serializable {
private $elements = [ 0 => 'foo', 2 => 'bar' ];
function bsonSerialize(): array {
return $this->elements;
}
}
function bsonSerialize(): array {
return [ 'things' => $this->things ];
}
} // => { "things" : { "0" : "foo", "2" : "bar" } }
class ContainerClass implements MongoDB\BSON\Serializable {
public $things = AnotherClass5 implements MongoDB\BSON\Serializable {
private $elements = [ 0 => 'foo', 2 => 'bar' ];
function bsonSerialize(): array {
return array_values($this->elements);
}
}
function bsonSerialize(): array {
return [ 'things' => $this->things ];
}
} // => { "things" : [ "foo", "bar" ] }
class ContainerClass implements MongoDB\BSON\Serializable {
public $things = AnotherClass6 implements MongoDB\BSON\Serializable {
private $elements = [ 'foo', 'bar' ];
function bsonSerialize(): array {
return (object) $this->elements;
}
}
function bsonSerialize(): array {
return [ 'things' => $this->things ];
}
} // => { "things" : { "0" : "foo", "1" : "bar" } }
class UpperClass implements MongoDB\BSON\Persistable {
public $foo = 42;
protected $prot = "wine";
private $fpr = "cheese";
function bsonSerialize(): array {
return [ 'foo' => $this->foo, 'prot' => $this->prot ];
}
} // => { "foo" : 42, "prot" : "wine", "__pclass" : { "$type" : "80", "$binary" : "VXBwZXJDbGFzcw==" } }
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: