Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony Serializer doesn't convert an array of objects from camelCase to snake_case

Can someone help me with this? I'm trying to serialize an object to json using the Symfony Serializer, it does convert object to json but it doesnt convert an array of objects from camelCase to snake_case.

Im currently using the default Serializer with Symfony 3.3

Below is my code.

app/config/config.yml

 framework: 
  serializer:  
    enabled: true
    name_converter: 'serializer.name_converter.camel_case_to_snake_case'

app/config/services.yml

 services:
    get_set_method_normalizer:
        class:Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer
        public: true
        tags: [serializer.normalizer]

Persons.php

class Person{
     private $firstName;
     private $lastName;
     private $email;

     //setters and getters
}

PersonController.php

class PersonController extends Controller {
     /**
      * @Route("api/person")
      * @Method("POST")
      */
     public function person(){
         $person = new Person();
          $person->setFirstName("test");
          $person->setLastName("test");
          $person->setEmail("[email protected]");

         $person1 = new Person();
         $person1->setFirstName("test 1");
          $person1->setLastName("test 1");
          $person1->setEmail("[email protected]");
         $arr = array($person, $person1);
         $serializer = $this->get("serializer");
         return new Response($serializer->serialize($arr,"json"));
     }

}

Below is the current output.

[
  {
     "firstName":"test",
     "lastName":"test"
     "email":"[email protected]"
  },
  {
     "firstName":"test 1",
     "lastName":"test 1"
     "email":"[email protected]"
  }
]

Expected output would be:

 [
   {
      "first_name":"test",
      "last_name":"test",
      "email":"[email protected]"
   },
   {
      "first_name":"test 1",
      "last_name":"test 1",
      "email":"[email protected]"
   }
]
like image 657
blitzen12 Avatar asked Nov 25 '17 15:11

blitzen12


3 Answers

For symfony 4 this worked for me:

Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer:
    public: true
    arguments: ['@serializer.mapping.class_metadata_factory', '@serializer.name_converter.camel_case_to_snake_case']
    tags: [serializer.normalizer]

Put that code inside your app/config/services.yaml, inside the "services" section.

like image 70
Macr1408 Avatar answered Oct 02 '22 18:10

Macr1408


try explicitly pass arguments for your service:

get_set_method_normalizer:
        class:Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer
        public: true
        arguments: ['@serializer.mapping.class_metadata_factory', '@serializer.name_converter.camel_case_to_snake_case']
        tags: [serializer.normalizer]
like image 42
FORTIS Avatar answered Oct 02 '22 19:10

FORTIS


It is easier to add an alias for camel_case_to_snake_case

services:
    ...
    Symfony\Component\Serializer\NameConverter\NameConverterInterface: '@serializer.name_converter.camel_case_to_snake_case'
like image 37
fullpipe Avatar answered Oct 02 '22 17:10

fullpipe