Skip to content

Allow serialization to array, not just encoded string #169

@lindyhopchris

Description

@lindyhopchris

There are occasions when it is necessary to serialize a JSON API payload to an array, rather than the encoded string that Encoder currently returns.

For example, in Laravel when broadcasting data via a websocket, Laravel expects the data as an array rather than a string. This is typically because third-party PHP libraries will also expect the data as an array.
https://laravel.com/docs/5.4/broadcasting#broadcast-data

My suggestion would be for there to be an interface that has a similar pattern as the EncoderInterface, but returns arrays. E.g. if it was called SerializerInterface then:

EncoderInterface SerializerInterface
withLinks withLinks
withMeta withMeta
withJsonApiVersion withJsonApiVersion
withRelationshipSelfLink withRelationshipRelatedLink
encodeData serializeData
encodeIdentifiers serializeIdentifiers
encodeError serializeError
encodeErrors serializeErrors
encodeMeta serializeMeta

Then Encoder would take a Serializer instance in its constructor, use that to get the array that it then encodes to a string. This would allow you to create just a serializer if you didn't want to encode the JSON API document to a string. I.e. Encoder::instance() and Serializer::instance() would both exist.

I'm happy to submit a PR for this feature, but thought I'd run it past you first.

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions