Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot appear to have object as collection root #383

Open
PeteDevoy opened this issue May 2, 2017 · 3 comments
Open

Cannot appear to have object as collection root #383

PeteDevoy opened this issue May 2, 2017 · 3 comments
Labels

Comments

@PeteDevoy
Copy link

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?

@jongbelegen
Copy link

jongbelegen commented Dec 5, 2018

I got it working by returning an assoc array:

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

@johannesschobel
Copy link
Contributor

Dear @PeteDevoy ,

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

@stale
Copy link

stale bot commented Apr 16, 2022

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 stale bot added the stale label Apr 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants