DBIx-Class-Async

 view release on metacpan or  search on metacpan

lib/DBIx/Class/Async/Schema.pm  view on Meta::CPAN

            # Explicit check for JSON Serializer
            elsif ($info->{serializer_class}
                   && $info->{serializer_class} eq 'JSON') {
                require JSON;
                my $json = JSON->new->utf8->allow_nonref;
                $map->{$source_name}{$col} = {
                    inflate => sub {
                        my $val = shift;
                        return $val if !defined $val || ref($val);

                        my $decoded;
                        eval  {
                            $decoded = $json->decode($val); 1;
                        }
                        or do {
                            warn "Failed to inflate JSON in $col: $@ (Value: $val)";
                            return $val;
                        };
                        return $decoded;
                    },
                    deflate => sub {
                        my $val = shift;
                        return $val if !defined $val || !ref($val);

                        return $json->encode($val);
                    },
                };
            }
        }

t/040-inflation.t  view on Meta::CPAN

);

$schema->inflate_column('Product', 'metadata', {
    inflate => sub {
        my $val = shift;
        return {} unless defined $val;

        return $val if ref $val;

        if ($val !~ /^HASH\(0x/) {
            my $decoded = eval { $json->decode($val) };
            return $decoded if $decoded;
        }

        return {};
    },
    deflate => sub {
        my $hashref = shift;
        return undef unless defined $hashref;
        # Turn the HashRef back into a JSON string for the DB
        return ref($hashref) ? $json->encode($hashref) : $hashref;
    },



( run in 0.617 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )