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

Custom serialization for specific classes #25

Merged
merged 1 commit into from
Jun 16, 2016
Merged

Conversation

tinoadams
Copy link

This is trying to address issues #16. Let me know what you think.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.2%) to 98.095% when pulling 5c25ea2 on tinoadams:master into 68c6a72 on zumba:master.

@jrbasso
Copy link
Member

jrbasso commented Jun 9, 2016

@tinoadams I tried to work on it in the past for a little bit, but the problem is how to deal with recursions, ie. object A has an attribute with an instance of B. If the JsonSerializer has custom serializers for A and B, how it would work? Besides that, how can we serialize the "native" portion, like regular objects that we have defined in the serializer...

@tinoadams
Copy link
Author

@jrbasso I figured that resolving transitive dependencies is responsibility of the custom serializer. In the given example I would expect the custom serializer for class A to return the full structure (nested maps) that must also contain the properties object B ie. array('objA_refToB'=>array('objB_property1'=>'x')) which ultimately becomes {"@type":"A","objA_refToB":{"objB_property1":"x"}}. We have to assume that the custom serializer for class A knows how to serialize all properties of class A including the case where one of the properties contains an instance of class B.

If you see a use case where a custom serializer would want to make use of reflection on some properties then we could expose the serializeData method and pass the current Zumba\JsonSerializer to the custom serialize method. In that case the custom serializer for class A could call serializeData to serialize refToB using reflection ie. return array('objA_refToB'=>$zumbaSerializer->serializeData($objA->refToB)).

Thoughts?

@jrbasso jrbasso mentioned this pull request Jun 11, 2016
@jrbasso
Copy link
Member

jrbasso commented Jun 16, 2016

@tinoadams This kind matches the behave of Serializable interface, where the serialize method needs to return the desired output. However it doesn't behave like JsonSerializable, which return the non-object value to be serialized (ie, array with fields).

I would argue that following JsonSerializable makes more sense since it is designed for json, like this lib. However, this lib supports JsonSerializable, so the class could potentially implement it. Using the Serializable behavior, like your did, would cover scenarios where the class doesn't implement, like DateTime.

I guess this implementation is a good start to support it. To send the serializer instance to the object can be analyzed it in the future.

Thanks for your contribution!

@jrbasso jrbasso merged commit 90832a2 into zumba:master Jun 16, 2016
@jrbasso jrbasso added this to the 3.0 milestone Jun 16, 2016
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

Successfully merging this pull request may close these issues.

3 participants