Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
[ZF-11183] ensure arrays of objects are deserialized
Browse files Browse the repository at this point in the history
- Ensure arrays containing objects are de-serialized to arrays when
  deserializing JSON
  • Loading branch information
weierophinney committed Mar 16, 2011
1 parent 9d0c069 commit 9ce83ec
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,13 @@ protected function flattenObjects($config)
if (is_object($value)) {
$value = $this->flattenObjects($value);
}
if (is_array($value)) {
foreach ($value as $k => $v) {
if (is_object($v)) {
$value[$k] = $this->flattenObjects($v);
}
}
}
$flattened[$key] = $value;
}
return $flattened;
Expand Down
7 changes: 7 additions & 0 deletions test/JsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,11 @@ public function testIgnoringConstantsCanLeadToParseErrors()
$this->setExpectedException('Zend\Json\Exception', 'Syntax');
$config = new JsonConfig($json, null, array('ignore_constants' => true));
}

public function testNestedConfigSetsAreArrays()
{
$config = new JsonConfig(__DIR__ . '/_files/nested.json', 'testing');
$array = $config->toArray();
$this->assertInternalType('array', $array['definitions'][0]);
}
}
54 changes: 54 additions & 0 deletions test/_files/nested.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"testing": {
"definitions": [
{
"class": "Zend\\Di\\TestAsset\\Struct",
"params": {
"param1": "foo",
"param2": "bar"
},
"param_map": {
"param1": 0,
"param2": 1
}
},
{
"class": "Zend\\Di\\TestAsset\\DummyParams",
"constructor_callback": {
"class": "Zend\\Di\\TestAsset\\StaticFactory",
"method": "factory"
},
"params": {
"struct": { "__reference": "struct" },
"params": {"foo": "bar"}
},
"param_map": {
"struct": 0,
"params": 1
}
},
{
"class": "Zend\\Di\\TestAsset\\InjectedMethod",
"methods": [
{
"name": "setObject",
"args": [ { "__reference": "params" } ]
}
]
},
{
"class": "Zend\\Di\\TestAsset\\InspectedClass",
"params": {
"baz": "BAZ",
"foo": "FOO"
}
}
],
"aliases": {
"struct": "Zend\\Di\\TestAsset\\Struct",
"params": "Zend\\Di\\TestAsset\\DummyParams",
"injected": "Zend\\Di\\TestAsset\\InjectedMethod",
"inspected": "Zend\\Di\\TestAsset\\InspectedClass"
}
}
}

0 comments on commit 9ce83ec

Please sign in to comment.