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

Make more flexible to customize unserialization of classes #16

Open
jrbasso opened this issue Feb 29, 2016 · 2 comments
Open

Make more flexible to customize unserialization of classes #16

jrbasso opened this issue Feb 29, 2016 · 2 comments
Assignees
Milestone

Comments

@jrbasso
Copy link
Member

jrbasso commented Feb 29, 2016

We currently have a special condition for DateTime class, but looking at the project forks people are also extending it MongoId and MongoDate. Having an easier way to customize it would be ideal to avoid forks and customization diverging the project.

An idea would be making JsonSerializer receive a list of classname with callable to generate the output. It would be used on the unserialize portion. The current DateTime customization would live in this project since it is a php-core class. People could add it on demand.

Update docs to explain how to implement these customizations too and how to use.

@jrbasso
Copy link
Member Author

jrbasso commented Jun 16, 2016

#25 adds the support for this functionality. I will be creating the custom serializers for things like DateTime and Mongo.

@gedeef
Copy link

gedeef commented Apr 6, 2017

I couldn't get the enhanced version with composer for some reason (not in stable release).
However I could manage my date formatting issue rather simply :

use Zumba\JsonSerializer\JsonSerializer as ZumbaJsonSerializer;
class JsonSerializer extends ZumbaJsonSerializer
{
    protected function serializeObject($value) {
        $data = parent::serializeObject($value);
        if ($data['@type']=='DateTime'){
            //any changes here
            $data['date'] = $value->format(\DateTime::ISO8601);
        }
        return $data;
    }
}

Maybe not the most efficient since the formatting job is done twice, but it works (however a non standard format would require to override unserializeObject as well).

@jrbasso jrbasso modified the milestones: 3.0, 3.1 Jul 20, 2020
@jrbasso jrbasso modified the milestones: 3.1, 3.2 Jul 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants