1

I make a REST API on Symfony4, So I would like to Serialize my Entity with the default serializer of Symfony4.

But my entities have unusual attribute names that make serializer give me bad result.

I tried to implement NameConverterInterface and also tried CamelCaseToSnakeCaseNameConverter without a good result...

Every entity on my application have this kind of attribute so a solution with @annotation can't help me

class Product implements EntityInterface
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer", name="PROD_PKEY")
     */
    private $PROD_PKEY;

    /**
     * @ORM\Column(type="string", length=50)
     */
    private $PROD_Name;

    /**
     * @ORM\Column(type="string", length=50)
     */
    private $PROD_Code;

And how I use the serializer :

$product = new Product();
$product->setPRODName("Name");
$product->setPRODCode("Code");

$json = $this->serializer->serialize($product, 'json');

The content of $json is :

{
    "pRODName": "Name",
    "pRODCode": "Code",
}

but I expect somethings like this :

{
    "PROD_Name": "Name",
    "PROD_Code": "Code",
}

Simply equal to my attributes names in my entity, I don't understand why first letter get lowercase and my underscore get out...

Thanks for your help !

Clavat
  • 107
  • 1
  • 8
  • If you use a framework like Symfony you should follow its [Naming Conventions](https://symfony.com/doc/current/contributing/code/standards.html#naming-conventions) and anyway your way could produce many more unexpected behaviors (and headaches). I hope you can change that naming type. Good luck. – gp_sflover Apr 19 '19 at 15:43
  • `unusual attribute names` - There is always time to improve so please take your time! – BentCoder Apr 19 '19 at 21:27

2 Answers2

2

In Symfony you can implement a custom NameConverter to transform fields names in your json representation.

Something along these lines should do the trick:

<?php namespace App\Service;

use Symfony\Component\Serializer\NameConverter\AdvancedNameConverterInterface;

class YourCustomNameConverter implements AdvancedNameConverterInterface
{
    public function normalize($propertyName, string $class = null, string $format = null, array $context = [])
    {
        preg_match('/^([a-z]?[A-Z]+)([A-Z]{1}[_a-zA-Z]+)$/', $propertyName, $matches);
        if (strstr($propertyName, 'PKEY')) {
            return ucfirst(substr_replace($propertyName, '_', -4, 0));
        } elseif (count($matches)) {
            array_shift($matches);
            $matches[0] = ucfirst($matches[0]);

            return implode('_', $matches);
        } else {
            return $propertyName;
        }
    }

    public function denormalize($propertyName, string $class = null, string $format = null, array $context = [])
    {
        return $propertyName;
    }
}
Atrakeur
  • 4,126
  • 3
  • 17
  • 22
0

I think you might need to create a custom serializer, I often use jmsserializer bundle and had no issues with it

How to use JMSSerializer with symfony 4.2

https://symfony.com/doc/current/serializer/custom_normalizer.html

Omar Makled
  • 1,638
  • 1
  • 19
  • 17
  • 1
    Yes i seen JMSSerializer, but why use an external dependence if the framework can do the job ? I don't understand why symfony serializer transform my attribute name this way ... – Clavat Apr 19 '19 at 15:27