c# - Serializng and deserializing empty arrays to Dictionary/object -
i'm working on silverlight 3 application has communicate php 5.2.13 server app. we're using json.net finish job, i'm having problems dictionaries.
i'm doing experiments , trying deserialize object contains dictionary:
public dictionary<string, block> table { { return m_table; } set { m_table = value; } } c# serializes , i'm happy it, on php side, when serializing equivalent object has empty table, won't work.
$this->table = array(); the problem empty arrays, obviously, aren't considered assoc array , exported [] instead of {}.
i thought of adding 'null' => null array (force assoc) , clean-up in client, don't control client c# objects neither can constraint them nullable so... i'm stuck on 1 ;)
do know of solution?
thanks time, appreciated :)
edit: clarify, can't control structure of both, c# , php objects. on test i've created object contains dictionary hole object gets encoded @ once. here's on 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 dictionary , encoded as
echo json_encode( new block() );
you can use json_force_object flag force [] become {}, so:
$b = array(); echo "empty array output array: " . json_encode($b) . "\n"; echo "empty array output object: " . json_encode($b, json_force_object); the output:
[] {} note without option on, associative arrays encoded using object notation.
from: http://www.php.net/manual/en/function.json-encode.php
edit
according this question, casting data object before encoding work:
$b = array(); json_encode((object)$b); edit
the way solve little hackish, work:
$block = new block(); $json = json_encode($block); $json = str_replace("[]", "{}", $json); echo $json; this searches resultant json [] , replaces {}. problem have aware of if, example, name []. changed {}. around parsing json , reconstructing it, replacing [] {} when not part of string literal. but, may able make assumption [] never part of string literal.
Comments
Post a Comment