Commit 1e332766 authored by hlarget's avatar hlarget 🥑

Merge branch '6-remove-null-fields' into 'master'

"remove null fields"

Closes #6

See merge request open-source/agrabbitmq!7
parents bb0a6f34 286634e9
......@@ -14,7 +14,7 @@ class JsonSerializer
public static function getInstance()
{
if (self::$serializer == null) {
if (self::$serializer === null) {
$encoders = [new JsonEncoder()];
$normalizers = [
new \Symfony\Component\Serializer\Normalizer\DateTimeNormalizer(\DateTime::ISO8601),
......@@ -34,6 +34,24 @@ class JsonSerializer
*/
public static function stringify($object)
{
return self::getInstance()->serialize($object, "json");
$array = self::getInstance()->normalize($object);
$arrayfiltered = self::array_filter_recursive($array, function($v){return $v !== null;});
$jsonEncoder = new JsonEncoder();
$object = $jsonEncoder->encode($arrayfiltered, 'json');
return $object;
}
public static function array_filter_recursive($input, $callback = null)
{
foreach ($input as &$value)
{
if (is_array($value))
{
$value = self::array_filter_recursive($value, $callback);
}
}
return array_filter($input, $callback);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment