Laravel-json-api: XML Encoder/Decoder

Created on 12 Mar 2018  路  7Comments  路  Source: cloudcreativity/laravel-json-api

First off: Awesome package! My Praises!

I had a question regarding extensibility: Is it possible with de Encoder/Decoder to return XML documents in this format as well? if so how? Just some guidance would help a lot.

question

All 7 comments

Hey! Glad you like the package and sorry for taking a bit of time to reply.

In theory this would be possible as the encoder/decoder are interfaces.

You'd need to write an XML encoder that implements this interface:
https://github.com/neomerx/json-api/blob/v1.x/src/Contracts/Encoder/EncoderInterface.php

And a decoder that implements this interface:
https://github.com/neomerx/json-api/blob/v1.x/src/Contracts/Decoder/DecoderInterface.php

For you to tie this into my extensions to the neomerx/jsonapi package. you'd need to return an array from the decoder that is in the expected JSON API document format. (I.e. your array must look like it would if it had come in via JSON rather than XML.)

If you get to the point of having an encoder and decoder that work behind these interfaces, let me know and I'll give you some guidance as to how to wire it in. It's possible I might have to make one or two minor changes to this package to do that, but it totally should be possible for you to provide your own encoder/decoder so I'd be happy to make any adjustments that are needed.

He Chris,

No problem. We're probably both busy. It's amazing how far you've come. I've been looking at all the packages for json-api (limoncello, neomerx etc.), but it's so much code and abstraction. It's hard to just 'get in there'.

i'll first experiment some more with this package to get more confidence in my knowledge of this package, but already so thankful for what you're trying to achieve (no one achieved this so far...)

@Nizari no problem! yes that packages are quite abstract underneath. I'm actually planning to merge my cloudcreativity/json-api package into this one, as we now don't use it for anything except in Laravel - hoping that merging them means we can simplify the code a bit.

I'll close this issue for the moment but do feel free to open issues with follow up questions once you start digging into things.

When I have written a custom Encoder, e.g. for CSV, where would be the place the set this encoder? custom ContentNegotiator with something like this?

...
protected $encoding = [
        'application/csv' =>  \App\Encoders\CsvEncoder::class
    ];
...

@fibis, you should try it.
By docs: i supposed to do it like this:
(custom ContentNegotiator with method like this)

...
protected function decodingsForResource(?Media $media): DecodingList
    {
        $decoder = new FileDecoder();

        return $this
            ->decodingMediaTypes()
            ->when(is_null($media), Decoding::create('multipart/form-data', $decoder))
            ->when(is_null($media), Decoding::create('multipart/form-data; boundary=*', $decoder));
    }
...

https://laravel-json-api.readthedocs.io/en/latest/features/media-types/

@fibis yes the instructions in the Media Types (Content Negotiation) chapter are the ones to follow now if you want to do XML encoding. You'll need to look at the Custom Encoding section.

@Warchiefs @lindyhopchris Thank you both very much for the response, i will give it a try. I was only a little bit confused that the decoding part was more detailed than the encoding part.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

daryledesilva picture daryledesilva  路  6Comments

tembra picture tembra  路  6Comments

petenys picture petenys  路  5Comments

defunctl picture defunctl  路  6Comments

JeanLucEsser picture JeanLucEsser  路  6Comments