Serializng and deserializing empty arrays to Dictionary/object

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() );


You can use the JSON_FORCE_OBJECT flag to force [] to become {}, like so:

$b = array();

echo "Empty array output as array: " . json_encode($b) . "\n";
echo "Empty array output as object: " . json_encode($b, JSON_FORCE_OBJECT);

The output:


Note that without this option on, only associative arrays are encoded using object notation.



According to this question, casting the data to an object before encoding it will work:

$b = array();


The way I would solve this is a little hackish, but it will work:

$block = new Block();

$json = json_encode($block);
$json = str_replace("[]", "{}", $json);

echo $json;

This searches the resultant JSON for [] and replaces it with {}. The only problem with this that you have to be aware of is if, for example, Name is []. It will be changed to {}. You could get around this by parsing the JSON and reconstructing it, replacing [] with {} when it is not part of a string literal. But, you may be able to make the assumption that [] will never be part of a string literal.

Need Your Help

undefined method `namespace'

ruby-on-rails ruby gem sinatra

I'm trying implement a namespace(with sinatra-contrib gem) routing for Sinatra app and have an error after I'm trying to start the server:

How to make a POST request using AJAX in a Chrome Extension?

javascript php ajax google-chrome google-chrome-extension

I am attempting to make an ajax call when the browser is on my webpage it works perfectly but as soon as I leave my domain, it fails. This is for a closed system that user knows they are being trac...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.