diff --git a/.gitignore b/.gitignore index f9418e4..f1a0a83 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ build/* composer.lock phpjsonable.iml .idea/* -vendor/* \ No newline at end of file +vendor/* +index.php \ No newline at end of file diff --git a/grinfeld_phpjsonable.phar b/grinfeld_phpjsonable.phar index 84663cf..756848e 100644 Binary files a/grinfeld_phpjsonable.phar and b/grinfeld_phpjsonable.phar differ diff --git a/src/transformers/BeanTransformer.php b/src/transformers/BeanTransformer.php index 2f56ff6..dc44344 100644 --- a/src/transformers/BeanTransformer.php +++ b/src/transformers/BeanTransformer.php @@ -40,8 +40,19 @@ public function transform($obj, OutputStream $output, Configuration $conf) { $clazzName = $conf->getString(Configuration::CLASS_PROPERTY, Configuration::DEFAULT_CLASS_PROPERTY_VALUE); $clazzStrategy = $conf->getString(Configuration::CLASS_TYPE_PROPERTY, LanguageStrategyFactory::LANG_PHP); foreach($props as $prop) { - $prop->setAccessible(true); - $val = $prop->getValue($obj); + // check getter + $val = null; + try { + $refMethod = new \ReflectionMethod($clazz, "get" . ucfirst($prop->getName())); + if ($refMethod->isPublic()) + $val = $refMethod->invoke($obj); + } catch (\Exception $e) { + echo $e->getMessage(); + } + if ($val == null) { + $prop->setAccessible(true); + $val = $prop->getValue($obj); + } if ($excludeNull === false || ($val !== null || $val !== "")) { if ($prop->getName() != $clazzName) { if ($i != 0) diff --git a/src/transformers/MapTransformer.php b/src/transformers/MapTransformer.php index 48843d7..ff09efe 100644 --- a/src/transformers/MapTransformer.php +++ b/src/transformers/MapTransformer.php @@ -20,6 +20,10 @@ class MapTransformer implements Transformer { public function match($obj) { if (!is_array($obj)) return false; + + if (is_object($obj)) + return false; + return true; } diff --git a/test/parsers/json/WriterTest.php b/test/parsers/json/WriterTest.php index 8d2a229..aaf0d20 100644 --- a/test/parsers/json/WriterTest.php +++ b/test/parsers/json/WriterTest.php @@ -127,5 +127,17 @@ public function testParse() { Json::encode($pair, $output, (new Configuration())->push(Configuration::INCLUDE_CLASS_NAME_PROPERTY, "true")->push(Configuration::EXCLUDE_NULL_PROPERTY, "false")); $this->assertEquals($expected, $output->toString(), "Should be $expected"); + $output = new StringOutputStream(); + Json::encode(new SimpleBean(), $output, (new Configuration())->push(Configuration::INCLUDE_CLASS_NAME_PROPERTY, "false")->push(Configuration::EXCLUDE_NULL_PROPERTY, "false")); + $this->assertEquals("{\"val\":\"123\"}", $output->toString(), "Should be 123"); } -} \ No newline at end of file +} + + class SimpleBean { + protected $val = "1"; + + /** + * @return mixed + */ + public function getVal() { return "123"; } + } \ No newline at end of file