[Serializer] Add support for stdClass
This commit is contained in:
parent
33e2735d14
commit
d7bca80007
@ -262,7 +262,7 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
|
|||||||
array_unshift($attributes, $mapping->getTypeProperty());
|
array_unshift($attributes, $mapping->getTypeProperty());
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($context['cache_key']) {
|
if ($context['cache_key'] && \stdClass::class !== $class) {
|
||||||
$this->attributesCache[$key] = $attributes;
|
$this->attributesCache[$key] = $attributes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,6 +62,10 @@ class ObjectNormalizer extends AbstractObjectNormalizer
|
|||||||
*/
|
*/
|
||||||
protected function extractAttributes(object $object, string $format = null, array $context = [])
|
protected function extractAttributes(object $object, string $format = null, array $context = [])
|
||||||
{
|
{
|
||||||
|
if (\stdClass::class === \get_class($object)) {
|
||||||
|
return array_keys((array) $object);
|
||||||
|
}
|
||||||
|
|
||||||
// If not using groups, detect manually
|
// If not using groups, detect manually
|
||||||
$attributes = [];
|
$attributes = [];
|
||||||
|
|
||||||
|
@ -696,6 +696,20 @@ class ObjectNormalizerTest extends TestCase
|
|||||||
$normalizer->normalize($obj, 'any')
|
$normalizer->normalize($obj, 'any')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testNormalizeStdClass()
|
||||||
|
{
|
||||||
|
$o1 = new \stdClass();
|
||||||
|
$o1->foo = 'f';
|
||||||
|
$o1->bar = 'b';
|
||||||
|
|
||||||
|
$this->assertSame(['foo' => 'f', 'bar' => 'b'], $this->normalizer->normalize($o1));
|
||||||
|
|
||||||
|
$o2 = new \stdClass();
|
||||||
|
$o2->baz = 'baz';
|
||||||
|
|
||||||
|
$this->assertSame(['baz' => 'baz'], $this->normalizer->normalize($o2));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ProxyObjectDummy extends ObjectDummy
|
class ProxyObjectDummy extends ObjectDummy
|
||||||
|
Reference in New Issue
Block a user