2

I am trying to retrieve all records and display them in a JSON file.

My current function retrieves all Events that belong to a specific user.

/**
 * create json files from doctrine/mongo
 * @Route("/createjson", name="createjson")
 */
public function createJson()
{
    // check user authentication
    $this->denyAccessUnlessGranted('ROLE_USER', null, 'Unable to access this page!');
    $dm = $this->get('doctrine_mongodb')->getManager();

    $repository = $dm->getRepository('AppBundle:Event');
    $events = $repository->findBy(array('user' => $this->getUser()));

    $serializer = SerializerBuilder::create()->build();
    $result = $serializer->deserialize($events, 'AppBundle\Document\Event', 'json');
    var_dump($result);
    exit;
}

This is not working because some of the elements passed into the serializer are of an array format. Error I am getting.

Warning: json_decode() expects parameter 1 to be string, array given
500 Internal Server Error - ContextErrorException

However if I use the inbuilt Symfony Serializer it works fine:

    $serializer = $this->container->get('serializer');
    $reports = $serializer->serialize($events, 'json');
    return new Response($reports);

However the JSON to be produced will be different to my Document/Entity hence why I want/need to use the JMSSerializerBundle.

For example, a record looks like this:

[{
    "id": "572041b3288b560e5e00451c",
    "name": "Test",
    "date": "2016-04-27T05:25:00+1000",
    "enddate": "2016-04-30T11:55:00+1000",
    "location": {
        "name": "Sydney, NSW"
    },
    "key": {
        "id": "1g43g34g34g23f32g32G32gGSDF"
    },
    "user": {
        "id": "57203174288b560e5e0044da"
    }, ...
}]

But I only want to display (output) to JSON

[{
    "id": "572041b3288b560e5e00451c",
    "name": "Test",
    "date": "2016-04-27T05:25:00+1000",
    "location": "Sydney, NSW"
}]

How would I go about doing this? There is not much documentation on JMSSerializerBundle online.

Edit: I should mention that the database collection I am querying has a relation to the User collection which is managed by FOSUserBundle. I'm not sure if this has any relation to my problem however

Brendan
  • 908
  • 2
  • 15
  • 30

1 Answers1

1

You should look at the documentation of the bundle, may be you will find more information http://jmsyst.com/bundles/JMSSerializerBundle

http://jmsyst.com/libs/serializer/master/usage

You seems to use the wrong function of the serializer. In your case, you seems to need to get a json from your user object, so you need to use

$serializer = SerializerBuilder::create()->build();
$result = $serializer->serialize($events, 'AppBundle\Document\Event', 'json');

serialize($object):string : get a string from an object

deserialize($string):object : get an object from a representation of an object (json, xml...).

olibiaz
  • 2,551
  • 4
  • 29
  • 31
  • Thanks for your reply. I did use the wrong serializer you're right. I changed from `$result = $serializer->deserialize($events, 'AppBundle\Document\Event', 'json');` to `$result = $serializer->serialize($events, 'json');` as per the documentation: http://jmsyst.com/bundles/JMSSerializerBundle#usage Where I am stuck is filtering the result so only selected fields are displayed. I couldn't find any note about this in the documentation and was wondering if it was possible. Thanks – Brendan May 04 '16 at 04:19
  • 1
    I think you should take a look at the exclusion system: http://jmsyst.com/libs/serializer/master/cookbook/exclusion_strategies – olibiaz May 04 '16 at 04:39