BSON으로 직렬화
배열
배열이 묶음 배열인 경우 — 즉, 빈 배열 또는 키가 0에서 시작하고 간격 없이 순차적인 경우: BSON 배열.
배열이 패킹되지 않은 경우 — 즉 연관(문자열) 키가 있는 경우 키가 0에서 시작하지 않거나 gaps:: BSON 객체가 있는 경우
최상위(루트) 문서는 항상 BSON 문서로 직렬화됩니다.
Examples
다음은 BSON 배열로 직렬화됩니다.
[ 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 }
다섯 가지 예는 전체 문서에서 발췌한 것이며 문서 내에서 단 하나의 값만 나타냅니다.
객체
객체가 stdClass 클래스인 경우 BSON 문서로 직렬화합니다.
개체가 MongoDB\BSON\Type을 구현하는 지원되는 클래스인 경우 해당 특정 유형에 대해 BSON 직렬화 논리를 사용하십시오. MongoDB\BSON\Type 인스턴스(MongoDB\BSON\Serializable 제외는 문서 필드 값으로만 직렬화할 수 있습니다. 이러한 개체를 루트 문서로 직렬화하려고 하면 MongoDB\Driver\Exception\UnexpectedValueException이 발생합니다.
개체가 MongoDB\BSON\Type 인터페이스를 구현하는 알 수 없는 클래스의 경우 MongoDB\Driver\Exception\UnexpectedValueException을 throw합니다.
객체가 다른 클래스에 속한다면 특별한 인터페이스를 구현하지 않고 BSON 문서로 직렬화합니다. 공개 속성만 유지하고 보호 속성과 비공개 속성은 무시합니다.
객체가 MongoDB\BSON\Serializable 인터페이스를 구현하는 클래스의 경우 MongoDB\BSON\Serializable::bsonSerialize()를 호출하고 반환된 배열 또는 stdClass를 사용하여 BSON 문서 또는 배열로 직렬화합니다. BSON 유형은 다음에 의해 결정됩니다.
- 루트 문서는 BSON 문서로 직렬화되어야 합니다.
- MongoDB\BSON\Persistable 개체는 BSON 문서로 직렬화되어야 합니다.
- MongoDB\BSON\Serializable::bsonSerialize()가 묶음 배열을 반환하는 경우 BSON 배열로 직렬화합니다.
- MongoDB\BSON\Serializable::bsonSerialize()가 압축되지 않은 배열 또는 stdClass를 반환하는 경우 BSON 문서로 직렬화합니다.
- MongoDB\BSON\Serializable::bsonSerialize()가 배열이나 stdClass를 반환하지 않으면 MongoDB\Driver\Exception\UnexpectedValueException 예외를 던집니다.
개체가 MongoDB\BSON\Persistable 인터페이스(MongoDB\BSON\Serializable을 의미함)를 구현하는 클래스의 경우 이전 단락과 유사한 방식으로 속성을 가져오고 추가 속성 __pclass를 Binary 값으로 추가합니다. , 하위 유형이 0x80
이고 직렬화되는 개체의 정규화된 클래스 이름이 포함된 데이터입니다.
__pclass 속성은 MongoDB\BSON\Serializable::bsonSerialize()에 의해 반환된 배열 또는 객체에 추가됩니다. 즉, MongoDB\BSON\Serializable::bsonSerialize() 반환 값의 __pclass 키/속성을 덮어씁니다. 이 동작을 피하고 고유한 __pclass 값을 설정하려면 MongoDB\BSON\Persistable을 구현하지 말고 대신 MongoDB\BSON\Serializable을 직접 구현해야 합니다.
Examples
<?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() {
return [ 'foo' => $this->foo, 'prot' => $this->prot ];
}
} // => { "foo" : 42, "prot" : "wine" }
class AnotherClass2 implements MongoDB\BSON\Serializable {
public $foo = 42;
function bsonSerialize() {
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() {
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() {
return $this->elements;
}
}
function bsonSerialize() {
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() {
return array_values($this->elements);
}
}
function bsonSerialize() {
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() {
return (object) $this->elements;
}
}
function bsonSerialize() {
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() {
return [ 'foo' => $this->foo, 'prot' => $this->prot ];
}
} // => { "foo" : 42, "prot" : "wine", "__pclass" : { "$type" : "80", "$binary" : "VXBwZXJDbGFzcw==" } }