serializer
serializer copied to clipboard
@Inline annotation prevent serialization of scalar properties
| Q | A |
|---|---|
| Bug report? | yes |
| Feature request? | no |
| BC Break report? | no |
| RFC? | no |
SerializationVisitor simply skip scalars. why?
https://github.com/schmittjoh/serializer/blob/0edb562707c032156516fe94d27d0f685032573f/src/JsonSerializationVisitor.php#L146
@Inline is meant to "inline" an array as if the elements were props of the current object.
Using @inline on a scalar makes no sense IMO.
serializer should not silently skip property. throw an exception or rise an error.
It would be great if you could provide a fix for this DX issue. I think that a good place to start is https://github.com/schmittjoh/serializer/blob/0edb562707c032156516fe94d27d0f685032573f/src/Metadata/Driver/AnnotationDriver.php#L272
you could add a check that verifies if the prop is marked as inline but is not an array (or arrayCollection)
Annotation Driver cannot handle custom types.
let me explain the situation.
if u have custom type, u must use @Inline annotaion. if in the end u have scalar u must not use @Inline annotation.
for now i wrote
public function serializeToJson(JsonSerializationVisitor $visitor, $data, array $type, Context $context)
{
if (('NestedArray' === $type['name']) && is_scalar($data)) {
foreach ($context->getMetadataStack() as $metadata) {
if (($metadata instanceof PropertyMetadata) && ($type['name'] === $metadata->type['name'])) {
$metadata->inline = false;
break;
}
}
}
return $data;
}
I have the same problem. The class is this: https://github.com/ujamii/openimmo/blob/master/src/API/DistanzenSport.php and the structure is given by the XML we get: <distanzen_sport distanz_zu_sport="SEE">15.0</distanzen_sport>.
Now, when serializing this to XML, everything is fine, but the inline scalar value is missing. I did not get the proposed solution from above to work, I tried this registration:
/**
* @return array|array[]
*/
public static function getSubscribingMethods()
{
return [
[
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
'format' => 'json',
'type' => DistanzenSport::class,
'method' => 'serializeToJson',
]
];
}
I did not find any documentation for the type property except for the DateTime example. What value is expected here, if it for a certain property of a certain class only? ("float" did not work either)
I use the https://symfony.com/doc/current/components/serializer.html now, this works as expected with my objects.