public function serializeCollection(VisitorInterface $visitor, Collection $collection, array $type, Context $context) { // We change the base type, and pass through possible parameters. $type['name'] = 'array'; //don't include items that will produce null elements $dataArray = []; foreach ($collection->toArray() as $element) { if (!$context->isVisiting($element)) { $dataArray[] = $element; } } return $visitor->visitArray($dataArray, $type, $context); }