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

Implement enum json serialization #896

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

dont-know-php
Copy link

@dont-know-php dont-know-php commented Oct 29, 2024

Hi team, this is a small PR fixing enum serialization.

Prerequisites

zend.exception_ignore_args must be disabled.

Description

PHP doesn't support Pure Enum json serialization: https://www.php.net/manual/en/language.enumerations.serialization.php

If a Pure Enum is serialized to JSON, an error will be thrown.

We will get an serialization error, for example, if we want to write json serialized error trace with arguments into log, because some package classes/methods use enum in arguments.

There are two possible solutions:

  1. Using Backed Enum
  2. Implement JsonSerializable interface. I chose this option because the package uses Pure Enums.

@dont-know-php dont-know-php force-pushed the feature/enum-serialization branch 2 times, most recently from 9a4b881 to bcbbd0c Compare October 30, 2024 07:50
@rubenvanassche
Copy link
Member

Hi @dont-know-php,

I'm not sure when it happens that these structures would be converted into JSON. I'll guess you're catching stacktraces with arguments and somehow try to JSON encode them to send them to an external service. But I think it then would be easier just to make them string backed enums instead of building an own serialization mechanism.

@dont-know-php dont-know-php force-pushed the feature/enum-serialization branch from bcbbd0c to bff27b1 Compare January 3, 2025 16:56
@dont-know-php dont-know-php force-pushed the feature/enum-serialization branch from bff27b1 to 9b444df Compare January 3, 2025 17:01
@dont-know-php
Copy link
Author

hi @rubenvanassche ,

I'll guess you're catching stacktraces with arguments and somehow try to JSON encode them to send them to an external service

You are absolutely right, by default stackraces with arguments is disabled using zend.exception_ignore_args ini configuration directive. But we use detailed tracing with arguments in our Laravel projects.

But I think it then would be easier just to make them string backed enums instead of building an own serialization mechanism.

Agree, I'have pushed new changes with Backed enums implementation. Please, advise.

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.

2 participants