I'm working on a Silverlight 3 application that has to communicate with a PHP 5.2.13 server app. We're using JSON.NET to finish the job, but I'm having some problems with Dictionaries.
I'm doing some experiments and trying to deserialize an object that contains a Dictionary:
public Dictionary<string, Block> Table
{
get { return m_table; }
set { m_table = value; }
}
C# serializes properly and I'm happy with it, but on the PHP side, when serializing an equivalent object that has an empty Table
, it won't work.
$this->Table = array();
The problem is that empty arrays, obviously, aren't considered an assoc array and so they are exported as []
instead of {}
.
I thought of adding something like 'null' => null
to the array (force assoc) and then do some clean-up in the client, but I don't control the client C# objects neither I can constraint them to be nullable so... I'm stuck on this one ;)
Do you know of any solution?
Thanks for your time, very much appreciated :)
EDIT: To clarify, I can't control the structure of both, the C# and PHP objects. On my test I've created an object which contains a dictionary but the hole object gets encoded at once. Here's an over simplified version of it:
class Block
{
public $X = 0;
public $Y = 0;
public $Name = '';
public $Children = array();
public $Table = array();
public $Nested = null;
}
Where Table
should be a dictionary and encoded as
echo json_encode( new Block() );
json_encode
function, or some other library?