Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Composed types (De)serialization support. #6

Merged
merged 2 commits into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/JsonParseNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private function assignFieldValues($result): void {
$additionalData = [];
if (is_a($result, AdditionalDataHolder::class)) {
$isAdditionalDataHolder = true;
$additionalData = $result->getAdditionalData();
$additionalData = $result->getAdditionalData() ?? [];
}
foreach ($this->jsonNode as $key => $value){
$deserializer = $fieldDeserializers[$key] ?? null;
Expand Down
22 changes: 18 additions & 4 deletions src/JsonSerializationWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ public function writeCollectionOfObjectValues(?string $key, ?array $values): voi
/**
* @inheritDoc
*/
public function writeObjectValue(?string $key, $value): void {
if ($value !== null) {
public function writeObjectValue(?string $key, $value, Parsable ...$additionalValuesToMerge): void {
if ($value !== null || count($additionalValuesToMerge) > 0) {
if(!empty($key)) {
$this->writePropertyName($key);
}
Expand All @@ -165,7 +165,21 @@ public function writeObjectValue(?string $key, $value): void {
if ($this->getOnStartObjectSerialization() !== null) {
$this->getOnStartObjectSerialization()($value, $this);
}
$value->serialize($this);
if ($value !== null) {
$value->serialize($this);
}
foreach ($additionalValuesToMerge as $additionalValueToMerge) {
if ($this->getOnBeforeObjectSerialization() !== null) {
call_user_func($this->getOnBeforeObjectSerialization(), $additionalValueToMerge, $this);
}
if ($this->getOnStartObjectSerialization() !== null) {
call_user_func($this->getOnStartObjectSerialization(), $additionalValueToMerge, $this);
}
$additionalValueToMerge->serialize($this);
if ($this->getOnAfterObjectSerialization() !== null) {
call_user_func($this->getOnAfterObjectSerialization(), $additionalValueToMerge);
}
}
if ($this->writer[count($this->writer) - 1] === ',') {
array_pop($this->writer);
}
Expand Down Expand Up @@ -444,4 +458,4 @@ public function writeDateIntervalValue(?string $key, ?DateInterval $value): void
$this->writePropertyValue($key, $val);
}
}
}
}