79623785

Date: 2025-05-15 16:06:37
Score: 0.5
Natty:
Report link

As @sergey-fedotov suggested, you need a custom implementation.

Give the code below a try:


use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

class StdClassNormalizer implements NormalizerInterface
{
    public function normalize($object, string $format = null, array $context = []): array|\stdClass
    {
        if ($object instanceof \stdClass && empty(get_object_vars($object))) {
            return new \stdClass();
        }
        return (array) $object;
    }

    public function supportsNormalization($data, string $format = null, array $context = []): bool
    {
        return $data instanceof \stdClass;
    }
}

Don't forget to register StdClassNormalizer as a Service.

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @sergey-fedotov
  • Low reputation (0.5):
Posted by: yaoviametepe