Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DateTime converted to ArrayObject instead of string in custom visitor class #1017

Closed
mvachette opened this issue Dec 5, 2018 · 0 comments
Closed

Comments

@mvachette
Copy link

Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no

I use a custom visitor class (declared using Symfony DIC).

In the "getResult" method, DateTime objects are converted to ArrayObject, instead of string (I can see that in standard JsonSerializationVisitor, DateTime objects are well converted, when printing "$this->getRoot()" in getResults()), that leads to empty string in my final output.

Steps required to reproduce the problem

  1. Configure a custom visitor class (I'm using JMS serializer with Symfony)
  2. launch serialization with data containing some DateTime objects
  3. In the "getResult" method, print the content of this->getRoot()

Expected Result

  • DateTime are converted to string at this level (this is what I see if I use standard serializer)

Actual Result

  • DateTime object are converted to ArrayObject at this level
  • Even if I declare the standard JsonSerializationVisitor class as custom class serializer, the problem occurs: it seems to be related to the use of a custom serilizer class, not to the class itself

Code Extracts

Configuration of custom serializer

custom-serializer:
        class: JMS\Serializer\JsonSerializationVisitor
        arguments: ["@jms_serializer.naming_strategy"]
        tags:
          - { name: jms_serializer.serialization_visitor, format: "custom" }

Serialized data

$data = ['date' => new \DateTime()];
$serializer->serialize($data, 'custom');

Output with "custom" format

Trace in JsonSerializationVisitor:

 public function getResult()
    {
        var_dump($this->getRoot()); exit;

Output

array (size=1)
  'date'=> 
      object(ArrayObject)[5097]
      private 'storage' =>
        array (size=0)
          empty
</pre>

Output using regular "custom" format

array (size=1)
  'date' => string '2018-12-05T11:24:48+01:00' (length=25)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant