|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2013-04-23 04:15 UTC] keith at openx dot com
Description:
------------
So, it looks like that when you call json_encode with the JSON_NUMERIC_CHECK
option on strings that have all numbers except for one letter 'e' PHP throws a
warning:
PHP Warning: json_encode(): double INF does not conform to the JSON spec,
encoded as 0 in php shell code on line 1
It happens only on certain strings that make PHP think the number is very large.
According to the docs the 'e' should be followed with +/-, but it seems that
isn't the case.
This causes a problem whenever converting values that are, say, hashed with SHA1.
Since the valid characters are [0-9][A-F], it's very possible to have a value
that is: [0-9]e[0-9]
Test script:
---------------
<?php
$t = array('test' => '123343e871700');
var_dump(json_encode($t, JSON_NUMERIC_CHECK));
Expected result:
----------------
string(10) "{"test":"123343e871700"}"
Actual result:
--------------
PHP Warning: json_encode(): double INF does not conform to the JSON spec,
encoded as 0 in php shell code on line 1
string(10) "{"test":0}"
Patchesreturn_string_instead_of_warning (last revision 2013-04-23 06:27 UTC by keith at openx dot com)is_numeric_patch (last revision 2013-04-23 04:16 UTC by keith at openx dot com) Pull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 11:00:02 2025 UTC |
So the new patch just updates json.c. Instead of throwing the warning and setting the value to 0, it makes the value a string. That way json_encode doesn't change a value that it possibly shouldn't. Valid exponential notation values are converted. php > $t = array('test' => '123343e871700'); php > var_dump(json_encode($t, JSON_NUMERIC_CHECK)); string(24) "{"test":"123343e871700"}" php > $t = array('test' => '123343e1'); php > var_dump(json_encode($t, JSON_NUMERIC_CHECK)); string(16) "{"test":1233430}" php > $t = array('test' => '1.03e-3'); php > var_dump(json_encode($t, JSON_NUMERIC_CHECK)); string(16) "{"test":0.00103}"