<commit_before>package PGObject::Type::JSON;

use 5.006;
use strict;
use warnings;
use PGObject;
use JSON;

=head1 NAME

PGObject::Type::JSON - JSON wrappers for PGObject

=head1 VERSION

Version 0.01

=cut

our $VERSION = '0.01';


=head1 SYNOPSIS

   PGOBject::Type::JSON->register();

Columns of type json will be converted into hashrefs

   my $obj =  PGOBject::Type::JSON->new($hashref);

$obj will now serialize to the database as json.

=head1 DESCRIPTION

This module allows json types or others (specified by custom register) types to
be converted from JSON into objects according to their values.

This module assumes that encoding will be in UTF8 across the board and is not
safe to use with other database encodings.

=head1 SUBROUTINES/METHODS

=head2 register()

=cut

=head2 new($ref)

Stores this for JSON.

=cut

sub new {
    my ($class, $ref);
    $ref = \$ref unless ref $ref;
    bless $ref, $class;
} 

=head2 from_db



=head2 to_db

=head2 is_null

=head1 AUTHOR<commit_msg>this is going to be fun to test :-P  to_db and from_db I think are done.  Need testing.  Still need register()<commit_after>package PGObject::Type::JSON;

use 5.006;
use strict;
use warnings;
use PGObject;
use JSON;

=head1 NAME

PGObject::Type::JSON - JSON wrappers for PGObject

=head1 VERSION

Version 0.01

=cut

our $VERSION = '0.01';


=head1 SYNOPSIS

   PGOBject::Type::JSON->register();

Columns of type json will be converted into hashrefs

   my $obj =  PGOBject::Type::JSON->new($hashref);

$obj will now serialize to the database as json.

=head1 DESCRIPTION

This module allows json types or others (specified by custom register) types to
be converted from JSON into objects according to their values.

This module assumes that encoding will be in UTF8 across the board and is not
safe to use with other database encodings.

=head1 SUBROUTINES/METHODS

=head2 register()

=cut

=head2 new($ref)

Stores this for JSON.

=cut

sub new {
    my ($class, $ref);
    $ref = \$ref unless ref $ref;
    bless $ref, $class;
} 

=head2 from_db

serializes from the db. Note that database nulls are preserved distinct from
json null's.

=cut

sub from_db {
    my ($class, $var) = @_;
    return $class->new($var) unless ref $var;
    return $class->new(JSON->new->allow_nonref->decode($self));
}


=head2 to_db

returns undef if is_null.  Otherwise returns the value encoded as JSON

=cut

sub to_db {
    my $self = shift @_;
    return undef if $self->is_null;
    return JSON->new->allow_nonref->encode($self);
}

=head2 is_null

Returns true if is a reference to undef.

=cut

sub is_null {
    my $self = shift @_;
    my $test = '';
    return 1 if ref $self eq ref \$test and !defined $$self;
    return 0;
}

=head1 AUTHOR
{'fraction_non_alphanumeric': 0.09879253567508232, 'fraction_numerical': 0.016831320892791803, 'mean_word_length': 3.7383015597920277, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2234104', 'n_tokens_mistral': 960, 'n_tokens_neox': 914, 'n_words': 360}