-
-
Notifications
You must be signed in to change notification settings - Fork 188
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
feat: add PHP JsonSerializable Preset #1491
feat: add PHP JsonSerializable Preset #1491
Conversation
In PHP objects (and also arrays and scalars) can be serialized to JSON using the function `json_encode()`. In order to maintain the correct property names and values for Enums, normalization of values needs to be implemented manually using the `JsonSerializable` interface. The serialization to JSON can be enabled by using the optional preset `PHP_JSON_SERIALIZABLE_PRESET`. This way of serializing PHP objects does not require any user land package and works out of the box with each PHP version. It is also compatible with different serialization frameworks, like Symfony.
✅ Deploy Preview for modelina ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
Pull Request Test Coverage Report for Build 5962856642
💛 - Coveralls |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@markuspoerschke thanks for taking the time to provide a PR 🙏
I have a few comments ✌️
@Ferror do you have time to do a review? 🤔
Co-authored-by: Jonas Lagoni <jonas-lt@live.dk>
Co-authored-by: Jonas Lagoni <jonas-lt@live.dk>
examples/php-generate-json-serializable-preset/__snapshots__/index.spec.ts.snap
Outdated
Show resolved
Hide resolved
test/generators/php/presets/__snapshots__/JsonSerializablePreset.spec.ts.snap
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I much more prefer using Serializers form Symfony or other, but I can guess people may like that. Next would be ability to create event from array of data 😄
* remove import of JsonSerializable * add test for mixed (int and string) enums
Me too. The cool thing is, that Symfony ships with a JsonSerializableNormalizer, which allows using this kind of PHP object also with Symfony serializer. |
* extract serializing properties to own variable to make code more readable. * use ConstrainedDictionaryModel to detect additional properties
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The good thing is that even if two competing serialization libraries, each can have their own preset 🙂 We have such a case in C# for example.
Seems like some of the test snapshots need an update 🙂
SonarCloud Quality Gate failed. 0 Bugs No Coverage information Catch issues before they fail your Quality Gate with our IDE extension SonarLint |
/rtm |
🎉 This PR is included in version 1.9.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
@all-contributors please add @markuspoerschke for code, example, test, docs |
I've put up a pull request to add @markuspoerschke! 🎉 |
🎉 This PR is included in version 2.0.0-next.25 🎉 The release is available on: Your semantic-release bot 📦🚀 |
In PHP objects (and also arrays and scalars) can be serialized to JSON using the function
json_encode()
. In order to maintain the correct property names and values for Enums, normalization of values needs to be implemented manually using theJsonSerializable
interface.The serialization to JSON can be enabled by using the optional preset
PHP_JSON_SERIALIZABLE_PRESET
.This way of serializing PHP objects does not require any user land package and works out of the box with each PHP version. It is also compatible with different serialization frameworks, like Symfony.