Module-Generic

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

    directly for each serialiser:

    *   CBOR::Free: "canonical", "string_encode_mode",
        "preserve_references", "scalar_references"

    *   CBOR: "max_depth", "max_size", "allow_unknown", "allow_sharing",
        "allow_cycles", "forbid_objects", "pack_strings", "text_keys",
        "text_strings", "validate_utf8", "filter"

    *   JSON: "allow_blessed" "allow_nonref" "allow_unknown" "allow_tags"
        "ascii" "boolean_values" "canonical" "convert_blessed"
        "filter_json_object" "filter_json_single_key_object" "indent"
        "latin1" "max_depth" "max_size" "pretty" "relaxed" "space_after"
        "space_before" "utf8"

    *   "encode" in Sereal::Decoder if the serialiser is Sereal:
        "aliased_dedupe_strings", "canonical", "canonical_refs", "compress",
        "compress_level", "compress_threshold", "croak_on_bless",
        "dedupe_strings", "freeze_callbacks", "max_recursion_depth",
        "no_bless_objects", "no_shared_hashkeys", "protocol_version",
        "snappy", "snappy_incr", "snappy_threshold", "sort_keys",

README.md  view on Meta::CPAN

    Boolean. If true, this will lock the file before writing to it. This works only in conjonction with _file_ and the serialiser [Storable::Improved](https://metacpan.org/pod/Storable%3A%3AImproved)

- `serialiser` or `serializer`

    A string being the class of the serialiser to use. This can be only either [Sereal](https://metacpan.org/pod/Sereal) or [Storable::Improved](https://metacpan.org/pod/Storable%3A%3AImproved)

Additionally the following options are supported and passed through directly for each serialiser:

- [CBOR::Free](https://metacpan.org/pod/CBOR%3A%3AFree): `canonical`, `string_encode_mode`, `preserve_references`, `scalar_references`
- [CBOR](https://metacpan.org/pod/CBOR%3A%3AXS): `max_depth`, `max_size`, `allow_unknown`, `allow_sharing`, `allow_cycles`, `forbid_objects`, `pack_strings`, `text_keys`, `text_strings`, `validate_utf8`, `filter`
- [JSON](https://metacpan.org/pod/JSON): `allow_blessed` `allow_nonref` `allow_unknown` `allow_tags` `ascii` `boolean_values` `canonical` `convert_blessed` `filter_json_object` `filter_json_single_key_object` `indent` `latin1` `max_depth` `max_size` ...
- ["encode" in Sereal::Decoder](https://metacpan.org/pod/Sereal%3A%3ADecoder#encode) if the serialiser is [Sereal](https://metacpan.org/pod/Sereal): `aliased_dedupe_strings`, `canonical`, `canonical_refs`, `compress`, `compress_level`, `compress_thre...
- [Storable::Improved](https://metacpan.org/pod/Storable%3A%3AImproved) / [Storable](https://metacpan.org/pod/Storable): no option available

If an error occurs, this sets an [error](https://metacpan.org/pod/Module%3A%3AGeneric#error) and return `undef`

## serialize

Alias for ["serialise"](#serialise)

## set

lib/Module/Generic.pm  view on Meta::CPAN

        }
        else
        {
            return( $self->error( "No file and no data was provided to deserialise with $class." ) );
        }
    }
    # NOTE: deserialise with JSON
    elsif( $class eq 'JSON' )
    {
        my @options = qw(
            allow_blessed allow_nonref allow_unknown allow_tags ascii boolean_values 
            canonical convert_blessed filter_json_object filter_json_single_key_object
            indent latin1 max_depth max_size pretty relaxed space_after space_before utf8
        );
        my $json = JSON->new;
        for( @options )
        {
            next unless( CORE::exists( $opts->{ $_ } ) );
            if( my $code = $json->can( $_ ) )
            {
                $code->( $json, $opts->{ $_ } );

lib/Module/Generic.pm  view on Meta::CPAN

        {
            my $f = $self->new_file( $opts->{file} ) || return( $self->pass_error );
            $f->unload( $serialised, { binmode => 'raw' } ) || return( $self->pass_error( $f->error ) );
        }
        return( $serialised );
    }
    # NOTE: serialise with JSON
    elsif( $class eq 'JSON' )
    {
        my @options = qw(
            allow_blessed allow_nonref allow_unknown allow_tags ascii boolean_values 
            canonical convert_blessed filter_json_object filter_json_single_key_object
            indent latin1 max_depth max_size pretty relaxed space_after space_before utf8
        );
        my $json = JSON->new;
        for( @options )
        {
            next unless( CORE::exists( $opts->{ $_ } ) );
            if( my $code = $json->can( $_ ) )
            {
                $code->( $json, $opts->{ $_ } );

lib/Module/Generic.pm  view on Meta::CPAN

=back

Additionally the following options are supported and passed through directly for each serialiser:

=over 4

=item * L<CBOR::Free>: C<canonical>, C<string_encode_mode>, C<preserve_references>, C<scalar_references>

=item * L<CBOR|CBOR::XS>: C<max_depth>, C<max_size>, C<allow_unknown>, C<allow_sharing>, C<allow_cycles>, C<forbid_objects>, C<pack_strings>, C<text_keys>, C<text_strings>, C<validate_utf8>, C<filter>

=item * L<JSON>: C<allow_blessed> C<allow_nonref> C<allow_unknown> C<allow_tags> C<ascii> C<boolean_values> C<canonical> C<convert_blessed> C<filter_json_object> C<filter_json_single_key_object> C<indent> C<latin1> C<max_depth> C<max_size> C<pretty> ...

=item * L<Sereal::Decoder/encode> if the serialiser is L<Sereal>: C<aliased_dedupe_strings>, C<canonical>, C<canonical_refs>, C<compress>, C<compress_level>, C<compress_threshold>, C<croak_on_bless>, C<dedupe_strings>, C<freeze_callbacks>, C<max_recu...

=item * L<Storable::Improved> / L<Storable>: no option available

=back

If an error occurs, this sets an L<error|Module::Generic/error> and return C<undef>

=head2 serialize

lib/Module/Generic/File.pm  view on Meta::CPAN

    # Return a Module::Generic::Array object.
    return( $self->new_array( $all ) );
}

sub load_json
{
    my $self = shift( @_ );
    my $opts = $self->_get_args_as_hash( @_ );
    # Inherited from Module::Generic
    my $j = $self->new_json || return( $self->pass_error );
    if( exists( $opts->{boolean_values} ) && 
        $self->_is_array( $opts->{boolean_values} ) )
    {
        $j->boolean_values( @{$opts->{boolean_values}} );
    }
    if( exists( $opts->{filter_json_object} ) && 
        ref( $opts->{filter_json_object} ) eq 'CODE' )
    {
        $j->filter_json_object( $opts->{filter_json_object} );
    }
    if( exists( $opts->{filter_json_single_key_object} ) && 
        ref( $opts->{filter_json_single_key_object} ) eq 'HASH' &&
        scalar( keys( %{$opts->{filter_json_single_key_object}} ) ) &&
        ref( $opts->{filter_json_single_key_object}->{ [keys( %{$opts->{filter_json_single_key_object}} ) ]->[0] } ) eq 'CODE' )

lib/Module/Generic/File.pm  view on Meta::CPAN

This will load the file data, which is assumed to be json in utf8, then decode it using L<JSON> and return the resulting perl data.

This requires the L<JSON> module to be installed or it will set an L<error|Module::Generic/error> and return undef.

If an eror occurs during decoding, this will set an L<error|Module::Generic/error> and return undef.

You can provide the following options to change the way C<JSON> will decode the data:

=over 4

=item * C<boolean_values>

See L<JSON/boolean_values>

=item * C<filter_json_object>

See L<JSON/filter_json_object>

=item * C<filter_json_single_key_object>

See L<JSON/filter_json_single_key_object>

=item * C<decode_prefix>

lib/Module/Generic/Hash.pm  view on Meta::CPAN

        $opts->{indent} = 1;
    }
    if( !CORE::exists( $opts->{relaxed} ) && $opts->{pretty} )
    {
        $opts->{relaxed} = 1;
    }
    my $j = JSON->new->allow_nonref;
    my @keys = qw(
        ascii latin1 utf8 pretty indent space_before space_after relaxed 
        canonical allow_nonref allow_unknown allow_blessed convert_blessed allow_tags 
        boolean_values filter_json_object filter_json_single_key_object max_depth max_size
    );
    foreach my $k ( @keys )
    {
        next unless( CORE::exists( $opts->{ $k } ) );
        my $code = $j->can( $k );
        if( defined( $code ) )
        {
            $code->( $j, $opts->{ $k } );
        }
    }

lib/Module/Generic/JSON.pm  view on Meta::CPAN

=head2 allow_tags

=head2 allow_unknown

=head2 ascii

=head2 backend

=head2 boolean

=head2 boolean_values

=head2 canonical

=head2 convert_blessed

=head2 decode

Decodes a JSON string and returns the resulting Perl data structure. On error, sets an L<error object|Module::Generic::Exception> and returns C<undef> in scalar context or an empty list in list context:

    my $data = $j->decode( '{"a":1}' ) || die( $j->error );



( run in 0.257 second using v1.01-cache-2.11-cpan-9ff20fc0ed8 )