PHP Compiler: always cast scalar types in jsonSerialize()
diff --git a/compiler/cpp/src/generate/t_php_generator.cc b/compiler/cpp/src/generate/t_php_generator.cc
index c6d6f99..0c255ec 100644
--- a/compiler/cpp/src/generate/t_php_generator.cc
+++ b/compiler/cpp/src/generate/t_php_generator.cc
@@ -1097,6 +1097,8 @@
indent(out) << "$json->" << name << " = ";
if (type->is_map()) {
out << "(object)";
+ } else {
+ out << type_to_cast(type);
}
out << "$this->" << name << ";" << endl;
indent_down();
diff --git a/lib/php/test/Test/Thrift/JsonSerialize/JsonSerializeTest.php b/lib/php/test/Test/Thrift/JsonSerialize/JsonSerializeTest.php
index 40003ed..2471b52 100644
--- a/lib/php/test/Test/Thrift/JsonSerialize/JsonSerializeTest.php
+++ b/lib/php/test/Test/Thrift/JsonSerialize/JsonSerializeTest.php
@@ -33,6 +33,11 @@
class JsonSerializeTest extends \PHPUnit_Framework_TestCase
{
+ protected function setUp() {
+ if (version_compare(phpversion(), '5.4', '<')) {
+ $this->markTestSkipped('Requires PHP 5.4 or newer!');
+ }
+ }
public function testEmptyStruct()
{
@@ -100,4 +105,12 @@
$this->assertEquals('{}', json_encode($emptymap));
}
+ public function testScalarTypes()
+ {
+ $b = new \ThriftTest\Bools(['im_true' => '1', 'im_false' => '0']);
+ $this->assertEquals('{"im_true":true,"im_false":false}', json_encode($b));
+ $s = new \ThriftTest\StructA(['s' => 42]);
+ $this->assertEquals('{"s":"42"}', json_encode($s));
+ }
+
}