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

feat: add PHP JsonSerializable Preset #1491

Merged
merged 6 commits into from
Aug 26, 2023

Conversation

markuspoerschke
Copy link
Contributor

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.

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.
@netlify
Copy link

netlify bot commented Aug 24, 2023

Deploy Preview for modelina ready!

Name Link
🔨 Latest commit 6e8fba3
🔍 Latest deploy log https://app.netlify.com/sites/modelina/deploys/64e89692e4b67000073e1fde
😎 Deploy Preview https://deploy-preview-1491--modelina.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@coveralls
Copy link

Pull Request Test Coverage Report for Build 5962856642

  • 14 of 14 (100.0%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.04%) to 92.236%

Totals Coverage Status
Change from base Build 5943002307: 0.04%
Covered Lines: 5276
Relevant Lines: 5578

💛 - Coveralls

@markuspoerschke markuspoerschke changed the title feat: PHP JsonSerializable Preset feat: add PHP JsonSerializable Preset Aug 24, 2023
Copy link
Member

@jonaslagoni jonaslagoni left a 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? 🤔

docs/languages/Php.md Outdated Show resolved Hide resolved
examples/php-generate-json-serializable-preset/README.md Outdated Show resolved Hide resolved
src/generators/php/presets/JsonSerializablePreset.ts Outdated Show resolved Hide resolved
Ferror
Ferror previously approved these changes Aug 25, 2023
Copy link
Collaborator

@Ferror Ferror left a 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
@markuspoerschke
Copy link
Contributor Author

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 😄

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
Copy link
Member

@jonaslagoni jonaslagoni left a 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
Copy link

sonarcloud bot commented Aug 25, 2023

SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
7.5% 7.5% Duplication

idea Catch issues before they fail your Quality Gate with our IDE extension sonarlint SonarLint

@jonaslagoni
Copy link
Member

/rtm

@asyncapi-bot asyncapi-bot merged commit b1d2f62 into asyncapi:master Aug 26, 2023
18 of 19 checks passed
@asyncapi-bot
Copy link
Contributor

🎉 This PR is included in version 1.9.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@jonaslagoni
Copy link
Member

@all-contributors please add @markuspoerschke for code, example, test, docs

@allcontributors
Copy link
Contributor

@jonaslagoni

I've put up a pull request to add @markuspoerschke! 🎉

@markuspoerschke markuspoerschke deleted the php-json-serializeable branch August 28, 2023 11:49
@asyncapi-bot
Copy link
Contributor

🎉 This PR is included in version 2.0.0-next.25 🎉

The release is available on:

Your semantic-release bot 📦🚀

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.

5 participants