drupal_json_decode() and drupal_json_encode() limit what you can do with the functions. Using straight up json_encode() and json_decode() actually give you more power than the Drupal wrappers. Let's make the wrappers just as powerful as the PHP functions themselves.

Comments

alan evans’s picture

I'm heavily on the fence on this one. And that can be painful, depending on the fence. I might have to jump to the -1 side for now.

I've done these checks:

  • Applied the patch
  • verified that there are no remaining instances of drupal_json_decode,
  • checked basic json functionality within a test site
  • ran json-based simpletests: all good

And then I started wondering about the asymmetry this leaves with json_encode though. Essentially your patch removes the wrapper from json_decode (which enforces one option), but it leaves the corresponding wrapper in place for json_encode:

function drupal_json_encode($var) {
  // Encode <, >, ', &, and " using the json_encode() options parameter.
  return json_encode($var, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT);
}

I know I'm being finicky, but this just doesn't feel right (and not just because of the fence posts). If it's the number of options that dissuade you from removing the wrapper on json_encode, then maybe define the combined options as a constant somewhere and refer to that in the function calls. Your patch already converts many function calls to add an extra parameter (which increases the error-proneness - it's harder to maintain because of the number of instances that have to change if anything needs changing), so doing the same for the other wrapper shouldn't be a problem.

Actually the solution I'd prefer to see is further abstraction ... sorry :S ...

Thinking about the asymmetry of json_decode/drupal_json_encode and what the wrapper might be good for, I wonder if you might ever want to switch out the implementation of the json functions (either for some testing purpose, or because you need an option changed in all calls at once ... or you want to use a different library ... or for some unknown reason you want to transfer json as something else ... ok, going too far)

My point is:

  • Keeping the wrappers around aids maintainability, in that options only need to be got right once, and they apply to every call
  • If we remove one of them and keep the other, we end up with "lopsided" code. Not the worst thing in the world, but not ideal either
  • If for some reason you want to change an option, or the underlying implementations, you have to change every instance in code, whereas with a wrapper of some sort you just change one line to change all calls.

That said, the existing wrappers don't gain us anything in flexibility, as the underlying implementation is hardcoded, the only thing they do gain us is keeping the options consistent across all calls.

I would personally suggest wrapping both in an object which manages the undelying implementation and which can be swapped out as a whole wherever needed. So, the first call to either would instantiate a swappable singleton which wraps both json_encode and json_decode with their appropriate options. I do have to wonder if this would be going too far though, for adding flexibility that has no concrete use case yet (AFAIK, no-one has ever asked to have the json backend swappable, the only obvious use case might be changing options).

Leaving in needs review for other opinions.

David_Rothstein’s picture

I would tend to agree that drupal_json_encode() and drupal_json_decode() should either stay or go as a pair...

And I'm also having trouble thinking of use cases where a particular Drupal site would need to swap out different JSON options (or the entire JSON implementation) across all the modules they're running at once. Doesn't mean it doesn't exist though :)

kristiaanvandeneynde’s picture

The thing is that both drupal_json_encode and drupal_json_decode are wrappers that fill in a non-default value for the second argument in their php counterpart. Removing the wrappers could bring about more clutter and inconsistent use of json_encode and json_decode in core.

Module developers have never been told that they have to use the drupal_ versions. If they wanted a different return value from the php counterparts, they probably used those native functions anyway.

Asking all implementing authors to update their module, however, is a big deal. If they don't get the memo, their modules will most likely break because objects are being treated as arrays. Keeping the wrappers in place has the added benefit that we could add extra functionality or security to them, making all implementing modules automatically receive 'the goodies'.

robloach’s picture

Status: Needs review » Postponed
Issue tags: +PHP 5.4

Asking all implementing authors to update their module, however, is a big deal. If they don't get the memo, their modules will most likely break because objects are being treated as arrays.

That's what the change notifications are for :-) .

I'm voting for keeping it how it is for now, at least until we target PHP 5.4, with its new additions to both json_decode and json_encode.

robloach’s picture

Title: Remove drupal_json_decode() » json_encode() and json_decode() limit what we can do with JSON
Status: Postponed » Needs review
StatusFileSize
new1.56 KB

Let's allow the Drupal wrappers to have the same functionality as what's provided with PHP then. This allows JSON_PRETTY_PRINT to be passed in if you want.

Status: Needs review » Needs work
Issue tags: -json, -PHP 5.4

The last submitted patch, json.patch, failed testing.

robloach’s picture

Status: Needs work » Needs review
Issue tags: +json, +PHP 5.4

#5: json.patch queued for re-testing.

jthorson’s picture

This patch looks like it cycles endlessly in testbot. :(

The last submitted patch, json.patch, failed testing.

Anonymous’s picture

Issue summary: View changes

f

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

joelpittet’s picture

Version: 8.2.x-dev » 8.3.x-dev
Priority: Minor » Normal
Issue summary: View changes
Issue tags: -PHP 5.4 +Needs tests

We require 5.5 for 8.x, this is a task so moving to 8.3.x. Likely needs tests to ensure we have this functionality persists

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

aaronbauman’s picture

Title: json_encode() and json_decode() limit what we can do with JSON » Json::encode() and Json::decode() limit what we can do with JSON
Status: Needs work » Needs review
StatusFileSize
new1.02 KB

Anyone still interested in this thread?
The same limitations described apply equally to Drupal\Component\Serialization\Json, which hides all pass-through args to json functions.

Seems like it should be very simple and 100% backwards compatible to expose all the options in the component.
Any reason not to do this?

borisson_’s picture

Issue tags: -json +API-First Initiative

Tagging with API-First Initiative to give it more exposure. This looks not to give BC problems because the new parameters have default values.

borisson_’s picture

I think that this might need additional test-coverage?

dawehner’s picture

To be honest I'm not 100% following. This is a convenience wrapper for the common way how we want to encode/decode JSON. We know that these methods work perfectly together as in, one is the opposite of the other.

What I don't fully understand is the following: What do we gain by adding these parameters? If someone wants to not do the default Drupal json encoding, aka. when they use json_encode/json_decode, they put a quite semantic statement in their code: We use the non Drupal function as we do something different.

If we want to support that usecase, how about adding encodeWithOptions and decodeWithoptions. That way we don't change the signature of \Drupal\Component\Serialization\SerializationInterface.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative, +Needs issue summary update

This issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.

Also not sure about the "why" for this.

Tagging for an issue summary update to better explain why this needs to be done.

Also see it was previously tagged for tests which still would need to happen.

Thanks!

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.