--------------------------------------------------------
## Module Generic - ~/lib/Module/Generic/JSON.pm
## Version v0.2.2
## Copyright(c) 2025 DEGUEST Pte. Ltd.
## Author: Jacques Deguest <jack@degu
:Generic::JSON;
BEGIN
{
use v5.12.0;
use strict;
use warnings;
use parent qw( Module::Generic );
use vars qw( @EXPORT @EXPORT_OK $AUTOLOAD $DEBUG $VERSION );
use JSON ();
u
= qw( Module::Generic );
our @EXPORT = qw( from_json to_json encode_json decode_json );
our @EXPORT_OK = qw( new_json );
our %EXPORT_TAGS = ();
our $VERSION = 'v0.2.2';
};
= CORE::return( $class->new( $str ) );
}
CORE::return( $new );
}
sub TO_JSON { CORE::return( ${$_[0]} ); }
}
{
# NOTE: Module::Generic::Global::Exception
package
}
}
sub FREEZE
{
my( $self, $serialiser ) = @_;
# $serialiser is 'JSON' for example.
return( $self->TO_JSON );
}
sub STORABLE_freeze
{
my( $self, $is_cloning ) = @_;
my $data
( keys( %$ref ) )
{
$hash{ $k } = $ref->{ $k };
}
return( $self );
}
sub TO_JSON
{
my $self = shift( @_ );
my $data = $self->{data};
my $repo = $self->{object_repo};
{
$new = CORE::return( $class->new( $str ) );
}
CORE::return( $new );
}
sub TO_JSON { CORE::return( ${$_[0]} ); }
# NOTE: Module::Generic::RegexpCapture package
{
package
;
use vars qw( $VERSION $DEBUG $KEY_OBJECT );
use Clone ();
use Data::Dumper;
use JSON;
use Module::Generic::TieHash;
use Regexp::Common;
use Wanted;
use overload (
already an hash, so no need to do anything.
# To convert to a regular hash as needed by JSON, the method TO_JSON can be used.
sub as_hash
{
my $self = shift( @_ );
if( @_ )
{
my $o
{ %$self };
return( $ref );
}
}
return( $self );
}
sub as_json { return( shift->json(@_)->scalar ); }
sub as_string { return( shift->dump ); }
sub chomp
{
my $self =
h =
{
name => 'John Doe',
active => $bool,
};
# Converting to json
print( JSON->new->encode( $hash ), "\n" );
# Would result in
{
name: "Jogn Doe",
ed interchangeably as perl boolean, i.e. 1 or 0, or as L<JSON> bool, i.e. C<true> or C<false>
The code is taken and adapted from part of L<JSON> module.
=head1 METHODS
=head2 as_array
Returns an L
n added into L<JSON>
=head2 false
This returns a perl false value i.e. 0 or C<false> when added into L<JSON>
=head2 TO_JSON
This is called by L<JSON> to transform an object into json data format.
STORABLE_freeze
=for Pod::Coverage STORABLE_thaw
=for Pod::Coverage THAW
=for Pod::Coverage TO_JSON
Serialisation by L<CBOR|CBOR::XS>, L<Sereal> and L<Storable::Improved> (or the legacy L<Storable
ORE::stat( $file )];
return( $self );
}
# This class does not convert to an HASH, but the TO_JSON method will convert to a string
sub as_hash { return( $_[0] ); }
sub atime
{
my $self = shif
se
{
$new = CORE::bless( $hash => $class );
}
CORE::return( $new );
}
sub TO_JSON { CORE::return( CORE::shift->filepath ); }
sub _datetime
{
my $self = shift( @_ );
my $t
STORABLE_freeze
=for Pod::Coverage STORABLE_thaw
=for Pod::Coverage THAW
=for Pod::Coverage TO_JSON
Serialisation by L<CBOR|CBOR::XS>, L<Sereal> and L<Storable::Improved> (or the legacy L<Storable
STORABLE_freeze
=for Pod::Coverage STORABLE_thaw
=for Pod::Coverage THAW
=for Pod::Coverage TO_JSON
Serialisation by L<CBOR|CBOR::XS>, L<Sereal> and L<Storable::Improved> (or the legacy L<Storable
STORABLE_freeze
=for Pod::Coverage STORABLE_thaw
=for Pod::Coverage THAW
=for Pod::Coverage TO_JSON
Serialisation by L<CBOR|CBOR::XS>, L<Sereal> and L<Storable::Improved> (or the legacy L<Storable
th, os => $os, debug => $self->debug ) );
}
# This class does not convert to an HASH, but the TO_JSON method will convert to a string
sub as_hash { return( $_[0] ); }
sub atime { return( shift->finf
ray( $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_err
{filter_json_object} ) &&
ref( $opts->{filter_json_object} ) eq 'CODE' )
{
$j->filter_json_object( $opts->{filter_json_object} );
}
if( exists( $opts->{filter_json_single_
tor used in formatting the number.
=head2 TO_JSON
Special method called by L<JSON> to transform this object into a string suitable to be added in a json data.
=head2 unformat
Provided with a strin
::Util ();
# This is disruptive for everybody. Bad idea.
# use JSON 4.03 qw( -convert_blessed_universally );
use JSON 4.03;
use Module::Generic::Global ':const';
use Storable::Impr
# I leave the feature of using it as a choice to the user, but defaults to JSON
$self->{_packing_method} = 'json';
$self->SUPER::init( @_ ) || return( $self->pass_error );
$self->{add
{
$self->{id} = shift( @_ );
}
return( $self->{id} );
}
sub json { return( shift->_packing_method( 'json' ) ); }
sub key
{
my $self = shift( @_ );
if( @_ )
{
my
eturn( $self );
}
else
{
CORE::return( $class->new( $ref ) );
}
}
sub TO_JSON { CORE::return( [ @{$_[0]} ] ); }
# NOTE: Module::Generic::Array::Tie class
{
package
ery useful because they can be used in perl as o or 1 to indicate false or true, but when used in json, they are automatically converted to C<false> or C<true>
=head2 as_number
Returns the current s
STORABLE_freeze
=for Pod::Coverage STORABLE_thaw
=for Pod::Coverage THAW
=for Pod::Coverage TO_JSON
Serialisation by L<CBOR|CBOR::XS>, L<Sereal> and L<Storable::Improved> (or the legacy L<Storable
shift->_func( 'asin', { posix => 1 } ) ); }
# This class does not convert to an HASH, but the TO_JSON method will convert to a string
sub as_hash { return( $_[0] ); }
sub atan { return( shift->_func
se
{
$new = CORE::bless( $hash => $class );
}
CORE::return( $new );
}
sub TO_JSON { return( shift->as_string ); }
# NOTE: package Module::Generic::NumberSpecial
package Module::G
STORABLE_freeze
=for Pod::Coverage STORABLE_thaw
=for Pod::Coverage THAW
=for Pod::Coverage TO_JSON
Serialisation by L<CBOR|CBOR::XS>, L<Sereal> and L<Storable::Improved> (or the legacy L<Storable
STORABLE_freeze
=for Pod::Coverage STORABLE_thaw
=for Pod::Coverage THAW
=for Pod::Coverage TO_JSON
Serialisation by L<CBOR|CBOR::XS>, L<Sereal> and L<Storable::Improved> (or the legacy L<Storable
ata::UUID;
# This is disruptive for everybody. Bad idea.
# use JSON 4.03 qw( -convert_blessed_universally );
use JSON 4.03;
use Module::Generic::File qw( file sys_tmpdir );
our $HA
$self->{serial} = '';
$self->{size} = 0;
$self->{_packing_method} = 'json';
$self->{_init_strict_use_sub} = 1;
$self->SUPER::init( @_ ) || return( $self->pass_erro
stMmap' ) ); }
sub id { return( shift->_set_get_scalar( 'id', @_ ) ); }
sub json { return( shift->_packing_method( 'json' ) ); }
# sub key { return( shift->_set_get_scalar( 'key', @_ ) ); }
sub key
post_processing
=for Pod::Coverage STORABLE_thaw
=for Pod::Coverage THAW
=for Pod::Coverage TO_JSON
Serialisation by L<CBOR|CBOR::XS>, L<Sereal> and L<Storable::Improved> (or the legacy L<Storable