THRIFT-3126 PHP JSON serializer converts empty or int-indexed maps to lists
Client: php/test/Test/Thrift/JsonSerialize/JsonSerializeTest.php
Patch: Stig Bakken <stig@zedge.net>
This closes #479
diff --git a/compiler/cpp/src/generate/t_php_generator.cc b/compiler/cpp/src/generate/t_php_generator.cc
index 6c7da41..8919877 100644
--- a/compiler/cpp/src/generate/t_php_generator.cc
+++ b/compiler/cpp/src/generate/t_php_generator.cc
@@ -1092,7 +1092,11 @@
}
indent(out) << "if ($this->" << name << " !== null) {" << endl;
indent_up();
- indent(out) << "$json->" << name << " = $this->" << name << ";" << endl;
+ indent(out) << "$json->" << name << " = ";
+ if (type->is_map()) {
+ out << "(object)";
+ }
+ out << "$this->" << name << ";" << endl;
indent_down();
indent(out) << "}" << endl;
}
diff --git a/lib/php/test/Test/Thrift/JsonSerialize/JsonSerializeTest.php b/lib/php/test/Test/Thrift/JsonSerialize/JsonSerializeTest.php
index 5c15bdf..40003ed 100644
--- a/lib/php/test/Test/Thrift/JsonSerialize/JsonSerializeTest.php
+++ b/lib/php/test/Test/Thrift/JsonSerialize/JsonSerializeTest.php
@@ -92,4 +92,12 @@
$this->assertEquals($expected, json_decode(json_encode($nested)));
}
+ public function testMaps()
+ {
+ $intmap = new \ThriftTest\ThriftTest_testMap_args(['thing' => [0 => 'zero']]);
+ $emptymap = new \ThriftTest\ThriftTest_testMap_args([]);
+ $this->assertEquals('{"thing":{"0":"zero"}}', json_encode($intmap));
+ $this->assertEquals('{}', json_encode($emptymap));
+ }
+
}