json - JMS Serialize ArrayCollection as an object -


i'am using jms serializer. jsonserializer gives me incorrect array format when works doctrine arraycollection types. spected results should follow format [ {}, {} ] gives me { 1: {}, 2: {} }.

additional information scenario. occurs when try serialize object contains object contains arraycollection , arraycollection includes first level object. example:

{      "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 not occurs if serialize user entity directly:

{      "username":"user1",    "password":"123",    "oid":2,    "name":"rafael",    "lastname":"jimenez"    "announcements":[         {            "description":"cargar cajas la guardilla",          "date":"1434145921000",          "oid":1       },       {            "description":"cuidar de anciano moribundo",          "date":"1434745921000",          "oid":3       },       {            "description":"reparar ordenador",          "date":"1434145921000",          "oid":5       }    ] } 

any clue?

you need reset keys in arraycollection array:

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

as @stof sad on github:

if array not indexed sequence 0 count($array) - 1, getting js object rather array expected behaviour, because how associative arrays need converted json. , if keys not such sequence, array map, not list.

take examples:

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

Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -