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

Allow to typehint for the type (array/hash) of the root item to be serialized #728

Merged
merged 8 commits into from
Apr 21, 2017

Conversation

goetas
Copy link
Collaborator

@goetas goetas commented Mar 13, 2017

BC version of #698

Fixes schmittjoh/JMSSerializerBundle#373 #709 #706 schmittjoh/JMSSerializerBundle#375

Closes #475 #248 #234

Examples:

// default / current (let the PHP's json_encode function decide (and sometimes the serializer :/))
$serializer->serialize([1, 2]); //  [1, 2]
$serializer->serialize(['a', 'b']); //  ['a', 'b']
$serializer->serialize(['c' => 'd']); //  {"c" => "d"}
$serializer->serialize([1, 2], SerializationContext::create()->setInitialType('array')); //  [1, 2]
$serializer->serialize([1 => 2], SerializationContext::create()->setInitialType('array')); //  {"1": 2}
$serializer->serialize(['a', 'b'], SerializationContext::create()->setInitialType('array')); //  ['a', 'b']
$serializer->serialize(['c' => 'd'], SerializationContext::create()->setInitialType('array')); //  {"c" => "d"}

// typehint as strict array, keys will be always discarded 
$serializer->serialize([], SerializationContext::create()->setInitialType('array<integer>')); //  []
$serializer->serialize([1, 2], SerializationContext::create()->setInitialType('array<integer>')); //  [1, 2]
$serializer->serialize(['a', 'b'], SerializationContext::create()->setInitialType('array<integer>')); //  ['a', 'b']
$serializer->serialize(['c' => 'd'], SerializationContext::create()->setInitialType('array<string>')); //  ["d"]

// typehint as hash, keys will be always considered
$serializer->serialize([], SerializationContext::create()->setInitialType('array<integer,integer>')); //  {}
$serializer->serialize([1, 2], SerializationContext::create()->setInitialType('array<integer,integer>')); //  {"0" : 1, "1" : 2}
$serializer->serialize(['a', 'b'], SerializationContext::create()->setInitialType('array<integer,integer>')); //  {"0" : "a", "1" : "b"}
$serializer->serialize(['c' => 'd'], SerializationContext::create()->setInitialType('array<string,string>')); //  {"d" : "d"}

@goetas goetas changed the title Allow to typehint if the type (array/hash) of the root item to be serialized Allow to typehint for the type (array/hash) of the root item to be serialized Mar 13, 2017
@goetas
Copy link
Collaborator Author

goetas commented Mar 15, 2017

@schmittjoh what do you think?

@goetas
Copy link
Collaborator Author

goetas commented Mar 29, 2017

After thinking for some time on how to deal with this, i can suggest:

  1. modify existing syntax/behaviour
array # let the json_encode function decide
array<foo> # always array
array<string,foo> # always hash
array<integer,foo> # always hash

The approach uses "string" and "integer", "float"... (an explicit key type definition) to define when an array is a hash.
In reality the key type definition is just ignored, since in hashes keys are always strings.

  1. introduce a special parameter map or list.
array # working as before
array<foo> # working as before
array<map,foo> # always hash
array<list,foo> # always array

This approach makes it a bit differently, using the first parameter to specify when an array is a hash.

  1. introduce a special type map or list.
map<foo> # always hash
list<foo> # always array

This approach is even more explicit, but introduces new types.

The approach 1 and 2 is compatible with ArrayCollection too, since is possible to define

ArrayCollection<map, User>
or 
ArrayCollection<int, User>

Currently the approach I prefer is the 2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant