Skip to content

Commit 1c53323

Browse files
committed
Support EJSON serialization/deserialization
1 parent 048985c commit 1c53323

7 files changed

Lines changed: 678 additions & 1 deletion

File tree

src/Builder/Expression/DeserializeEJSONOperator.php

Lines changed: 70 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Builder/Expression/FactoryTrait.php

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Builder/Expression/SerializeEJSONOperator.php

Lines changed: 78 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MongoDB\Tests\Builder\Expression;
6+
7+
use MongoDB\Builder\Expression;
8+
use MongoDB\Builder\Pipeline;
9+
use MongoDB\Builder\Stage;
10+
use MongoDB\Tests\Builder\PipelineTestCase;
11+
12+
use function MongoDB\object;
13+
14+
/**
15+
* Test $deserializeEJSON expression
16+
*/
17+
class DeserializeEJSONOperatorTest extends PipelineTestCase
18+
{
19+
public function testDeserializeExtendedJSONDocument(): void
20+
{
21+
$pipeline = new Pipeline(
22+
Stage::match(
23+
title: 'Inception',
24+
),
25+
Stage::project(
26+
original: Expression::variable('ROOT'),
27+
serialized: Expression::serializeEJSON(
28+
input: Expression::variable('ROOT'),
29+
),
30+
),
31+
Stage::project(
32+
title: Expression::fieldPath('original.title'),
33+
deserialized: Expression::deserializeEJSON(
34+
input: Expression::fieldPath('serialized'),
35+
),
36+
),
37+
);
38+
39+
$this->assertSamePipeline(Pipelines::DeserializeEJSONDeserializeExtendedJSONDocument, $pipeline);
40+
}
41+
42+
public function testDeserializeSpecificFields(): void
43+
{
44+
$pipeline = new Pipeline(
45+
Stage::match(
46+
title: 'Inception',
47+
),
48+
Stage::project(
49+
title: 1,
50+
serializedMetadata: Expression::serializeEJSON(
51+
input: object(
52+
releaseDate: Expression::fieldPath('released'),
53+
runtime: Expression::fieldPath('runtime'),
54+
rating: Expression::fieldPath('imdb.rating'),
55+
),
56+
),
57+
),
58+
Stage::project(
59+
title: 1,
60+
metadata: Expression::deserializeEJSON(
61+
input: Expression::fieldPath('serializedMetadata'),
62+
),
63+
),
64+
);
65+
66+
$this->assertSamePipeline(Pipelines::DeserializeEJSONDeserializeSpecificFields, $pipeline);
67+
}
68+
69+
public function testParseJSONStringAndDeserialize(): void
70+
{
71+
$pipeline = new Pipeline(
72+
Stage::documents([
73+
object(jsonData: '{"_id":{"$oid":"507f1f77bcf86cd799439011"},"title":"The Matrix","year":{"$numberInt":"1999"},"rating":{"$numberDouble":"8.7"}}'),
74+
]),
75+
Stage::project(
76+
parsed: Expression::convert(
77+
input: Expression::fieldPath('jsonData'),
78+
to: 'object',
79+
),
80+
),
81+
Stage::project(
82+
movie: Expression::deserializeEJSON(
83+
input: Expression::fieldPath('parsed'),
84+
),
85+
),
86+
);
87+
88+
$this->assertSamePipeline(Pipelines::DeserializeEJSONParseJSONStringAndDeserialize, $pipeline);
89+
}
90+
91+
public function testUseOnErrorForErrorHandling(): void
92+
{
93+
$pipeline = new Pipeline(
94+
Stage::project(
95+
result: Expression::deserializeEJSON(
96+
input: Expression::fieldPath('ejsonField'),
97+
onError: object(error: 'Invalid EJSON format'),
98+
),
99+
),
100+
);
101+
102+
$this->assertSamePipeline(Pipelines::DeserializeEJSONUseOnErrorForErrorHandling, $pipeline);
103+
}
104+
}

0 commit comments

Comments
 (0)