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

Embed JSON string without extra escape #1142

Closed
bobvandevijver opened this issue Nov 15, 2019 · 1 comment
Closed

Embed JSON string without extra escape #1142

bobvandevijver opened this issue Nov 15, 2019 · 1 comment

Comments

@bobvandevijver
Copy link
Contributor

bobvandevijver commented Nov 15, 2019

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

I have an object which contains a couple of file reference, one of which is a JSON file. I want to serialize this object into JSON, but the not serialize the JSON string again.

I'm already using a custom serialization handler to convert the files into their string representation.

$visitor->visitProperty(
        new StaticPropertyMetadata('', 'field', NULL),
        $object->file->getContents());

This results in:

{
  "field": "[{\"id\": 1, \"prop\": 1}]"
}

Instead of the wanted:

{
  "field": [{"id": 1, "prop":1}]
}

I have now fixed this with a smallworkaround (using json_decode to deserialize the JSON string first), but is there a way to do this without the extra deserialize/serialize roundtrip?

$visitor->visitProperty(
        new StaticPropertyMetadata('', 'field', NULL),
        json_decode($object->file->getContents(), true));
@goetas
Copy link
Collaborator

goetas commented Nov 15, 2019

The way you did is the right one. The internal data structure requires an array, that will be converted later in json.

@goetas goetas closed this as completed Nov 15, 2019
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

2 participants