Skip to content

[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

Open
wants to merge 1 commit into
base: 7.4
Choose a base branch
from

Conversation

mtarld
Copy link
Contributor

@mtarld mtarld commented Jun 6, 2025

Q A
Branch? 7.4
Bug fix? no
New feature? yes
Deprecations? no
Issues
License MIT

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 option skip_null_properties that allow to skip null 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.

@@ -2,7 +2,7 @@

return static function (mixed $data, \Psr\Container\ContainerInterface $valueTransformers, array $options): \Traversable {
try {
yield 'null';
yield "null";
Copy link
Member

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?

Copy link
Contributor Author

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).

Copy link
Member

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?

Copy link
Contributor Author

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).

@soyuka
Copy link
Contributor

soyuka commented Jun 6, 2025

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, null values just add noise and have rather few impact when used in Javascript as they'll just get evaluated as undefined.

@mtarld mtarld force-pushed the feat/skip-nullable branch from 2b0f89a to 7982333 Compare June 7, 2025 08:16
@mtarld
Copy link
Contributor Author

mtarld commented Jun 7, 2025

I agree with @soyuka; in my opinion, the less noise we have, the better. I updated the PR in that way.

@mtarld mtarld changed the title [JsonStreamer] Allow to skip properties with null value [JsonStreamer] Skip properties with null value Jun 7, 2025
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
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy