| 1 | ##-*- Mode: CPerl -*- | ||||||||||||||||||||||||||||
| 2 | ## | ||||||||||||||||||||||||||||
| 3 | ## File: Tie/File/Indexed/JSON.pm | ||||||||||||||||||||||||||||
| 4 | ## Author: Bryan Jurish <moocow@cpan.org> | ||||||||||||||||||||||||||||
| 5 | ## Description: tied array access to indexed data files: JSON-encoded data structures | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | package Tie::File::Indexed::JSON; | ||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Tie::File::Indexed; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | ##====================================================================== | ||||||||||||||||||||||||||||
| 13 | ## Globals | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 2 | our @ISA = qw(Tie::File::Indexed); | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | ##====================================================================== | ||||||||||||||||||||||||||||
| 18 | ## Constructors etc. | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | ## $tfi = CLASS->new(%opts) | ||||||||||||||||||||||||||||
| 21 | ## $tfi = CLASS->new($file,%opts) | ||||||||||||||||||||||||||||
| 22 | ## + new %opts, object structure: | ||||||||||||||||||||||||||||
| 23 | ## ( | ||||||||||||||||||||||||||||
| 24 | ## json => $json, ##-- JSON object or HASH-ref of options | ||||||||||||||||||||||||||||
| 25 | ## ) | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | ## \%defaults = CLASS_OR_OBJECT->defaults() | ||||||||||||||||||||||||||||
| 28 | ## + default attributes for constructor | ||||||||||||||||||||||||||||
| 29 | 1 | ▶ | sub defaults { | ||||||||||||||||||||||||||
|
Subroutine
defaults: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 30 | return ( | ||||||||||||||||||||||||||||
| 31 | 1 | $_[0]->SUPER::defaults, | |||||||||||||||||||||||||||
| 32 | json => {utf8=>1, relaxed=>1, allow_nonref=>1, allow_unknown=>1, allow_blessed=>1, convert_blessed=>1, pretty=>0, canonical=>0}, | ||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | ##-------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 37 | ## Utilities: JSON | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | ## $json = $aj->json() | ||||||||||||||||||||||||||||
| 40 | ## + returns json codec | ||||||||||||||||||||||||||||
| 41 | 1 | ▶ | sub json { | ||||||||||||||||||||||||||
|
Subroutine
json: called CC=4 SCAR=14.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 42 | 1 | ▶ | return $_[0]{json} if (UNIVERSAL::isa($_[0]{json}, 'JSON')); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 1 | my $json = JSON->new; | |||||||||||||||||||||||||||
| 44 | 1 | ▶ | foreach (grep {$json->can($_)} keys %{$_[0]{json}//{}}) { | ||||||||||||||||||||||||||
|
Condition50%1/2
$_[0]{"json"} // {}
MC/DC0%0/1
$_[0]{"json"} // {}
$_[0]{"json"}
Truth table50%1/2
A
A= $_[0]{"json"}
| |||||||||||||||||||||||||||||
| 45 | 8 | $json->can($_)->($json,$_[0]{json}{$_}); | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | 1 | return $_[0]{json} = $json; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | ##====================================================================== | ||||||||||||||||||||||||||||
| 51 | ## Object API: overrides | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | ##-------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 54 | ## Object API: overrides: open/close | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | ## $tfi_or_undef = $tfi->open($file,$mode) | ||||||||||||||||||||||||||||
| 57 | ## $tfi_or_undef = $tfi->open($file) | ||||||||||||||||||||||||||||
| 58 | ## $tfi_or_undef = $tfi->open() | ||||||||||||||||||||||||||||
| 59 | ## + opens file(s) | ||||||||||||||||||||||||||||
| 60 | 1 | ▶ | sub open { | ||||||||||||||||||||||||||
|
Subroutine
open: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 61 | 1 | my $tfi = shift; | |||||||||||||||||||||||||||
| 62 | 1 | ▶ | return undef if (!$tfi->SUPER::open(@_)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | ##-- ensure 'json' object is defined | ||||||||||||||||||||||||||||
| 65 | 1 | $tfi->json(); | |||||||||||||||||||||||||||
| 66 | 1 | return $tfi; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | ##====================================================================== | ||||||||||||||||||||||||||||
| 70 | ## Subclass API: Data I/O | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | ## $bool = $tfi->writeData($utf8_string) | ||||||||||||||||||||||||||||
| 73 | ## + override transparently encodes $data using the JSON module | ||||||||||||||||||||||||||||
| 74 | 7 | ▶ | sub writeData { | ||||||||||||||||||||||||||
|
Subroutine
writeData: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 75 | 7 | ▶ | return 1 if (!defined($_[1])); ##-- don't waste space on undef | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 6 | return $_[0]{datfh}->print( $_[0]{json}->encode($_[1]) ); | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | ## $data_or_undef = $tfi->readData($length) | ||||||||||||||||||||||||||||
| 80 | ## + override decodes stored data using the JSON module | ||||||||||||||||||||||||||||
| 81 | 7 | ▶ | sub readData { | ||||||||||||||||||||||||||
|
Subroutine
readData: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||
| 82 | 7 | ▶ | return undef if ($_[1]==0 || !defined(my $buf=$_[0]->SUPER::readData($_[1]))); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$_[1] == 0 or not defined(my $buf = $_[0]->SUPER::readData($_[1]))
MC/DC50%1/2
$_[1] == 0 or not defined(my $buf = $_[0]->SUPER::readData($_[1]))
$_[1] == 0
not defined(my $buf = $_[0]->SUPER::readData($_[1]))
Truth table66%2/3
A or not(B)
A= $_[1] == 0B= not defined(my $buf = $_[0]->SUPER::readData($_[1]))
| |||||||||||||||||||||||||||||
| 83 | 5 | return $_[0]{json}->decode($buf); | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 2 | 1; ##-- be happpy | |||||||||||||||||||||||||||