Skip to content

Commit e612435

Browse files
authored
Merge pull request #85 from fabfischer/feature/nullable-properties
Export properties with null as a value
2 parents c1f3b05 + 7a200bc commit e612435

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/Structure/ClassStructureTrait.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,33 @@ public static function create()
9292

9393
protected $__validateOnSet = true; // todo skip validation during import
9494

95+
/**
96+
* @return \stdClass
97+
*/
9598
public function jsonSerialize()
9699
{
97100
$result = new \stdClass();
98101
$schema = static::schema();
102+
$classname = $schema->getObjectItemClass();
103+
$classReference = (class_exists($classname)) ? new $classname() : new \stdClass();
99104
$properties = $schema->getProperties();
100105
if (null !== $properties) {
101106
foreach ($properties->getDataKeyMap() as $propertyName => $dataName) {
102107
$value = $this->$propertyName;
103-
if ((null !== $value) || array_key_exists($propertyName, $this->__arrayOfData)) {
108+
$types = ($schema->getProperty($propertyName) instanceof Schema)
109+
? $schema->getProperty($propertyName)->type
110+
: null;
111+
if (
112+
(
113+
null !== $value
114+
||
115+
($value === null
116+
&& is_array($types)
117+
&& in_array('null', $types)
118+
)
119+
)
120+
|| array_key_exists($propertyName, $this->__arrayOfData)
121+
) {
104122
$result->$dataName = $value;
105123
}
106124
}
@@ -154,4 +172,4 @@ public function validate()
154172
{
155173
static::schema()->out($this);
156174
}
157-
}
175+
}

0 commit comments

Comments
 (0)