Skip to content

Commit 3356cda

Browse files
committed
more efficient json serialization
1 parent 4fa1ed3 commit 3356cda

File tree

1 file changed

+24
-33
lines changed
  • template/versions/types/serialization/json/serialize

1 file changed

+24
-33
lines changed

template/versions/types/serialization/json/serialize/default.php

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -35,43 +35,39 @@ public function jsonSerialize(): mixed
3535

3636
if ($type->isCommentContainer() && !$type->hasCommentContainerParent()) : ?>
3737
if ([] !== ($vs = $this->_getFHIRComments())) {
38-
$out->{<?php echo PHPFHIR_CLASSNAME_CONSTANTS; ?>::JSON_FIELD_FHIR_COMMENTS} = $vs;
38+
$out->fhir_comments = $vs;
3939
}
4040
<?php endif;
4141
foreach ($type->getProperties()->getIterator() as $property) :
4242
$propConst = $property->getFieldConstantName();
4343
$propConstExt = $property->getFieldConstantExtensionName();
44-
$getter = $property->getGetterName();
4544

4645
if ($property->getOverloadedProperty()) :
4746
continue;
4847
endif;
4948
$propertyType = $property->getValueFHIRType();
5049
if ($propertyType->getKind()->isOneOf(TypeKindEnum::PRIMITIVE, TypeKindEnum::LIST)) :
5150
if ($property->isCollection()) : ?>
52-
if ([] !== ($vs = $this-><?php echo $getter; ?>())) {
53-
$out->{self::<?php echo $propConst; ?>} = $vs;
51+
if (isset($this-><?php echo $property->getName(); ?>) && [] !== $this-><?php echo $property->getName(); ?>) {
52+
$out-><?php echo $property->getName(); ?> = $this-><?php echo $property->getName(); ?>;
5453
}
5554
<?php else : ?>
56-
if (null !== ($v = $this-><?php echo $getter; ?>())) {
57-
$out->{self::<?php echo $propConst; ?>} = $v;
55+
if (isset($this-><?php echo $property->getName(); ?>)) {
56+
$out-><?php echo $property->getName(); ?> = $this-><?php echo $property->getName(); ?>;
5857
}
5958
<?php endif;
6059

6160
elseif ($propertyType->isValueContainer() || $propertyType->getKind() === TypeKindEnum::PRIMITIVE_CONTAINER || $propertyType->hasPrimitiveContainerParent()) :
6261
$propTypeClassname = $property->getValueFHIRType()->getClassName();
6362

6463
if ($property->isCollection()) : ?>
65-
if ([] !== ($vs = $this-><?php echo $getter; ?>())) {
64+
if (isset($this-><?php echo $property->getName(); ?>) && [] !== $this-><?php echo $property->getName(); ?>) {
6665
$vals = [];
6766
$exts = [];
68-
foreach ($vs as $v) {
69-
if (null === $v) {
70-
continue;
71-
}
67+
foreach (<?php echo $property->getName(); ?> as $v) {
7268
$val = $v->getValue();
7369
$ext = $v->jsonSerialize();
74-
unset($ext->{<?php echo $propTypeClassname; ?>::FIELD_VALUE});
70+
unset($ext->value);
7571
if (null !== $val) {
7672
$vals[] = $val;
7773
}
@@ -80,49 +76,44 @@ public function jsonSerialize(): mixed
8076
}
8177
}
8278
if ([] !== $vals) {
83-
$out->{self::<?php echo $propConst; ?>} = $vals;
79+
$out-><?php echo $property->getName(); ?> = $vals;
8480
}
8581
if (count((array)$ext) > 0) {
86-
$out->{self::<?php echo $propConstExt; ?>} = $exts;
82+
$out-><?php echo $property->getExtName(); ?> = $exts;
8783
}
8884
}
8985
<?php else : ?>
90-
if (null !== ($v = $this-><?php echo $getter; ?>())) {
91-
if (null !== ($val = $v->getValue())) {
92-
$out->{self::<?php echo $propConst; ?>} = $val;
86+
if (isset($this-><?php echo $property->getName(); ?>)) {
87+
if (null !== ($val = $this-><?php echo $property->getName(); ?>->getValue())) {
88+
$out-><?php echo $property->getName(); ?> = $val;
9389
}
94-
$ext = $v->jsonSerialize();
95-
unset($ext->{<?php echo $propTypeClassname; ?>::FIELD_VALUE});
90+
$ext = $this-><?php echo $property->getName(); ?>->jsonSerialize();
91+
unset($ext->value);
9692
if (count((array)$ext) > 0) {
97-
$out->{self::<?php echo $propConstExt; ?>} = $ext;
93+
$out-><?php echo $property->getExtName(); ?> = $ext;
9894
}
9995
}
10096
<?php endif;
10197

10298
else :
10399
if ($property->isCollection()) : ?>
104-
if ([] !== ($vs = $this-><?php echo $getter; ?>())) {
105-
$out->{self::<?php echo $propConst; ?>} = [];
106-
foreach($vs as $v) {
107-
$out->{self::<?php echo $propConst; ?>}[] = $v;
108-
}
100+
if (isset($this-><?php echo $property->getName(); ?>) && [] !== $this-><?php echo $property->getName(); ?>) {
101+
$out-><?php echo $property->getName(); ?> = $this-><?php echo $property->getName(); ?>;
109102
}
110103
<?php else : ?>
111-
if (null !== ($v = $this-><?php echo $getter; ?>())) {
112-
$out->{self::<?php echo $propConst; ?>} = $v;
104+
if (isset($this-><?php echo $property->getName(); ?>)) {
105+
$out-><?php echo $property->getName(); ?> = $this-><?php echo $property->getName(); ?>;
113106
}
114107
<?php endif;
115108
endif;
116109
endforeach;
117110
if ($type->isCommentContainer() && !$type->hasCommentContainerParent()) : ?>
118111
if ([] !== ($vs = $this->_getFHIRComments())) {
119-
$out->{<?php echo PHPFHIR_CLASSNAME_CONSTANTS; ?>::JSON_FIELD_FHIR_COMMENTS} = $vs;
112+
$out->fhir_comments = $vs;
120113
}
121-
<?php endif; ?>
122-
123-
<?php if ($type->isContainedType()) : ?>
124-
$out->{<?php echo PHPFHIR_CLASSNAME_CONSTANTS; ?>::JSON_FIELD_RESOURCE_TYPE} = $this->_getResourceType();
125-
114+
<?php endif;
115+
if ($type->isContainedType()) : ?>
116+
$out->resourceType = $this->_getResourceType();
126117
<?php endif; ?>
127118
return $out;
128119
}

0 commit comments

Comments
 (0)