Commit 0dbf7aec authored by Ondrej Sury's avatar Ondrej Sury

Imported Upstream version 5.6.21+dfsg

parent c68d5d15
JSON
Omar Kilani, Scott MacVicar
This diff is collapsed.
/* JSON_parser.h */
#ifndef JSON_PARSER_H
#define JSON_PARSER_H
#include "php.h"
#include "ext/standard/php_smart_str.h"
#include "php_json.h"
#define JSON_PARSER_DEFAULT_DEPTH 512
typedef struct JSON_parser_struct {
int state;
int depth;
int top;
int error_code;
int* stack;
zval **the_zstack;
zval *the_static_zstack[JSON_PARSER_DEFAULT_DEPTH];
} * JSON_parser;
enum error_codes {
PHP_JSON_ERROR_NONE = 0,
PHP_JSON_ERROR_DEPTH,
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
};
extern JSON_parser new_JSON_parser(int depth);
extern int parse_JSON_ex(JSON_parser jp, zval *z, unsigned short utf16_json[], int length, int options TSRMLS_DC);
extern int free_JSON_parser(JSON_parser jp);
static inline int parse_JSON(JSON_parser jp, zval *z, unsigned short utf16_json[], int length, int assoc TSRMLS_DC)
{
return parse_JSON_ex(jp, z, utf16_json, length, assoc ? PHP_JSON_OBJECT_AS_ARRAY : 0 TSRMLS_CC);
}
#endif
json 1.2.0
==========
This extension implements the JavaScript Object Notation (JSON)
data-interchange format as specified in [0].
Two functions are implemented: encoding and decoding. The decoding
is handled by a parser based on JSON_checker[1] by Douglas Crockford.
Function overview
-----------------
string json_encode ( mixed value )
json_encode returns a string containing the JSON representation of value.
value can be any type except a resource.
mixed json_decode ( string json, [bool assoc] )
json_decode takes a JSON string and converts it into a PHP variable.
When assoc is given, and evaluates to TRUE, json_decode() will return
any objects as associative arrays.
Example usage
-------------
$arr = array("a"=>1,"b"=>2,"c"=>3,"d"=>4,"e"=>5);
echo json_encode($arr);
---> {"a":1,"b":2,"c":3,"d":4,"e":5}
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json));
---> object(stdClass)#1 (5) {
["a"]=>
int(1)
["b"]=>
int(2)
["c"]=>
int(3)
["d"]=>
int(4)
["e"]=>
int(5)
}
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json, true));
---> array(5) {
["a"]=>
int(1)
["b"]=>
int(2)
["c"]=>
int(3)
["d"]=>
int(4)
["e"]=>
int(5)
}
Authors
-------
Omar Kilani <omar@php.net>
---
[0] http://www.crockford.com/JSON/draft-jsonorg-json-00.txt
[1] http://www.crockford.com/JSON/JSON_checker/
dnl
dnl $Id$
dnl
PHP_ARG_ENABLE(json, whether to enable JavaScript Object Serialization support,
[ --disable-json Disable JavaScript Object Serialization support], yes)
if test "$PHP_JSON" != "no"; then
AC_DEFINE([HAVE_JSON],1 ,[whether to enable JavaScript Object Serialization support])
AC_HEADER_STDC
PHP_NEW_EXTENSION(json, json.c JSON_parser.c, $ext_shared)
PHP_INSTALL_HEADERS([ext/json], [php_json.h])
PHP_SUBST(JSON_SHARED_LIBADD)
fi
// $Id$
// vim:ft=javascript
ARG_ENABLE("json", "JavaScript Object Serialization support", "yes");
if (PHP_JSON != "no") {
EXTENSION('json', 'json.c', PHP_JSON_SHARED, "");
ADD_SOURCES(configure_module_dirname, "JSON_parser.c", "json");
PHP_INSTALL_HEADERS("ext/json/", "php_json.h");
}
This diff is collapsed.
# Microsoft Developer Studio Project File - Name="json" - Package Owner=<4>
# Microsoft Developer Studio Generated Build File, Format Version 6.00
# ** DO NOT EDIT **
# TARGTYPE "Win32 (x86) Dynamic-Link Library" 0x0102
CFG=json - Win32 Debug_TS
!MESSAGE This is not a valid makefile. To build this project using NMAKE,
!MESSAGE use the Export Makefile command and run
!MESSAGE
!MESSAGE NMAKE /f "json.mak".
!MESSAGE
!MESSAGE You can specify a configuration when running NMAKE
!MESSAGE by defining the macro CFG on the command line. For example:
!MESSAGE
!MESSAGE NMAKE /f "json.mak" CFG="json - Win32 Debug_TS"
!MESSAGE
!MESSAGE Possible choices for configuration are:
!MESSAGE
!MESSAGE "json - Win32 Debug_TS" (based on "Win32 (x86) Dynamic-Link Library")
!MESSAGE "json - Win32 Release_TS" (based on "Win32 (x86) Dynamic-Link Library")
!MESSAGE
# Begin Project
# PROP AllowPerConfigDependencies 0
# PROP Scc_ProjName ""
# PROP Scc_LocalPath ""
CPP=cl.exe
MTL=midl.exe
RSC=rc.exe
!IF "$(CFG)" == "json - Win32 Debug_TS"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 1
# PROP BASE Output_Dir "Debug_TS"
# PROP BASE Intermediate_Dir "Debug_TS"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 1
# PROP Output_Dir "Debug_TS"
# PROP Intermediate_Dir "Debug_TS"
# PROP Ignore_Export_Lib 0
# PROP Target_Dir ""
# ADD BASE CPP /nologo /MTd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "JSON_EXPORTS" /YX /FD /GZ /c
# ADD CPP /nologo /MDd /W3 /Gm /GX /ZI /Od /I "..\.." /I "..\..\main" /I "..\..\Zend" /I "..\..\TSRM" /I "json_c" /D HAVE_JSON=1 /D "ZEND_WIN32" /D "PHP_WIN32" /D ZEND_DEBUG=1 /D ZTS=1 /D COMPILE_DL_JSON=1 /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "JSON_EXPORTS" /YX /FD /GZ /c
# ADD BASE MTL /nologo /D "_DEBUG" /mktyplib203 /win32
# ADD MTL /nologo /D "_DEBUG" /mktyplib203 /win32
# ADD BASE RSC /l 0x1009 /d "_DEBUG"
# ADD RSC /l 0x1009 /d "_DEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /debug /machine:I386 /pdbtype:sept
# ADD LINK32 iconv.lib php4ts_debug.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /debug /machine:I386 /out:"..\..\Debug_TS/php_json.dll" /pdbtype:sept /libpath:"..\..\Debug_TS"
!ELSEIF "$(CFG)" == "json - Win32 Release_TS"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 0
# PROP BASE Output_Dir "Release_TS"
# PROP BASE Intermediate_Dir "Release_TS"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 0
# PROP Output_Dir "Release_TS"
# PROP Intermediate_Dir "Release_TS"
# PROP Ignore_Export_Lib 0
# PROP Target_Dir ""
# ADD BASE CPP /nologo /MT /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "JSON_EXPORTS" /YX /FD /c
# ADD CPP /nologo /MD /W3 /GX /O2 /I "..\.." /I "..\..\main" /I "..\..\Zend" /I "..\..\TSRM" /I "json_c" /D HAVE_JSON=1 /D "ZEND_WIN32" /D ZEND_DEBUG=0 /D "PHP_WIN32" /D ZTS=1 /D COMPILE_DL_JSON=1 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "JSON_EXPORTS" /D "HAVE_FCNTL_H" /YX /FD /c
# ADD BASE MTL /nologo /D "NDEBUG" /mktyplib203 /win32
# ADD MTL /nologo /D "NDEBUG" /mktyplib203 /win32
# ADD BASE RSC /l 0x1009 /d "NDEBUG"
# ADD RSC /l 0x1009 /d "NDEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /machine:I386
# ADD LINK32 iconv.lib php4ts.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /machine:I386 /out:"..\..\Release_TS/php_json.dll" /libpath:"..\..\Release_TS"
!ENDIF
# Begin Target
# Name "json - Win32 Debug_TS"
# Name "json - Win32 Release_TS"
# Begin Group "Source Files"
# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
# Begin Source File
SOURCE=".\json.c"
# End Source File
# Begin Source File
SOURCE=.\JSON_parser.c
# End Source File
# Begin Source File
SOURCE=.\JSON_parser.h
# End Source File
# End Group
# Begin Group "Header Files"
# PROP Default_Filter "h;hpp;hxx;hm;inl"
# Begin Source File
SOURCE=.\php_json.h
# End Source File
# End Group
# Begin Group "Resource Files"
# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"
# End Group
# End Target
# End Project
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE package SYSTEM "../pear/package.dtd">
<package>
<dep type="php" rel="ge" version="4.3.0" optional="no"/>
<name>json</name>
<summary>JavaScript Object Notation</summary>
<maintainers>
<maintainer>
<user>omar</user>
<name>Omar Kilani</name>
<email>omar@php.net</email>
<role>lead</role>
</maintainer>
</maintainers>
<description>
Support for JSON (JavaScript Object Notation) serialization.
</description>
<license>PHP 3.01</license>
<release>
<state>stable</state>
<version>1.2.1</version>
<date>2006-03-18</date>
<notes>
Fix PECL bug #7147 - rework handling of comma insertion while encoding.
Add tests to package.xml
</notes>
</release>
<configureoptions>
</configureoptions>
<filelist>
<file role="doc" name="README" />
<file role="src" name="config.m4" />
<file role="src" name="config.w32" />
<file role="src" name="json.dsp" />
<file role="src" name="json.c" />
<file role="src" name="JSON_parser.c" />
<file role="src" name="JSON_parser.h" />
<file role="src" name="php_json.h" />
<dir role="test" name="tests">
<file role="test" name="fail001.phpt" />
<file role="test" name="pass001.phpt" />
<file role="test" name="pass001.1.phpt" />
<file role="test" name="pass002.phpt" />
<file role="test" name="pass003.phpt" />
</dir>
</filelist>
<changelog>
<release>
<state>stable</state>
<version>1.0.0</version>
<date>2005-04-01</date>
<notes>
Initial release.
</notes>
</release>
<release>
<state>stable</state>
<version>1.0.1</version>
<date>2005-06-10</date>
<notes>
Fixed non-linear and mixed type array index issues, fixed issues with escaping \\, forked json-c and added Unicode support.
</notes>
</release>
<release>
<state>stable</state>
<version>1.0.2</version>
<date>2005-06-11</date>
<notes>
Fixed issues with object reference counts under PHP4.
</notes>
</release>
<release>
<state>stable</state>
<version>1.0.3</version>
<date>2005-06-15</date>
<notes>
Fixed json-c string corruption issues under Mac OS X and FreeBSD.
</notes>
</release>
<release>
<state>stable</state>
<version>1.0.4</version>
<date>2005-06-15</date>
<notes>
Changes in 1.0.4 released with 1.0.5.
</notes>
</release>
<release>
<state>stable</state>
<version>1.0.5</version>
<date>2005-06-16</date>
<notes>
Changed spacing in json-c encoding, added optional assoc (boolean) parameter to json_decode to decode as associative array instead of object, fixed issues with escaping /.
</notes>
</release>
<release>
<state>stable</state>
<version>1.0.6</version>
<date>2005-08-05</date>
<notes>
Fixed issues with exporting private and protected class members.
</notes>
</release>
<release>
<state>stable</state>
<version>1.0.7</version>
<date>2005-09-07</date>
<notes>
Fixed issues with negative array keys, modified json-c to return an error on unquoted object key names instead of going into an infinite loop.
</notes>
</release>
<release>
<state>stable</state>
<version>1.0.8</version>
<date>2005-12-01</date>
<notes>
Changed license to LGPL, modified build system to allow static compilation into PHP, added strndup check for json-c.
</notes>
</release>
<release>
<state>stable</state>
<version>1.1.0</version>
<date>2005-12-04</date>
<notes>
Port to Win32.
</notes>
</release>
<release>
<state>stable</state>
<version>1.1.1</version>
<date>2006-01-12</date>
<notes>
Cleanup and TSRM performance fixes by rasmus.
</notes>
</release>
<release>
<state>stable</state>
<version>1.2.0</version>
<date>2006-03-15</date>
<notes>
Complete rewrite using JSON_checker as the base for the parser. Implements the JSON specification. 3-8x faster on encodes and 1.2x-4x faster on decodes.
</notes>
</release>
</changelog>
</package>
<!--
vim:et:ts=1:sw=1
-->
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2016 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Omar Kilani <omar@php.net> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifndef PHP_JSON_H
#define PHP_JSON_H
#define PHP_JSON_VERSION "1.2.1"
#include "ext/standard/php_smart_str.h"
extern zend_module_entry json_module_entry;
#define phpext_json_ptr &json_module_entry
#if defined(PHP_WIN32) && defined(JSON_EXPORTS)
#define PHP_JSON_API __declspec(dllexport)
#else
#define PHP_JSON_API PHPAPI
#endif
#ifdef ZTS
#include "TSRM.h"
#endif
ZEND_BEGIN_MODULE_GLOBALS(json)
int encoder_depth;
int error_code;
int encode_max_depth;
ZEND_END_MODULE_GLOBALS(json)
#ifdef ZTS
# define JSON_G(v) TSRMG(json_globals_id, zend_json_globals *, v)
#else
# define JSON_G(v) (json_globals.v)
#endif
PHP_JSON_API void php_json_encode(smart_str *buf, zval *val, int options TSRMLS_DC);
PHP_JSON_API void php_json_decode_ex(zval *return_value, char *str, int str_len, int options, long depth TSRMLS_DC);
extern PHP_JSON_API zend_class_entry *php_json_serializable_ce;
/* json_encode() options */
#define PHP_JSON_HEX_TAG (1<<0)
#define PHP_JSON_HEX_AMP (1<<1)
#define PHP_JSON_HEX_APOS (1<<2)
#define PHP_JSON_HEX_QUOT (1<<3)
#define PHP_JSON_FORCE_OBJECT (1<<4)
#define PHP_JSON_NUMERIC_CHECK (1<<5)
#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)
#define PHP_JSON_PRESERVE_ZERO_FRACTION (1<<10)
/* Internal flags */
#define PHP_JSON_OUTPUT_ARRAY 0
#define PHP_JSON_OUTPUT_OBJECT 1
/* json_decode() options */
#define PHP_JSON_OBJECT_AS_ARRAY (1<<0)
#define PHP_JSON_BIGINT_AS_STRING (1<<1)
static inline void php_json_decode(zval *return_value, char *str, int str_len, zend_bool assoc, long depth TSRMLS_DC)
{
php_json_decode_ex(return_value, str, str_len, assoc ? PHP_JSON_OBJECT_AS_ARRAY : 0, depth TSRMLS_CC);
}
#endif /* PHP_JSON_H */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/
--TEST--
json_decode() tests
--SKIPIF--
<?php if (!extension_loaded("json")) print "skip"; ?>
--FILE--
<?php
var_dump(json_decode());
var_dump(json_decode(""));
var_dump(json_decode("", 1));
var_dump(json_decode("", 0));
var_dump(json_decode(".", 1));
var_dump(json_decode(".", 0));
var_dump(json_decode("<?>"));
var_dump(json_decode(";"));
var_dump(json_decode("руссиш"));
var_dump(json_decode("blah"));
var_dump(json_decode(NULL));
var_dump(json_decode('{ "test": { "foo": "bar" } }'));
var_dump(json_decode('{ "test": { "foo": "" } }'));
var_dump(json_decode('{ "": { "foo": "" } }'));
var_dump(json_decode('{ "": { "": "" } }'));
var_dump(json_decode('{ "": { "": "" }'));
var_dump(json_decode('{ "": "": "" } }'));
?>
===DONE===
--EXPECTF--
Warning: json_decode() expects at least 1 parameter, 0 given in %s on line %d
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
object(stdClass)#%d (1) {
["test"]=>
object(stdClass)#%d (1) {
["foo"]=>
string(3) "bar"
}
}
object(stdClass)#%d (1) {
["test"]=>
object(stdClass)#%d (1) {
["foo"]=>
string(0) ""
}
}
object(stdClass)#%d (1) {
["_empty_"]=>
object(stdClass)#%d (1) {
["foo"]=>
string(0) ""
}
}
object(stdClass)#%d (1) {
["_empty_"]=>
object(stdClass)#%d (1) {
["_empty_"]=>
string(0) ""
}
}
NULL
NULL
===DONE===
--TEST--
json_encode() tests
--SKIPIF--
<?php if (!extension_loaded("json")) print "skip"; ?>
--FILE--
<?php
var_dump(json_encode(""));
var_dump(json_encode(NULL));
var_dump(json_encode(TRUE));
var_dump(json_encode(array(""=>"")));
var_dump(json_encode(array(array(1))));
var_dump(json_encode(array()));
var_dump(json_encode(array(""=>""), JSON_FORCE_OBJECT));
var_dump(json_encode(array(array(1)), JSON_FORCE_OBJECT));
var_dump(json_encode(array(), JSON_FORCE_OBJECT));
var_dump(json_encode(1));
var_dump(json_encode("руссиш"));
echo "Done\n";
?>
--EXPECTF--
string(2) """"
string(4) "null"
string(4) "true"
string(7) "{"":""}"
string(5) "[[1]]"
string(2) "[]"
string(7) "{"":""}"
string(13) "{"0":{"0":1}}"
string(2) "{}"
string(1) "1"
string(38) ""\u0440\u0443\u0441\u0441\u0438\u0448""
Done
--TEST--
json_encode() & endless loop - 1
--SKIPIF--
<?php if (!extension_loaded("json")) print "skip"; ?>
--FILE--
<?php
$a = array();
$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());
echo "Done\n";
?>
--EXPECTF--
array(1) {
[0]=>
&array(1) {
[0]=>
*RECURSION*
}
}
bool(false)
int(6)
string(%d) "Recursion detected"
string(8) "[[null]]"
int(6)
string(%d) "Recursion detected"
Done
--TEST--
json_encode() & endless loop - 2
--SKIPIF--
<?php if (!extension_loaded("json")) print "skip"; ?>
--FILE--
<?php
$a = new stdclass;
$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";
?>
--EXPECTF--
object(stdClass)#%d (1) {
["prop"]=>
*RECURSION*
}
bool(false)
int(6)
string(%d) "Recursion detected"
string(22) "{"prop":{"prop":null}}"
int(6)
string(%d) "Recursion detected"
Done
--TEST--
json_encode() & endless loop - 3
--SKIPIF--
<?php if (!extension_loaded("json")) print "skip"; ?>
--FILE--
<?php
$a = array();
$a[] = $a;
var_dump($a);
var_dump(json_encode($a));
echo "Done\n";
?>
--EXPECTF--
array(1) {
[0]=>
array(0) {
}
}
string(4) "[[]]"
Done
--TEST--
json_encode() & extended encoding
--SKIPIF--
<?php if (!extension_loaded("json")) print "skip"; ?>
--FILE--
<?php
$a = array('<foo>', "'bar'", '"baz"', '&blong&');
echo "Normal: ", json_encode($a), "\n";
echo "Tags: ", json_encode($a, JSON_HEX_TAG), "\n";
echo "Apos: ", json_encode($a, JSON_HEX_APOS), "\n";
echo "Quot: ", json_encode($a, JSON_HEX_QUOT), "\n";
echo "Amp: ", json_encode($a, JSON_HEX_AMP), "\n";
echo "All: ", json_encode($a, JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_QUOT|JSON_HEX_AMP), "\n";