PHP_JSON_ERROR_STATE_MISMATCH,
PHP_JSON_ERROR_CTRL_CHAR,
PHP_JSON_ERROR_SYNTAX,
- PHP_JSON_ERROR_UTF8,
- PHP_JSON_ERROR_RECURSION,
- PHP_JSON_ERROR_INF_OR_NAN,
- PHP_JSON_ERROR_UNSUPPORTED_TYPE
+ PHP_JSON_ERROR_UTF8
};
extern JSON_parser new_JSON_parser(int depth);
static PHP_FUNCTION(json_encode);
static PHP_FUNCTION(json_decode);
static PHP_FUNCTION(json_last_error);
-static PHP_FUNCTION(json_last_error_msg);
static const char digits[] = "0123456789abcdef";
ZEND_BEGIN_ARG_INFO(arginfo_json_last_error, 0)
ZEND_END_ARG_INFO()
-
-ZEND_BEGIN_ARG_INFO(arginfo_json_last_error_msg, 0)
-ZEND_END_ARG_INFO()
/* }}} */
/* {{{ json_functions[] */
PHP_FE(json_encode, arginfo_json_encode)
PHP_FE(json_decode, arginfo_json_decode)
PHP_FE(json_last_error, arginfo_json_last_error)
- PHP_FE(json_last_error_msg, arginfo_json_last_error_msg)
PHP_FE_END
};
/* }}} */
REGISTER_LONG_CONSTANT("JSON_UNESCAPED_SLASHES", PHP_JSON_UNESCAPED_SLASHES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("JSON_PRETTY_PRINT", PHP_JSON_PRETTY_PRINT, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("JSON_UNESCAPED_UNICODE", PHP_JSON_UNESCAPED_UNICODE, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("JSON_PARTIAL_OUTPUT_ON_ERROR", PHP_JSON_PARTIAL_OUTPUT_ON_ERROR, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("JSON_ERROR_NONE", PHP_JSON_ERROR_NONE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("JSON_ERROR_DEPTH", PHP_JSON_ERROR_DEPTH, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("JSON_ERROR_CTRL_CHAR", PHP_JSON_ERROR_CTRL_CHAR, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("JSON_ERROR_SYNTAX", PHP_JSON_ERROR_SYNTAX, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("JSON_ERROR_UTF8", PHP_JSON_ERROR_UTF8, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("JSON_ERROR_RECURSION", PHP_JSON_ERROR_RECURSION, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("JSON_ERROR_INF_OR_NAN", PHP_JSON_ERROR_INF_OR_NAN, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("JSON_ERROR_UNSUPPORTED_TYPE", PHP_JSON_ERROR_UNSUPPORTED_TYPE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("JSON_OBJECT_AS_ARRAY", PHP_JSON_OBJECT_AS_ARRAY, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("JSON_BIGINT_AS_STRING", PHP_JSON_BIGINT_AS_STRING, CONST_CS | CONST_PERSISTENT);
}
if (myht && myht->nApplyCount > 1) {
- JSON_G(error_code) = PHP_JSON_ERROR_RECURSION;
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "recursion detected");
smart_str_appendl(buf, "null", 4);
return;
}
smart_str_appendl(buf, tmp, l);
efree(tmp);
} else {
- JSON_G(error_code) = PHP_JSON_ERROR_INF_OR_NAN;
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "double %.9g does not conform to the JSON spec, encoded as 0", d);
smart_str_appendc(buf, '0');
}
}
}
if (ulen < 0) {
JSON_G(error_code) = PHP_JSON_ERROR_UTF8;
+ if (!PG(display_errors)) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid UTF-8 sequence in argument");
+ }
smart_str_appendl(buf, "null", 4);
} else {
smart_str_appendl(buf, "\"\"", 2);
}
if (myht && myht->nApplyCount > 1) {
- JSON_G(error_code) = PHP_JSON_ERROR_RECURSION;
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "recursion detected");
smart_str_appendl(buf, "null", 4);
return;
}
smart_str_appendl(buf, d, len);
efree(d);
} else {
- JSON_G(error_code) = PHP_JSON_ERROR_INF_OR_NAN;
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "double %.9g does not conform to the JSON spec, encoded as 0", dbl);
smart_str_appendc(buf, '0');
}
}
break;
default:
- JSON_G(error_code) = PHP_JSON_ERROR_UNSUPPORTED_TYPE;
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "type is unsupported, encoded as null");
smart_str_appendl(buf, "null", 4);
break;
}
php_json_encode(&buf, parameter, options TSRMLS_CC);
- if (JSON_G(error_code) != PHP_JSON_ERROR_NONE && !(options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR)) {
- ZVAL_FALSE(return_value);
- } else {
- ZVAL_STRINGL(return_value, buf.c, buf.len, 1);
- }
+ ZVAL_STRINGL(return_value, buf.c, buf.len, 1);
smart_str_free(&buf);
}
/* }}} */
/* {{{ proto int json_last_error()
- Returns the error code of the last json_encode() or json_decode() call. */
+ Returns the error code of the last json_decode(). */
static PHP_FUNCTION(json_last_error)
{
if (zend_parse_parameters_none() == FAILURE) {
}
/* }}} */
-/* {{{ proto string json_last_error_msg()
- Returns the error string of the last json_encode() or json_decode() call. */
-static PHP_FUNCTION(json_last_error_msg)
-{
- if (zend_parse_parameters_none() == FAILURE) {
- return;
- }
-
- switch(JSON_G(error_code)) {
- case PHP_JSON_ERROR_NONE:
- RETURN_STRING("No error", 1);
- case PHP_JSON_ERROR_DEPTH:
- RETURN_STRING("Maximum stack depth exceeded", 1);
- case PHP_JSON_ERROR_STATE_MISMATCH:
- RETURN_STRING("State mismatch (invalid or malformed JSON)", 1);
- case PHP_JSON_ERROR_CTRL_CHAR:
- RETURN_STRING("Control character error, possibly incorrectly encoded", 1);
- case PHP_JSON_ERROR_SYNTAX:
- RETURN_STRING("Syntax error", 1);
- case PHP_JSON_ERROR_UTF8:
- RETURN_STRING("Malformed UTF-8 characters, possibly incorrectly encoded", 1);
- case PHP_JSON_ERROR_RECURSION:
- RETURN_STRING("Recursion detected", 1);
- case PHP_JSON_ERROR_INF_OR_NAN:
- RETURN_STRING("Inf and NaN cannot be JSON encoded", 1);
- case PHP_JSON_ERROR_UNSUPPORTED_TYPE:
- RETURN_STRING("Type is not supported", 1);
- default:
- RETURN_STRING("Unknown error", 1);
- }
-
-}
-/* }}} */
-
/*
* Local variables:
* tab-width: 4
#define PHP_JSON_UNESCAPED_SLASHES (1<<6)
#define PHP_JSON_PRETTY_PRINT (1<<7)
#define PHP_JSON_UNESCAPED_UNICODE (1<<8)
-#define PHP_JSON_PARTIAL_OUTPUT_ON_ERROR (1<<9)
/* Internal flags */
#define PHP_JSON_OUTPUT_ARRAY 0
$a[] = &$a;
var_dump($a);
-
-echo "\n";
-
var_dump(json_encode($a));
-var_dump(json_last_error(), json_last_error_msg());
-echo "\n";
-
-var_dump(json_encode($a, JSON_PARTIAL_OUTPUT_ON_ERROR));
-var_dump(json_last_error(), json_last_error_msg());
+/* Break circular data structure to prevent memory leaks */
+unset($a[0]);
echo "Done\n";
?>
}
}
-bool(false)
-int(6)
-string(%d) "Recursion detected"
-
+Warning: json_encode(): recursion detected in %s on line %d
string(8) "[[null]]"
-int(6)
-string(%d) "Recursion detected"
Done
$a->prop = $a;
var_dump($a);
-
-echo "\n";
-
var_dump(json_encode($a));
-var_dump(json_last_error(), json_last_error_msg());
-
-echo "\n";
-
-var_dump(json_encode($a, JSON_PARTIAL_OUTPUT_ON_ERROR));
-var_dump(json_last_error(), json_last_error_msg());
echo "Done\n";
?>
*RECURSION*
}
-bool(false)
-int(6)
-string(%d) "Recursion detected"
-
+Warning: json_encode(): recursion detected in %s on line %d
string(22) "{"prop":{"prop":null}}"
-int(6)
-string(%d) "Recursion detected"
Done
--FILE--
<?php
var_dump(json_decode("[1]"));
-var_dump(json_last_error(), json_last_error_msg());
+var_dump(json_last_error());
var_dump(json_decode("[[1]]", false, 2));
-var_dump(json_last_error(), json_last_error_msg());
+var_dump(json_last_error());
var_dump(json_decode("[1}"));
-var_dump(json_last_error(), json_last_error_msg());
+var_dump(json_last_error());
var_dump(json_decode('["' . chr(0) . 'abcd"]'));
-var_dump(json_last_error(), json_last_error_msg());
+var_dump(json_last_error());
var_dump(json_decode("[1"));
-var_dump(json_last_error(), json_last_error_msg());
+var_dump(json_last_error());
echo "Done\n";
int(1)
}
int(0)
-string(8) "No error"
NULL
int(1)
-string(28) "Maximum stack depth exceeded"
NULL
int(2)
-string(42) "State mismatch (invalid or malformed JSON)"
NULL
int(3)
-string(53) "Control character error, possibly incorrectly encoded"
NULL
int(4)
-string(12) "Syntax error"
Done
+
var_dump(json_encode("abc"));
var_dump(json_encode("ab\xE0"));
-var_dump(json_encode("ab\xE0", JSON_PARTIAL_OUTPUT_ON_ERROR));
-var_dump(json_encode(array("ab\xE0", "ab\xE0c", "abc"), JSON_PARTIAL_OUTPUT_ON_ERROR));
+var_dump(json_encode("ab\xE0c"));
+var_dump(json_encode(array("ab\xE0", "ab\xE0c", "abc")));
echo "Done\n";
?>
--EXPECTF--
string(5) ""abc""
-bool(false)
+string(4) "null"
string(4) "null"
string(17) "[null,null,"abc"]"
Done
+
var_dump(json_encode("ab\xE0"));
var_dump(json_encode("ab\xE0", JSON_UNESCAPED_UNICODE));
?>
---EXPECTF--
+--EXPECT--
string(156) ""latin 1234 -\/ russian \u043c\u0430\u043c\u0430 \u043c\u044b\u043b\u0430 \u0440\u0430\u043c\u0443 specialchars \u0002 \b \n U+1D11E >\ud834\udd1e<""
string(100) ""latin 1234 -\/ russian мама мыла раму specialchars \u0002 \b \n U+1D11E >𝄞<""
-bool(false)
-bool(false)
+string(4) "null"
+string(4) "null"
$bad_utf8 = quoted_printable_decode('=B0');
json_encode($bad_utf8);
-var_dump(json_last_error(), json_last_error_msg());
+var_dump(json_last_error());
$a = new stdclass;
$a->foo = quoted_printable_decode('=B0');
json_encode($a);
-var_dump(json_last_error(), json_last_error_msg());
+var_dump(json_last_error());
$b = new stdclass;
$b->foo = $bad_utf8;
$b->bar = 1;
json_encode($b);
-var_dump(json_last_error(), json_last_error_msg());
+var_dump(json_last_error());
$c = array(
'foo' => $bad_utf8,
'bar' => 1
);
json_encode($c);
-var_dump(json_last_error(), json_last_error_msg());
-
+var_dump(json_last_error());
?>
--EXPECTF--
int(5)
-string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
int(5)
-string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
int(5)
-string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
int(5)
-string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
+++ /dev/null
---TEST--
-Bug #61537 (json_encode() incorrectly truncates/discards information)
---SKIPIF--
-<?php if (!extension_loaded("json")) print "skip"; ?>
---FILE--
-<?php
-$invalid_utf8 = "\x9f";
-
-var_dump(json_encode($invalid_utf8));
-var_dump(json_last_error(), json_last_error_msg());
-
-var_dump(json_encode($invalid_utf8, JSON_PARTIAL_OUTPUT_ON_ERROR));
-var_dump(json_last_error(), json_last_error_msg());
-
-echo "\n";
-
-$invalid_utf8 = "an invalid sequen\xce in the middle of a string";
-
-var_dump(json_encode($invalid_utf8));
-var_dump(json_last_error(), json_last_error_msg());
-
-var_dump(json_encode($invalid_utf8, JSON_PARTIAL_OUTPUT_ON_ERROR));
-var_dump(json_last_error(), json_last_error_msg());
-
-?>
---EXPECTF--
-bool(false)
-int(5)
-string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
-string(4) "null"
-int(5)
-string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
-
-bool(false)
-int(5)
-string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
-string(4) "null"
-int(5)
-string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
$obj1 = new JsonTest1();
-var_dump(json_encode($obj1, JSON_PARTIAL_OUTPUT_ON_ERROR));
+var_dump(json_encode($obj1));
-echo "==\n";
+echo "\n==\n";
$obj2 = new JsonTest2();
-var_dump(json_encode($obj2, JSON_PARTIAL_OUTPUT_ON_ERROR));
+var_dump(json_encode($obj2));
?>
--EXPECTF--
+Warning: json_encode(): recursion detected in %s on line %d
string(44) "{"test":"123","me":{"test":"123","me":null}}"
+
==
+
+Warning: json_encode(): recursion detected in %s on line %d
string(44) "{"test":"123","me":{"test":"123","me":null}}"
+++ /dev/null
---TEST--
-An error is thrown when INF or NaN are encoded
---SKIPIF--
-<?php if (!extension_loaded("json")) print "skip"; ?>
---FILE--
-<?php
-
-$inf = INF;
-
-var_dump($inf);
-
-var_dump(json_encode($inf));
-var_dump(json_last_error(), json_last_error_msg());
-
-var_dump(json_encode($inf, JSON_PARTIAL_OUTPUT_ON_ERROR));
-var_dump(json_last_error(), json_last_error_msg());
-
-echo "\n";
-
-$nan = NAN;
-
-var_dump($nan);
-
-var_dump(json_encode($nan));
-var_dump(json_last_error(), json_last_error_msg());
-
-var_dump(json_encode($nan, JSON_PARTIAL_OUTPUT_ON_ERROR));
-var_dump(json_last_error(), json_last_error_msg());
-?>
---EXPECTF--
-float(INF)
-bool(false)
-int(7)
-string(34) "Inf and NaN cannot be JSON encoded"
-string(1) "0"
-int(7)
-string(34) "Inf and NaN cannot be JSON encoded"
-
-float(NAN)
-bool(false)
-int(7)
-string(34) "Inf and NaN cannot be JSON encoded"
-string(1) "0"
-int(7)
-string(34) "Inf and NaN cannot be JSON encoded"
-- Iteration 25 --
string(4) "null"
-- Iteration 26 --
-bool(false)
+
+Warning: json_encode(): type is unsupported, encoded as null in %s on line %d
+string(4) "null"
-- Iteration 27 --
string(82) "{"MyInt":99,"MyFloat":123.45,"MyBool":true,"MyNull":null,"MyString":"Hello World"}"
-===Done===
+===Done===
\ No newline at end of file
var_dump($arr);
echo "ENCODE: FROM OBJECT\n";
-$obj_enc = json_encode($obj, JSON_PARTIAL_OUTPUT_ON_ERROR);
+$obj_enc = json_encode($obj);
echo $obj_enc . "\n";
echo "ENCODE: FROM ARRAY\n";
-$arr_enc = json_encode($arr, JSON_PARTIAL_OUTPUT_ON_ERROR);
+$arr_enc = json_encode($arr);
echo $arr_enc . "\n";
echo "DECODE AGAIN: AS OBJECT\n";
var_dump($arr);
echo "ENCODE: FROM OBJECT\n";
-$obj_enc = json_encode($obj, JSON_PARTIAL_OUTPUT_ON_ERROR);
+$obj_enc = json_encode($obj);
echo $obj_enc . "\n";
echo "ENCODE: FROM ARRAY\n";
-$arr_enc = json_encode($arr, JSON_PARTIAL_OUTPUT_ON_ERROR);
+$arr_enc = json_encode($arr);
echo $arr_enc . "\n";
echo "DECODE AGAIN: AS OBJECT\n";
+++ /dev/null
---TEST--
-An error is thrown when an unsupported type is encoded
---SKIPIF--
-<?php if (!extension_loaded("json")) print "skip"; ?>
---FILE--
-<?php
-
-$resource = fopen(__FILE__, "r");
-
-var_dump($resource);
-
-var_dump(json_encode($resource));
-var_dump(json_last_error(), json_last_error_msg());
-
-var_dump(json_encode($resource, JSON_PARTIAL_OUTPUT_ON_ERROR));
-var_dump(json_last_error(), json_last_error_msg());
-
-?>
---EXPECTF--
-resource(5) of type (stream)
-bool(false)
-int(8)
-string(21) "Type is not supported"
-string(4) "null"
-int(8)
-string(21) "Type is not supported"
#define PHP_RELEASE_VERSION 5
#define PHP_EXTRA_VERSION "-dev"
#define PHP_VERSION "5.4.5-dev"
-#define PHP_VERSION_ID 50405
+#define PHP_VERSION_ID 50404