| 1 | package Dallycot::Value::JSON; | ||||||||||||||||||||
| 2 | 30 | our $AUTHORITY = 'cpan:JSMITH'; | |||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | # ABSTRACT: Manages a memory-based JSON object | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 30 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 30 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 30 | ▶ | use utf8; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 30 | ▶ | use parent 'Dallycot::Value::Any'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 30 | ▶ | use experimental qw(switch); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 30 | ▶ | use Promises qw(deferred); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | =head1 DESCRIPTION | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | A JSON value represents a collection of properties and values. | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | Long term, a JSON value will be a JSON-LD document that can be interpreted | ||||||||||||||||||||
| 21 | as a set of triples. These triples will be stored as a triple-store. | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | =cut | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | sub new { | ||||||||||||||||||||
| 26 | 2 | ▶ | my( $class, $hash ) = @_; | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 2 | ▶ | $class = ref $class || $class; | ||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 2 | return bless [ $hash ] => $class; | |||||||||||||||||||
| 31 | } | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | sub calculate_length { | ||||||||||||||||||||
| 34 | 0 | ▶ | my ( $self, $engine ) = @_; | ||||||||||||||||||
|
Subroutine
calculate_length: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 0 | my $d = deferred; | |||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 0 | $d->resolve( $engine->make_numeric( $self->_object_size($self->[0]) ) ); | |||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 0 | return $d->promise; | |||||||||||||||||||
| 41 | } | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | sub as_text { | ||||||||||||||||||||
| 44 | 0 | ▶ | my( $self ) = @_; | ||||||||||||||||||
|
Subroutine
as_text: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 0 | my @values; | |||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 0 | ▶ | while(my($k, $v) = each %{$self -> [0]}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 49 | 0 | push @values, "\"$k\": " . $v -> as_text; | |||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 0 | return '{' . join(",", @values) . '}'; | |||||||||||||||||||
| 53 | } | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | sub _object_size { | ||||||||||||||||||||
| 56 | 0 | ▶ | my($self, $obj) = @_; | ||||||||||||||||||
|
Subroutine
_object_size: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 0 | my $count = keys %$obj; | |||||||||||||||||||
| 59 | 0 | foreach my $v (values %$obj) { | |||||||||||||||||||
| 60 | 0 | given(ref $v) { | |||||||||||||||||||
| 61 | 0 | when('HASH') { | |||||||||||||||||||
| 62 | 0 | $count += $self->_object_size($v); | |||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | 0 | when('ARRAY') { | |||||||||||||||||||
| 65 | 0 | $count += @$v; | |||||||||||||||||||
| 66 | 0 | $count += $_ for map { $self->_object_size($_) } grep { 'HASH' eq ref $_ } @$_; | |||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | } | ||||||||||||||||||||
| 70 | 0 | return $count; | |||||||||||||||||||
| 71 | } | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | sub fetch_property { | ||||||||||||||||||||
| 74 | 1 | ▶ | my ( $self, $engine, $prop ) = @_; | ||||||||||||||||||
|
Subroutine
fetch_property: called CC=1 SCAR=0.1
Pod
undocumented
| |||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 1 | my $d = deferred; | |||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 1 | my $value = $self->[0]->{$prop}; | |||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 1 | given(ref $value) { | |||||||||||||||||||
| 81 | 1 | when('HASH') { | |||||||||||||||||||
| 82 | 0 | $d->resolve(bless [ $value ] => __PACKAGE__); | |||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | 1 | when('ARRAY') { | |||||||||||||||||||
| 85 | 0 | $d->resolve($self->_convert_to_vector($value)); | |||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | 1 | when(undef) { | |||||||||||||||||||
| 88 | 0 | $d->resolve(Dallycot::Value::Undefined->new); | |||||||||||||||||||
| 89 | } | ||||||||||||||||||||
| 90 | 1 | default { | |||||||||||||||||||
| 91 | 1 | $d->resolve($value); | |||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | } | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 1 | return $d->promise; | |||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | sub _convert_to_vector { | ||||||||||||||||||||
| 99 | 0 | ▶ | my($self, $values) = @_; | ||||||||||||||||||
|
Subroutine
_convert_to_vector: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 0 | my @converted; | |||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | 0 | for my $v (@$values) { | |||||||||||||||||||
| 104 | 0 | given(ref $v) { | |||||||||||||||||||
| 105 | 0 | when('HASH') { | |||||||||||||||||||
| 106 | 0 | push @converted, bless [ $v ] => __PACKAGE__; | |||||||||||||||||||
| 107 | } | ||||||||||||||||||||
| 108 | 0 | when('ARRAY') { | |||||||||||||||||||
| 109 | 0 | push @converted, $self->_convert_to_vector($v); | |||||||||||||||||||
| 110 | } | ||||||||||||||||||||
| 111 | 0 | default { | |||||||||||||||||||
| 112 | 0 | push @converted, $v; | |||||||||||||||||||
| 113 | } | ||||||||||||||||||||
| 114 | } | ||||||||||||||||||||
| 115 | } | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | 0 | return bless \@converted => 'Dallycot::Value::Vector'; | |||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | 30 | 1; | |||||||||||||||||||