From 178953910c18d36ebf16daaa3497123cec77b022 Mon Sep 17 00:00:00 2001 From: Stig Bakken Date: Fri, 24 Jul 2015 01:41:33 +0200 Subject: [PATCH] PHP Compiler: always cast scalar types in jsonSerialize() --- compiler/cpp/src/generate/t_php_generator.cc | 2 ++ .../Test/Thrift/JsonSerialize/JsonSerializeTest.php | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/compiler/cpp/src/generate/t_php_generator.cc b/compiler/cpp/src/generate/t_php_generator.cc index 8919877415c..6c4dd6b4abf 100644 --- a/compiler/cpp/src/generate/t_php_generator.cc +++ b/compiler/cpp/src/generate/t_php_generator.cc @@ -1095,6 +1095,8 @@ void t_php_generator::generate_php_struct_json_serialize(ofstream& out, 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 40003ed1494..2471b520b03 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 @@ public function testMaps() $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)); + } + }