4

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() );
3
  • On the PHP side, are you using the built-in json_encode function, or some other library? Dec 2, 2010 at 0:33
  • Ok, good. Check out my answer- I think it solves your problem :) Dec 2, 2010 at 0:44
  • Edited my question, sorry for not clarifying it before ;)
    – DiogoNeves
    Dec 2, 2010 at 1:01

2 Answers 2

3

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.

From: http://www.php.net/manual/en/function.json-encode.php

EDIT

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

$b = array();
json_encode((object)$b);

EDIT

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.

10
  • @SimpleCoder Unfortunately it is only available on PHP 5.3 (unless I made a stupid mistake) but I tried and it complained about having more than 1 function parameter. Thanks :)
    – DiogoNeves
    Dec 2, 2010 at 0:45
  • 1
    The problem is, the array is contained inside an object already, which I don't control. Let me edit my question but I've voted up on yours ;)
    – DiogoNeves
    Dec 2, 2010 at 0:55
  • I've posted a hackish solution. :) Dec 2, 2010 at 1:09
  • Now it fails for arrays of objects (that I deserialize to List<Block>) but I might have to set something like 'arrays as objects'... I'm going to wait just a little bit for a less hackish solution, if you don't mind but I'll mark this as correct in case it doesn't happen. Thanks :)
    – DiogoNeves
    Dec 2, 2010 at 1:57
  • Now it fails for arrays of objects - Could you post an example of this? Dec 2, 2010 at 1:58
1

I prefer the 2nd method by simplecode, which works in this case

<?php
$b = array(
    "a" => array(),
    "b" => (object)array()
);

echo "Empty array output as array: " . json_encode($b) . "\n";
?>
1
  • Thank you :) I'm also looking at this, great stuff hehe
    – DiogoNeves
    Dec 15, 2010 at 12:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.