[Catalyst] How to de-serialize json?

J. Shirley jshirley at gmail.com
Wed Jan 27 15:51:51 GMT 2010


On Wed, Jan 27, 2010 at 7:33 AM, Bill Moseley <moseley at hank.org> wrote:
>
>
> On Tue, Jan 26, 2010 at 6:33 AM, J. Shirley <jshirley at gmail.com> wrote:
>>
>>
>> I'm all for reusable code, but in no way should HTTP::Body start
>> taking this behavior by default.  I'm not really that sure how
>> effective it is, anyway.
>
> No, I was not suggesting that would be the default (although I'm not sure
> why not handling other serializations by default is a bad idea).  Not sure
> what you mean by "effective".
>
>>
>> decode_json( $c->req->body ); Is just not that hard :)
>
> Of course it's not that hard.  Of course, this isn't hard, either:  [1]
>
> map { s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg; $_ } split /[&;=]/,
> $c->req->body;
>
> I see those as similar operations.  The request is serialized in both cases.
>
> But, one should not have to worry about adding low-level details like that
> to application code when using an elegant web framework. ;)
>
>
> No big deal.  I was just curious why the HTTP::Body approach was not used in
> the existing REST/RPC modules, as that was already the place used by
> Catalyst to de-serialize the body.  I thought maybe there was a reason I
> might not understood, which is why I asked.
>
> [1] Or whatever the correct approach is, and apologies to Damian for the
> map.
>

(Really not trying to beat a dead horse, but I think this thread does
have some useful thoughts in it so I'd like to just summarize)

Well, I think that Data::Serializer is what you are after.  As far as
putting that feature directly in HTTP::Body, I'm not really sure what
it gains you since deserializing is not something you always want to
do unless your application can specify it.  Decoding, however, is
something you do want regardless of application.

And if your application specifies it, then it seems reasonable to me
to put it in the framework layer.  It seems like you could probably
have a request trait that could either automatically or lazily
deserialize things, and that would make everybody happy.  So,
something like Catalyst::TraitFor::Request::Deserialize would be
useful.  Alternatively, you could also write this as a Plack
middleware.

-J



More information about the Catalyst mailing list