php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52262 json_decode reports no error while returning NULL
Submitted: 2010-07-06 11:17 UTC Modified: 2010-07-06 19:08 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: ross@php.net Assigned: scottmac (profile)
Status: Closed Package: JSON related
PHP Version: 5.3.2 OS: Linux/Ubuntu
Private report: No CVE-ID: None
 [2010-07-06 11:17 UTC] ross@php.net
Description:
------------
I'm attempting to use json_decode on a relatively long piece of JSON. The JSON 
is returned successfully by file_get_contents, and is valid according to 
JSONLint.

What I expect to happen is for the JSON to be correctly decoded, or NULL to be 
returned and json_last_error to be populated with the reason. For some reason 
NULL is returned and json_last_error remains at 0.

I have a feeling that this could be to do with the length of the file 
(containing a large array of objects).

I'm currently using:

ross@ross-laptop:/$ php -v

PHP 5.3.2-1ubuntu4.2 with Suhosin-Patch (cli) (built: May 13 2010 20:01:00) 
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies

Test script:
---------------
<?php
$raw = file_get_contents("http://api.steampowered.com/ISteamUserStats/GetGlobalAchievementPercentagesForApp/v0001/?gameid=440");

$decoded = json_decode($raw);

$errors = array(
    JSON_ERROR_NONE => "No error has occurred",
    JSON_ERROR_DEPTH => "The maximum stack depth has been exceeded",
    JSON_ERROR_CTRL_CHAR => "Control character error, possibly incorrectly encoded",    
    JSON_ERROR_SYNTAX => "Syntax error",
    //JSON_ERROR_UTF8 => "Malformed UTF-8 characters, possibly incorrectly encoded"
);

var_dump("Raw result:", $raw, "\n\n");
var_dump("Decoded result:", $decoded, "\n\n");
var_dump("JSON errors:", $errors[json_last_error()]);

Expected result:
----------------
Raw result: (long raw json)
Decoded result: object stdClass(1) { (data array) }
JSON errors: string "No error has occurred"

Actual result:
--------------
Raw result: (long raw json)
Decoded result: NULL
JSON errors: string "No error has occurred"

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-07-06 13:34 UTC] johannes@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: scottmac
 [2010-07-06 13:34 UTC] johannes@php.net
That codition in php_json_decode is hit as utf16_len == -2

        utf16_len = utf8_to_utf16(utf16, str, str_len);
        if (utf16_len <= 0) {
                if (utf16) {
                        efree(utf16);
                }
                RETURN_NULL();
        }
 [2010-07-06 19:01 UTC] scottmac@php.net
Automatic comment from SVN on behalf of scottmac
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=301028
Log: Fix bug #52262 - Invalid UTF-8 documents don't set an error code when they fail to decode.
 [2010-07-06 19:08 UTC] scottmac@php.net
-Status: Assigned +Status: Closed
 [2010-07-06 19:08 UTC] scottmac@php.net
It now correctly returns an error code to indicate an invalid UTF-8 error. The 
issue is an incorrectly encoded character around number 21,190.

I suggest you speak to Steam and get them to produce a correctly valid feed.
 [2010-07-07 13:44 UTC] ross@php.net
Thanks for the fix, will talk to Steam.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 30 14:01:28 2024 UTC