Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Adapt Json\Json for better prettyPrint support #7016

Closed
wants to merge 4 commits into from
Closed

Adapt Json\Json for better prettyPrint support #7016

wants to merge 4 commits into from

Conversation

bernhard-efler
Copy link

I adapted the Json::prettyPrint to generate the same output as json_encode

also I added a "prettyPrint" option to Json::encode to take advantage of the json_encode pretty print and align the functionality to json_encode

This is my first pull request ever, so please be kind :-)


$encodeOptions = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP;

if (version_compare(phpversion(), '5.4', '>=') && $prettyPrint) {
Copy link
Contributor

Choose a reason for hiding this comment

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

And now this can't be cached/optimezed into bytecode.
Use PHP_VERSION_ID instead.

See symfony/symfony#12497

Or leave version and simply check if constant is defined.

Copy link
Member

Choose a reason for hiding this comment

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

In addition to that, phpversion() is very unreliable on some distros :-(

Copy link
Author

Choose a reason for hiding this comment

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

thx, I fixed the version check

$encodeOptions = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP;

if (PHP_VERSION_ID >= 50400 && $prettyPrint) {
$encodeOptions |= JSON_PRETTY_PRINT;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not check if JSON_PRETTY_PRINT is defined?
Now one must know the exact vesion of PHP that introduced this constant.
And what about hhvm - they may handle it a bit different (and may not, they for example have some problems with T_FINALLY).

Check if the feature you want is in the environment, not if the environment version is as you want.

add pretty print to Json ViewModel
@bernhard-efler
Copy link
Author

I changed the support of pretty print in json_encode
also I added pretty print support to the JSON view model


$encodeOptions = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP;

if (defined(JSON_PRETTY_PRINT) && $prettyPrint) {
Copy link
Contributor

Choose a reason for hiding this comment

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

must be defined('JSON_PRETTY_PRINT')

Copy link
Contributor

Choose a reason for hiding this comment

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

Wouldn't

if ($prettyPrint && defined('JSON_PRETTY_PRINT'))

be faster?

Copy link
Member

Choose a reason for hiding this comment

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

Indeed

Copy link
Author

Choose a reason for hiding this comment

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

You're nitpicker ;-)
But otherwise I wouldn't lern anything!

The line is fixed

weierophinney added a commit that referenced this pull request Feb 24, 2015
Adapt Json\Json for better prettyPrint support
weierophinney added a commit that referenced this pull request Feb 24, 2015
@weierophinney
Copy link
Member

Merged to develop for release with 2.4.

weierophinney added a commit to zendframework/zend-json that referenced this pull request May 15, 2015
Adapt Json\Json for better prettyPrint support
weierophinney added a commit to zendframework/zend-json that referenced this pull request May 15, 2015
weierophinney added a commit to zendframework/zend-view that referenced this pull request May 15, 2015
Adapt Json\Json for better prettyPrint support
weierophinney added a commit to zendframework/zend-view that referenced this pull request May 15, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants