zumba/json-serializer

Manage other mode for extra attributes

benito103e opened this issue · 6 comments

First, thanks for your effective and simple to use library.

During unserialization if a property does not exist on target object : the JsonSerializer will try to set an extra attribute to object

try {
    $propRef = $ref->getProperty($property);
    $propRef->setAccessible(true);
    $propRef->setValue($obj, $this->unserializeData($propertyValue));
} catch (ReflectionException $e) {
    $obj->$property = $this->unserializeData($propertyValue);
}

We should manage other mode ($extraAttributeManagementMode):

  • JsonSerializer::EXTRA_ATTRIBUTE_MANAGEMENT_MODE_SET : actual mode
  • JsonSerializer::EXTRA_ATTRIBUTE_MANAGEMENT_MODE_IGNORE : we ignore the json value
  • JsonSerializer::EXTRA_ATTRIBUTE_MANAGEMENT_MODE_EXCEPTION : throw a dedicate exception

@benito103e Thanks for using and contributing with improvements.

Just to understand better the case, which cases would you expect to ignore or receive an exception?

I serialize an object with JSON on application A and send it via api on application B.

If there is a difference on the model version of the object : we will have an error on certains cases.
We should ignore extra attributes : if the object has a magic __set() with controls on property existence for example
We should raise an exception : if we must have an identical model on the two side for example.

Alright, makes sense. Thanks for clarifying.

I should work on that in the next few days.

Hi @benito103e . I just made the PR #33 with the implementation of this. Does it matches with your idea?

I would like to introduce an $options param on the serialize and unserialize methods to match more with the json_serialize function, but it would break the compatibility and would require a major version. For now I just introduced a new method that should be enough without breaking the compatibility.

Hi @jrbasso, I just read your code and related tests.
It seems all god for me, I would have implemented it in the same way.

For the $options param on the serialize and unserialize methods : it was a great idea to reuse the same serializer with different options.
I don't see the compatibility break if the param is an optional array $options = array() param.

Thanks for your works !

@benito103e Yeah, Seems I could. I tried something on 3v4l and got a warning because the definition, but I just tried again and it works fine. I probably did something wrong last time. Either way, the method should be an alternative even in future version for people that like mode verbose setup.