Cannot decode JSON long value (results to 2147483647)

365 views
Skip to first unread message

rjjuu...@gmail.com

unread,
Jul 28, 2007, 4:31:29 PM7/28/07
to php-json
Hello,

I'm trying to decode a JSON encoded string. This string contains a big
value (of type long).
The result in PHP is always 2147483647 (PHP s max integer value), see
the code below.

How to make PHP aware of the fact that we deal with a long value?

<?php

$input = '{ "abc" : 121212121212 }';
$val = json_decode($input, true);

echo '<pre>';
echo var_dump($val);
echo '</pre>';

?>

array(1) {
["abc"]=>
int(2147483647)
}


Rob

jim

unread,
Aug 30, 2007, 11:45:49 PM8/30/07
to php-json
just ran across this problem today...
here's a quick fix, modify the json_create_zval function in
JSON_parser.c to the code below so it just treats everything as a
string...
I'm sure there is a better way to fix this problem, but i just spent a
minute to quickly fix the problems i was having.

static void json_create_zval(zval **z, smart_str *buf, int type)
{
ALLOC_INIT_ZVAL(*z);

if (type == IS_LONG)
{
ZVAL_STRINGL(*z, buf->c, buf->len, 1);
}
else if (type == IS_DOUBLE)
{
ZVAL_STRINGL(*z, buf->c, buf->len, 1);
}
else if (type == IS_STRING)
{
ZVAL_STRINGL(*z, buf->c, buf->len, 1);
}
else if (type == IS_BOOL)
{
ZVAL_STRINGL(*z, buf->c, buf->len, 1);
}
else /* type == IS_NULL) || type unknown */
{
ZVAL_NULL(*z);
}
}

On Jul 28, 1:31 pm, "rjjuurl...@gmail.com" <rjjuurl...@gmail.com>
wrote:

Omar Kilani

unread,
Sep 1, 2007, 1:06:36 AM9/1/07
to php-...@googlegroups.com
Hi Rob,

> I'm trying to decode a JSON encoded string. This string contains a big
> value (of type long).
> The result in PHP is always 2147483647 (PHP s max integer value), see
> the code below.
>
> How to make PHP aware of the fact that we deal with a long value?

Try this patch:

http://treehou.se/~omar/php-json-ext-1.2.1-32bit_int.patch

This bug exists as I don't have any 32bit machines in active use. ;)

I'll be sure to test this on future releases.

Thanks.

> Rob

Regards,
Omar

Reply all
Reply to author
Forward
0 new messages