Skip to content

Fix json_encode regression with JSON_PRETTY_PRINT #6811

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

Conversation

TysonAndre
Copy link
Contributor

@TysonAndre TysonAndre commented Mar 27, 2021

This makes the json encoding behavior the same as it was prior to the memory
optimizations added in f9f8c1c
(for objects with declared properties)

This is based on the code for the unoptimized case below the changes

Buggy output prior to this commit:

{
"prop":"value"}

Correct output:

{
    "prop": "value"
}

This makes the json encoding behavior the same as it was prior to the memory
optimizations added in f9f8c1c
(for objects with declared properties)

This is based on the code for the optimized case.

Buggy output prior to this commit:
```
{
"prop":"value"}
```

Correct output:

```
{
    "prop": "value"
}
```
echo json_encode($obj, JSON_PRETTY_PRINT), "\n";
?>
--EXPECT--
{}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The output in php 8.1.0-dev after this patch matches what I'm seeing in php 7.4

I think the original patch was worth it to avoid an increase in memory usage caused by json_encode(), and shouldn't cause any invalid memory accesses - there may be a tiny change in output for edge cases involving JsonSerializable and regular objects, but I don't expect code to rely on that behavior

@TysonAndre
Copy link
Contributor Author

I think this is correct and the unit test should cover relevant cases - @dstogov @nikic @kocsismate this could take another look.

If there are no objections I plan to merge this on tuesday, if this is approved I'll merge it earlier

@php-pulls php-pulls closed this in 92aeda5 Mar 27, 2021
kamil-tekiela pushed a commit to kamil-tekiela/php-src that referenced this pull request Apr 4, 2021
This makes the json encoding behavior the same as it was prior to the memory
optimizations added in f9f8c1c
(for objects with declared properties)

This is based on the code for the unoptimized case below the changes.

Buggy output prior to this commit:
```
{
"prop":"value"}
```

Correct output:

```
{
    "prop": "value"
}
```

Closes phpGH-6811
@TysonAndre TysonAndre deleted the fix-json_pretty_print-declared-properties branch November 25, 2021 21:34
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