Skip to content

[HttpFoundation] json_encode options #10496

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

Closed
wants to merge 1 commit into from
Closed

[HttpFoundation] json_encode options #10496

wants to merge 1 commit into from

Conversation

steffkes
Copy link

Q A
Bug fix? [no]
New feature? [no]
BC breaks? [no]
Deprecations? [no]
Tests pass? [yes]
Fixed tickets -
License MIT
Doc PR -

PR #9915 introduced the possibility to overwrite the options we use while calling json_encode - right now, if i want to extend JsonResponse i have to call setEncondingOptions in my constructor which causes the encoded string to be decoded and re-encoded again (this time using the correct/intended options).

if we would rely on getEncodingOptions in the first place, we could save one roundtrip, since my own JsonResponse could define getEncodingOptions like this:

protected function getEncodingOptions()
{
  return parent::getEncodingOptions() | JSON_PRETTY_PRINT;
}

and there would be no need to decode & re-encoding the whole thing at all - just because i would want to have pretty printed json.

WDYT?

@Tobion
Copy link
Contributor

Tobion commented Mar 20, 2014

The problem in your case would still be that the protected encodingOptions are not the same as the options that you actually use. A little strange in your class.
If you only set your data with setData and leave it null in the constructor, the overhead would be minimal to decode/encode null.

@GromNaN
Copy link
Member

GromNaN commented Mar 20, 2014

I agree with @Tobion, the method getEncodingOptions should not return something different that the property value.

If you extend the class, the following constructor will set the desired encoding options without recalculating the JSON value.

    public function __construct($data = null, $status = 200, $headers = array())
    {
        parent::__construct(null, $status, $headers);

        $this->encodingOptions |= JSON_PRETTY_PRINT;

        $this->setData($data);
    }

@steffkes
Copy link
Author

That's actually right, didn't think about it that way - thanks for the pointer :)

@steffkes steffkes closed this Mar 20, 2014
@steffkes steffkes deleted the json-encoding-options-method branch March 20, 2014 08:37
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.

3 participants