fractal icon indicating copy to clipboard operation
fractal copied to clipboard

Cannot appear to have object as collection root

Open PeteDevoy opened this issue 7 years ago • 3 comments

Please let me know if I am being ignorant here...is it the case that a Collection cannot have an object at the root level? I thought Serializers existed so that the output format could be manipulated but it seems that the Scope class will only play with arrays.

Specifically, I was a trying to create a custom serializer to output GeoJSON, a standard in which collections are objects at the root level, e.g.:

GeoJSON Feature collection example

class GeoJsonSerializer extends SerializerAbstract
{
    public function collection($resourceKey, array $data)
    {
        $featureCollection = new \StdClass();
        $featureCollection->type     = 'FeatureCollection';
        $featureCollection->features = ["foo","bar"]
        return $featureCollection; //does not work
        //return [$featureCollection]; //does work but wraps FeatureCollection object in array
    }

I am not using this with pagination or meta data so I could in theory strip the square brackets from the string after doing $manager->toJson() but it seem like that defeats the point of having a serializer. Please is there some elegant way around this that I am overlooking?

PeteDevoy avatar May 02 '17 15:05 PeteDevoy

I got it working by returning an assoc array:

        return [
            'type' => "FeatureCollection",
            'features' => []
        ];

jongbelegen avatar Dec 05 '18 15:12 jongbelegen

Dear @PeteDevoy ,

i would try to use the JsonApiSerializer for this purpose. From the structure of your GeoJson format, this may be quite similar.

johannesschobel avatar Jun 07 '19 11:06 johannesschobel

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 4 weeks if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Apr 16 '22 06:04 stale[bot]