[cgiapp] Re: CAP::JSON and UTF8

Michael Peters mpeters at plusthree.com
Mon Feb 2 15:51:20 EST 2009


Mike Tonks wrote:

> Hope it's ok to mail you directly. 

Yeah, that's fine, but I'm cc'ing the C::A list so that others can benefit from this discussion. 
Also, sorry for taking so long to get back to you on this.

> I recently added your
> CGI::Application::Plugin::JSON module to my project, and was very
> pleased until I noticed my french language stuff with utf8 characters,
> accents and so on, was mangled in the output.
> 
> I have temporarily abandonned the plugin and replaced the code as follows:
> 
> # send the JSON in the document body (removed)
> #$self->json_body( $data );
> 
> # Signal to JSON::Any to use utf8
> $ENV{JSON_ANY_CONFIG}     = "utf8=1";
> 
> my $j = JSON::Any->new;
> my $json = $j->encode($data);
> 
> return $json;

Since CAP::JSON uses JSON::Any and it calls encode() then you should just be able to set 
$ENV{JSON_ANY_CONFIG} = "utf8=1" in your application somewhere (probably in the prerun or init stages).

> Just wondered if you had come across this and had any thoughts or suggestions.

But since the JSON_ANY_CONFIG thing isn't documented anywhere, it's probably best to use something 
else. How about $self->json_args()? It would just keep the args around and pass them to JSON::Any's 
new() method. So in your init/prerun you'd do something like this:

   $self->json_args(utf8 => 1);

And now that I think about it, all these json_* methods are kind of annoying. I probably should have 
gone with $self->json->* instead. Oh well...

-- 
Michael Peters
Plus Three, LP



More information about the cgiapp mailing list