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

Using JSON in Yaml fixtures #183

Closed
thul opened this issue Feb 25, 2015 · 3 comments
Closed

Using JSON in Yaml fixtures #183

thul opened this issue Feb 25, 2015 · 3 comments

Comments

@thul
Copy link

thul commented Feb 25, 2015

Some of the entities im trying to write fixtures for contain json data.
The data looks like this in the fixture:

'{"label_alignment":"block","max_length":50,"field_length":"60","default_value":""}' 

But once i run the fixtures, the ", { and } will be escaped with a \ making it invalid json
I tried putting a Processor in between, but the Pre persist and Post persist data is both unescaped.

This is the entity entry for the data

/**
 * @ORM\Column(
 *     type="json_array"
 * )
 * @Type("array")
 * @Groups({"list", "detail"})
 */

Is it something i am doing wrong? or is it something the entities are doing wrong? or is Alice doing something wrong? I am clueless. Any help will be nice.

@Seldaek
Copy link
Member

Seldaek commented Feb 25, 2015

I believe you should just define an array (or yaml object) and let
doctrine serialize it to json..

i.e.

    field: {label_alignment: block, max_length: 50, field_length: 60, 
default_value: ""}

@thul
Copy link
Author

thul commented Feb 25, 2015

That seemed to have solved it, in combination with me removing an 'array to string' conversion error in the entity. Arrays given to the setConfig() were casted to a string for some reason

@thul thul closed this as completed Feb 25, 2015
@Eliasyoussef47
Copy link

I was able to solve this using a custom faker provider (as mentioned here).

JsonProvider.php

class JsonProvider
{
    /**
     * Decodes a JSON string.
     * This function must be used in order to use a JSON object (string) as a column value. Without this method, the 
     * JSON string would be escaped. The value that will be assigned to the column will be a correct JSON string.
     * @see https://github.com/nelmio/alice/issues/935#issuecomment-397587318
     * @example <json_decode('{"jeff":"IDK","slim":"shady","age":5}')>
     *
     * @throws JsonException
     */
    public function json_decode(string $json, bool $assoc = false, int $depth = 512, int $options = 0)
    {
        return json_decode($json, $assoc, $depth, JSON_THROW_ON_ERROR | $options);
    }
}

fixtures.yaml

Fully\Qualified\Name\MyClass:
  my_class_7:
    id: 1
    value: <json_decode('{"jeff":"IDK","slim":"shady","age":5}')>

Notice that I made json_decode throw an exception when something goes wrong. I did this to prevent any unexpected behaviour and to stop the fixtures early and report the problem.

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

3 participants