开发者

Serializng and deserializing empty arrays to Dictionary/object

开发者 https://www.devze.com 2023-01-27 17:42 出处:网络
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 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.

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.


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";
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号