-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[JsonStreamer] Skip properties with null
value
#60730
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
base: 7.4
Are you sure you want to change the base?
Conversation
@@ -2,7 +2,7 @@ | |||
|
|||
return static function (mixed $data, \Psr\Container\ContainerInterface $valueTransformers, array $options): \Traversable { | |||
try { | |||
yield 'null'; | |||
yield "null"; |
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.
Why did you do this change here and in other places?
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.
Yielding a string with "
instead of '
allows me to inject variables directly in strings, such as yield "{$prefix1}\"name\":";
.
Therefore, I'm yielding every string with "
to be able to merge them easily (see yieldString
and flushYieldBuffer
).
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.
do we have test case where the name contains $foo to ensure those are properly encoded?
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.
Nice catch @nicolas-grekas! It wasn't covered, and string interpolation could have occurred. I now escape every $
that are not explicitly needed (I added a DummyWithDollarProperties
test case to make sure of that).
I'm wondering if this shouldn't actually be the default, I would even not add this as an option as this would simplify the code if a value is null we just skip it. The JsonStreamer should be used for maximum performances, |
2b0f89a
to
7982333
Compare
I agree with @soyuka; in my opinion, the less noise we have, the better. I updated the PR in that way. |
null
valuenull
value
A really common use case in APIs is to be able to skip properties with
null
value (for instance, this use case is required to make the JsonStreamer component working with API Platform).This PR adds the optionskip_null_properties
that allow to skipnull
properties during stream writing.This PR skips properties with
nulln
values during stream writing.This requires to define dynamic object prefixes (such as
''
and','
). That's why the PHP generated code has been updated to merge as many prefixes as possible in raw strings and therefore yield a bit bigger chunks.