JMS Serialize ArrayCollection as an Object

I am using JMS Serializer. JsonSerializer gives me the wrong array format when it works with Doctrine ArrayCollection types. The results should be consistent with the format [ {}, {} ], but it gives me { 1: {}, 2: {} }.

Additional information about this scenario. This only happens when you try to serialize an object that contains an object containing an ArrayCollection, and ArrayCollection includes an object of the first level. For instance:

{  
   "description":"Text provided",
   "date":"1434145921000",
   "oid":1,
   "userCreator":{  
      "username":"name123",
      "password":"psw",
      "oid":2,
      "name":"the-name",
      "lastname":"the-lasname",
      "announcements":{  
         "1":{  
            "description":"More text",
            "date":"1434745921000",
            "oid":3
         },
         "2":{  
            "description":"Reparar ordenador",
            "date":"1434145921000",
            "oid":5
         }
      }
   }
}

However, this does not happen if I serialize the user object directly:

{  
   "username":"user1",
   "password":"123",
   "oid":2,
   "name":"Rafael",
   "lastname":"Jimenez"
   "announcements":[  
      {  
         "description":"Cargar cajas a la guardilla",
         "date":"1434145921000",
         "oid":1
      },
      {  
         "description":"Cuidar de anciano moribundo",
         "date":"1434745921000",
         "oid":3
      },
      {  
         "description":"Reparar ordenador",
         "date":"1434145921000",
         "oid":5
      }
   ]
}

Any clue?

+4
source share
2 answers

In the ArrayCollection array you need to reset:

$associative = new ArrayCollection([0 => 1, 2 => 1]);
$list = new ArrayCollection($associative->getValues());

Like @stof sad on github:

0 count ($ array) - 1, JS-, , , JSON. , - , .

:

php > echo json_encode([0 => 1, 1 => 1]);
[1,1]
php > echo json_encode([0 => 1, 2 => 1]);
{"0":1,"2":1}
+2

, , , JS Array Object.

null, , , , .

array:28 [
  0 => "High Fashion"
  1 => "Contemporary"
  2 => "Streetwear"
  3 => null
  4 => "Grooming"
]

$arr[3] null, . . = > .

:

, array_values(array_filter($resources))

0

All Articles