File Coverage

blib/lib/Dallycot/Value/JSON.pm
Criterion Covered Total %
statement 18 67 26.8
branch n/a
condition 0 3 0.0
subroutine 6 12 50.0
pod 0 4 0.0
total 24 86 27.9


line stmt bran cond sub pod time code
1             package Dallycot::Value::JSON;
2             our $AUTHORITY = 'cpan:JSMITH';
3              
4             # ABSTRACT: Manages a memory-based JSON object
5              
6 23     23   16226 use strict;
  23         40  
  23         740  
7 23     23   88 use warnings;
  23         35  
  23         584  
8              
9 23     23   87 use utf8;
  23         30  
  23         104  
10 23     23   422 use parent 'Dallycot::Value::Any';
  23         32  
  23         159  
11              
12 23     23   1190 use experimental qw(switch);
  23         34  
  23         162  
13              
14 23     23   3065 use Promises qw(deferred);
  23         38  
  23         113  
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 0     0 0   my( $class, $hash ) = @_;
27              
28 0   0       $class = ref $class || $class;
29              
30 0           return bless [ $hash ] => $class;
31             }
32              
33             sub calculate_length {
34 0     0 0   my ( $self, $engine ) = @_;
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     0 0   my( $self ) = @_;
45              
46 0           my @values;
47              
48 0           while(my($k, $v) = each %{$self -> [0]}) {
  0            
49 0           push @values, "\"$k\": " . $v -> as_text;
50             }
51              
52 0           return '{' . join(",", @values) . '}';
53             }
54              
55             sub _object_size {
56 0     0     my($self, $obj) = @_;
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 $_ } @$_;
  0            
  0            
67             }
68             }
69             }
70 0           return $count;
71             }
72              
73             sub fetch_property {
74 0     0 0   my ( $self, $engine, $prop ) = @_;
75              
76 0           my $d = deferred;
77              
78 0           my $value = $self->[0]->{$prop};
79              
80 0           given(ref $value) {
81 0           when('HASH') {
82 0           $d->resolve(bless [ $value ] => __PACKAGE__);
83             }
84 0           when('ARRAY') {
85 0           $d->resolve($self->_convert_to_vector($value));
86             }
87 0           when(undef) {
88 0           $d->resolve(Dallycot::Value::Undefined->new);
89             }
90 0           default {
91 0           $d->resolve($value);
92             }
93             }
94              
95 0           return $d->promise;
96             }
97              
98             sub _convert_to_vector {
99 0     0     my($self, $values) = @_;
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             1;