File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / php / ext / json / tests / serialize.phpt
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Tue May 29 12:34:40 2012 UTC (12 years, 5 months ago) by misho
Branches: php, MAIN
CVS tags: v5_4_3elwix, v5_4_29p0, v5_4_29, v5_4_20p0, v5_4_20, v5_4_17p0, v5_4_17, HEAD
php 5.4.3+patches

--TEST--
json_encode() Serialization tests
--SKIPIF--
<?php if (!extension_loaded("json")) print "skip"; ?>
--FILE--
<?php

class NonSerializingTest
{
	public $data;

	public function __construct($data)
	{
		$this->data = $data;
	}
}

class SerializingTest extends NonSerializingTest implements JsonSerializable
{
	public function jsonSerialize()
	{
		return $this->data;
	}
}

class ValueSerializingTest extends SerializingTest
{
	public function jsonSerialize()
	{
		return array_values(is_array($this->data) ? $this->data : get_object_vars($this->data));
	}
}

class SelfSerializingTest extends SerializingTest
{
	public function jsonSerialize()
	{
		return $this;
	}
}

$adata = array(
	'str'	=> 'foo',
	'int'	=> 1,
	'float'	=> 2.3,
	'bool'	=> false,
	'nil'	=> null,
	'arr'	=> array(1,2,3),
	'obj'	=> new StdClass,
);

$ndata = array_values($adata);

$odata = (object)$adata;

foreach(array('NonSerializingTest','SerializingTest','ValueSerializingTest','SelfSerializingTest') as $class) {
	echo "==$class==\n";
	echo json_encode(new $class($adata)), "\n";
	echo json_encode(new $class($ndata)), "\n";
	echo json_encode(new $class($odata)), "\n";
}
--EXPECT--
==NonSerializingTest==
{"data":{"str":"foo","int":1,"float":2.3,"bool":false,"nil":null,"arr":[1,2,3],"obj":{}}}
{"data":["foo",1,2.3,false,null,[1,2,3],{}]}
{"data":{"str":"foo","int":1,"float":2.3,"bool":false,"nil":null,"arr":[1,2,3],"obj":{}}}
==SerializingTest==
{"str":"foo","int":1,"float":2.3,"bool":false,"nil":null,"arr":[1,2,3],"obj":{}}
["foo",1,2.3,false,null,[1,2,3],{}]
{"str":"foo","int":1,"float":2.3,"bool":false,"nil":null,"arr":[1,2,3],"obj":{}}
==ValueSerializingTest==
["foo",1,2.3,false,null,[1,2,3],{}]
["foo",1,2.3,false,null,[1,2,3],{}]
["foo",1,2.3,false,null,[1,2,3],{}]
==SelfSerializingTest==
{"data":{"str":"foo","int":1,"float":2.3,"bool":false,"nil":null,"arr":[1,2,3],"obj":{}}}
{"data":["foo",1,2.3,false,null,[1,2,3],{}]}
{"data":{"str":"foo","int":1,"float":2.3,"bool":false,"nil":null,"arr":[1,2,3],"obj":{}}}



FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>