http://www.perlmonks.org?node_id=950357


in reply to JSON saving regex?

The problem is that qr/.../ produces a Regexp object (try ref(qr/.../)).

If you know where all the regexes are in your structure, you could convert them all to strings before calling to_json() on them.

Or you can do:
{ package Regexp; sub TO_JSON { my $regex = shift; $regex = "$regex"; return $regex; } my $json = JSON->new->convert_blessed; print $json->encode(qr/.../);
as documented in JSON

Replies are listed 'Best First'.
Re^2: JSON saving regex?
by The Perlman (Scribe) on Jan 27, 2012 at 21:09 UTC
    Excellent!

    Thanks for showing that this solution exists.

    (I hoped it would, but was too lazy to look it up :)

    Just one remark: I hope the OP doesn't expect that those RegExes always work in other languages than Perl.