| 1 | package Template::Flute::Iterator::JSON; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use JSON; | ||
| 7 | |||
| 8 | use base 'Template::Flute::Iterator'; | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | Template::Flute::Iterator::JSON - Iterator class for JSON strings and files | ||
| 13 | |||
| 14 | =head1 SYNOPSIS | ||
| 15 | |||
| 16 | $json = q{[ | ||
| 17 | {"sku": "orange", "image": "orange.jpg"}, | ||
| 18 | {"sku": "pomelo", "image": "pomelo.jpg"} | ||
| 19 | ]}; | ||
| 20 | |||
| 21 | $json_iter = Template::Flute::Iterator::JSON->new($json); | ||
| 22 | |||
| 23 | $json_iter->next(); | ||
| 24 | |||
| 25 | $json_iter_file = Template::Flute::Iterator::JSON->new(file => 'fruits.json'); | ||
| 26 | |||
| 27 | =head1 DESCRIPTION | ||
| 28 | |||
| 29 | Template::Flute::Iterator::JSON is a subclass of L<Template::Flute::Iterator>. | ||
| 30 | |||
| 31 | =head1 CONSTRUCTOR | ||
| 32 | |||
| 33 | =head2 new | ||
| 34 | |||
| 35 | Creates an Template::Flute::Iterator::JSON object from a JSON string. | ||
| 36 | |||
| 37 | The JSON string can be either passed as such or as scalar reference. | ||
| 38 | |||
| 39 | =cut | ||
| 40 | |||
| 41 | sub new { | ||
| 42 | my ($class, @args) = @_; | ||
| 43 | my ($json, $json_struct, $self, $key, $value); | ||
| 44 | |||
| 45 | $self = {}; | ||
| 46 | |||
| 47 | bless ($self, $class); | ||
| 48 | |||
| 49 | if (@args == 1) { | ||
| 50 | # single parameter => JSON is passed as string or scalar reference | ||
| 51 | if (ref($args[0]) eq 'SCALAR') { | ||
| 52 | $json = ${$args[0]}; | ||
| 53 | } | ||
| 54 | else { | ||
| 55 | $json = $args[0]; | ||
| 56 | } | ||
| 57 | |||
| 58 | $json_struct = from_json($json); | ||
| 59 | $self->_seed_iterator($json_struct); | ||
| 60 | |||
| 61 | return $self; | ||
| 62 | } | ||
| 63 | |||
| 64 | while (@args) { | ||
| 65 | $key = shift(@args); | ||
| 66 | $value = shift(@args); | ||
| 67 | |||
| 68 | $self->{$key} = $value; | ||
| 69 | } | ||
| 70 | |||
| 71 | if ($self->{file}) { | ||
| 72 | $json_struct = $self->_parse_json_from_file($self->{file}); | ||
| 73 | $self->_seed_iterator($json_struct); | ||
| 74 | } | ||
| 75 | else { | ||
| 76 | die "Missing JSON file."; | ||
| 77 | } | ||
| 78 | |||
| 79 | return $self; | ||
| 80 | } | ||
| 81 | |||
| 82 | sub _seed_iterator { | ||
| 83 | my ($self, $json_struct) = @_; | ||
| 84 | |||
| 85 | if (exists $self->{selector}) { | ||
| 86 | if (ref($self->{selector}) eq 'HASH') { | ||
| 87 | my (@k, $key, $value); | ||
| 88 | |||
| 89 | # loop through top level elements and locate selector | ||
| 90 | if ((@k = keys %{$self->{selector}})) { | ||
| 91 | $key = $k[0]; | ||
| 92 | $value = $self->{selector}->{$key}; | ||
| 93 | |||
| 94 | for my $record (@$json_struct) { | ||
| 95 | if (exists $record->{$key} | ||
| 96 | && $record->{$key} eq $value) { | ||
| 97 | $self->seed($record->{$self->{children}}); | ||
| 98 | return; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | return; | ||
| 104 | } | ||
| 105 | elsif ($self->{selector} eq '*') { | ||
| 106 | # find all elements | ||
| 107 | $self->seed($self->_tree($json_struct, $self->{children}, $self->{sort})); | ||
| 108 | |||
| 109 | if ($self->{sort}) { | ||
| 110 | $self->sort($self->{sort}, $self->{unique}); | ||
| 111 | } | ||
| 112 | |||
| 113 | return; | ||
| 114 | } | ||
| 115 | |||
| 116 | # no matches for selector, seed iterator with empty list | ||
| 117 | $self->seed(); | ||
| 118 | return; | ||
| 119 | } | ||
| 120 | |||
| 121 | $self->seed($json_struct); | ||
| 122 | } | ||
| 123 | |||
| 124 | sub _tree { | ||
| 125 | my ($self, $json_struct, $children, $sort) = @_; | ||
| 126 | my (@leaves); | ||
| 127 | |||
| 128 | for my $record (@$json_struct) { | ||
| 129 | if (exists $record->{$children}) { | ||
| 130 | for my $child (@{$record->{$children}}) { | ||
| 131 | push (@leaves, $child); | ||
| 132 | } | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | return \@leaves; | ||
| 137 | } | ||
| 138 | |||
| 139 | sub _parse_json_from_file { | ||
| 140 | my ($self, $file) = @_; | ||
| 141 | my ($json_fh, $json_struct, $json_txt); | ||
| 142 | |||
| 143 | # read from JSON file | ||
| 144 | unless (open $json_fh, '<', $file) { | ||
| 145 | die "$0: failed to open JSON file $file: $!\n"; | ||
| 146 | } | ||
| 147 | |||
| 148 | while (<$json_fh>) { | ||
| 149 | $json_txt .= $_; | ||
| 150 | } | ||
| 151 | |||
| 152 | close $json_fh; | ||
| 153 | |||
| 154 | # parse JSON | ||
| 155 | $json_struct = from_json($json_txt); | ||
| 156 | |||
| 157 | return $json_struct; | ||
| 158 | } | ||
| 159 | |||
| 160 | =head1 AUTHOR | ||
| 161 | |||
| 162 | Stefan Hornburg (Racke), <racke@linuxia.de> | ||
| 163 | |||
| 164 | =head1 LICENSE AND COPYRIGHT | ||
| 165 | |||
| 166 | Copyright 2011-2016 Stefan Hornburg (Racke) <racke@linuxia.de>. | ||
| 167 | |||
| 168 | This program is free software; you can redistribute it and/or modify it | ||
| 169 | under the terms of either: the GNU General Public License as published | ||
| 170 | by the Free Software Foundation; or the Artistic License. | ||
| 171 | |||
| 172 | See http://dev.perl.org/licenses/ for more information. | ||
| 173 | |||
| 174 | =cut | ||
| 175 | |||
| 176 | 1; |