Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
It’s possible (I know - I’ve just spent days trying to understand the problem!) that the json data is not in utf8 format and that will mean that json_decode() will just return NULL. See http://php.net/manual/en/function.json-decode.php#86997
Proposed resolution
Just before we call json_decode(), we should call utf8_encode() on the response we have.
Remaining tasks
Patch, review
User interface changes
none.
API changes
none.
Data model changes
none, other than we stand a better chance of working with a wider variety of data sources.
Comment | File | Size | Author |
---|---|---|---|
#12 | Interdiff-2799353-2-12.txt | 1.14 KB | rachel_norfolk |
#12 | json_decode_assumes-2799353-12.patch | 1003 bytes | rachel_norfolk |
Comments
Comment #2
rachel_norfolkAnd a patch to review...
Comment #3
rachel_norfolkHelpful, when adding a patch, to remember to change the status...
Comment #4
rachel_norfolkOh - just noticed that 8.x-2.x—dev is quite a bit different from beta 2 now. Here’s the same patch agains beta 2, if that is useful. (set to not be tested, hopefully, just for reference)
Comment #6
mikeryanCommitted, thanks!
Comment #10
mikeryanReverted per #2817099: Special characters are imported incorrectly during migration - seems this may do more harm than good.
Comment #11
mikeryanComment #12
rachel_norfolkYes - I can see now why this might not be ideal when a valid result might be obtained without recourse to utf8_endode().
What we should do I *only* do the encoding if json_decode() is failing and returning NULL.
More intelligent (!) patch attached.
Comment #13
rachel_norfolkAlso - mikeryan - note that tests can only be run against 8.x-2.x currently, not yet 3.x
Comment #14
mikeryanRan the 8.x-3.x test (not that this is actually tested:P).
I do need to point out:
I.e., what you have is not legal JSON.
That being said, from the comments at http://php.net/manual/en/function.json-decode.php it seems that brain-dead webservices producing ISO-8859-1 aren't all that unusual so we might as well guard against them...
Should use is_null() instead of comparison to NULL.
Comment #17
mikeryanWent ahead and fixed the is_null() on commit, thanks!