Skip to content

Commit

Permalink
more efficient json serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
dcarbone committed Jan 10, 2025
1 parent 4fa1ed3 commit 3356cda
Showing 1 changed file with 24 additions and 33 deletions.
57 changes: 24 additions & 33 deletions template/versions/types/serialization/json/serialize/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,43 +35,39 @@ public function jsonSerialize(): mixed

if ($type->isCommentContainer() && !$type->hasCommentContainerParent()) : ?>
if ([] !== ($vs = $this->_getFHIRComments())) {
$out->{<?php echo PHPFHIR_CLASSNAME_CONSTANTS; ?>::JSON_FIELD_FHIR_COMMENTS} = $vs;
$out->fhir_comments = $vs;
}
<?php endif;
foreach ($type->getProperties()->getIterator() as $property) :
$propConst = $property->getFieldConstantName();
$propConstExt = $property->getFieldConstantExtensionName();
$getter = $property->getGetterName();

if ($property->getOverloadedProperty()) :
continue;
endif;
$propertyType = $property->getValueFHIRType();
if ($propertyType->getKind()->isOneOf(TypeKindEnum::PRIMITIVE, TypeKindEnum::LIST)) :
if ($property->isCollection()) : ?>
if ([] !== ($vs = $this-><?php echo $getter; ?>())) {
$out->{self::<?php echo $propConst; ?>} = $vs;
if (isset($this-><?php echo $property->getName(); ?>) && [] !== $this-><?php echo $property->getName(); ?>) {
$out-><?php echo $property->getName(); ?> = $this-><?php echo $property->getName(); ?>;
}
<?php else : ?>
if (null !== ($v = $this-><?php echo $getter; ?>())) {
$out->{self::<?php echo $propConst; ?>} = $v;
if (isset($this-><?php echo $property->getName(); ?>)) {
$out-><?php echo $property->getName(); ?> = $this-><?php echo $property->getName(); ?>;
}
<?php endif;

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

if ($property->isCollection()) : ?>
if ([] !== ($vs = $this-><?php echo $getter; ?>())) {
if (isset($this-><?php echo $property->getName(); ?>) && [] !== $this-><?php echo $property->getName(); ?>) {
$vals = [];
$exts = [];
foreach ($vs as $v) {
if (null === $v) {
continue;
}
foreach (<?php echo $property->getName(); ?> as $v) {
$val = $v->getValue();
$ext = $v->jsonSerialize();
unset($ext->{<?php echo $propTypeClassname; ?>::FIELD_VALUE});
unset($ext->value);
if (null !== $val) {
$vals[] = $val;
}
Expand All @@ -80,49 +76,44 @@ public function jsonSerialize(): mixed
}
}
if ([] !== $vals) {
$out->{self::<?php echo $propConst; ?>} = $vals;
$out-><?php echo $property->getName(); ?> = $vals;
}
if (count((array)$ext) > 0) {
$out->{self::<?php echo $propConstExt; ?>} = $exts;
$out-><?php echo $property->getExtName(); ?> = $exts;
}
}
<?php else : ?>
if (null !== ($v = $this-><?php echo $getter; ?>())) {
if (null !== ($val = $v->getValue())) {
$out->{self::<?php echo $propConst; ?>} = $val;
if (isset($this-><?php echo $property->getName(); ?>)) {
if (null !== ($val = $this-><?php echo $property->getName(); ?>->getValue())) {
$out-><?php echo $property->getName(); ?> = $val;
}
$ext = $v->jsonSerialize();
unset($ext->{<?php echo $propTypeClassname; ?>::FIELD_VALUE});
$ext = $this-><?php echo $property->getName(); ?>->jsonSerialize();
unset($ext->value);
if (count((array)$ext) > 0) {
$out->{self::<?php echo $propConstExt; ?>} = $ext;
$out-><?php echo $property->getExtName(); ?> = $ext;
}
}
<?php endif;

else :
if ($property->isCollection()) : ?>
if ([] !== ($vs = $this-><?php echo $getter; ?>())) {
$out->{self::<?php echo $propConst; ?>} = [];
foreach($vs as $v) {
$out->{self::<?php echo $propConst; ?>}[] = $v;
}
if (isset($this-><?php echo $property->getName(); ?>) && [] !== $this-><?php echo $property->getName(); ?>) {
$out-><?php echo $property->getName(); ?> = $this-><?php echo $property->getName(); ?>;
}
<?php else : ?>
if (null !== ($v = $this-><?php echo $getter; ?>())) {
$out->{self::<?php echo $propConst; ?>} = $v;
if (isset($this-><?php echo $property->getName(); ?>)) {
$out-><?php echo $property->getName(); ?> = $this-><?php echo $property->getName(); ?>;
}
<?php endif;
endif;
endforeach;
if ($type->isCommentContainer() && !$type->hasCommentContainerParent()) : ?>
if ([] !== ($vs = $this->_getFHIRComments())) {
$out->{<?php echo PHPFHIR_CLASSNAME_CONSTANTS; ?>::JSON_FIELD_FHIR_COMMENTS} = $vs;
$out->fhir_comments = $vs;
}
<?php endif; ?>

<?php if ($type->isContainedType()) : ?>
$out->{<?php echo PHPFHIR_CLASSNAME_CONSTANTS; ?>::JSON_FIELD_RESOURCE_TYPE} = $this->_getResourceType();

<?php endif;
if ($type->isContainedType()) : ?>
$out->resourceType = $this->_getResourceType();
<?php endif; ?>
return $out;
}
Expand Down

0 comments on commit 3356cda

Please sign in to comment.