-
-
Notifications
You must be signed in to change notification settings - Fork 11
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
Allow null values in nullable properties #25
Conversation
A good use case is a memoized value that only persists during a single request. One cannot simply do: if($this->memo_value === null) {} Luckily this works just fine: $this->memo_value ??= $this->some_complex_thing(); However, that's pretty much the only use case I've run into, personally, where you intentionally store null, so you write code that expects null. I just have a simple iterator on deserialization that checks if it is nullable and not set, if it isn't set, it sets it to |
If you have a computed, memoized field, I'd expect it to not get serialized in the first place. I mean if you have a property that is null, or a property that is uninitialized, right now both of those will get simply omitted from the serialized output. Plus, if you try to deserialize a string where one of the properties is set to null, it will get skipped and the property will end up with either its default value or just uninitialized. I'm still not convinced that isn't the correct behavior; the object may then be in an invalid state, but full on validation is out of scope. There is the |
Ah, yep, these were excluded properties, so they wouldn't get serialized, but upon deserialization, they were unset, despite the default value being null (IIRC). Might be a separate issue altogether, but either way, worked around it just fine. |
Let's look into it from another angle... The expected behaviour of internal PHP JSON deserialization function does not consider
From that I would argue that the another JSON deserialization library should meet basically the same axioms. Even more the serialize/deserialize functions distinguish between
|
I have a case to consider here, which is https://datatracker.ietf.org/doc/html/draft-fge-json-schema-validation-00#section-6.2
So if you are using this library to serialize and deserialize a JSON schema, could this be a problem? (Have not checked your code) |
1a96dee
to
2c4b0da
Compare
Based on the discussion here and on Mastodon, I've decided to go ahead with this change. It's an API break, but not a huge one, and the code changes weren't quite as invasive as I was fearing. I am unsure if this also needs an attribute-based way to make null be skipped when serializing. Thoughts? |
For now I'm going to merge this in its current form. I won't be tagging a release just yet as there's a few other things I want to look into first, but it will get released eventually. 😄 |
Description
Another attempt at #15, to allow nullable values. Updating the serializing side was easy. Updating the deserializing side is much harder, and is not yet working.
I am also torn on if this is even a good idea. This cannot be done without an API change for formatters, and some fairly invasive changes. The idea that null == uninitialized is baked in pretty deep, and in all honesty I'm not sure in what situations that's a wrong assumption.
Can someone provide a concrete common use case that would make this necessary, that cannot already be solved another way (like a default value)? Because at the moment I'm very tempted to won't-fix this.
I also started a Mastodon thread.
Based on the discussion here and on Mastodon, I've decided to go ahead with this change. It's an API break, but not a huge one, and the code changes weren't quite as invasive as I was fearing. I am unsure if this also needs an attribute-based way to make null be skipped when serializing. Thoughts?
Types of changes
What types of changes does your code introduce? Put an
x
in all the boxes that apply: