| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | # file::json Brik | ||
| 5 | # | ||
| 6 | package Metabrik::File::Json; | ||
| 7 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | ▶ | use base qw(Metabrik::File::Text); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | sub brik_properties { | ||
| 13 | return { | ||
| 14 | revision => '$Revision$', | ||
| 15 | tags => [ qw(unstable) ], | ||
| 16 | author => 'GomoR <GomoR[at]metabrik.org>', | ||
| 17 | license => 'http://opensource.org/licenses/BSD-3-Clause', | ||
| 18 | attributes => { | ||
| 19 | input => [ qw(file) ], | ||
| 20 | output => [ qw(file) ], | ||
| 21 | encoding => [ qw(utf8|ascii) ], | ||
| 22 | overwrite => [ qw(0|1) ], | ||
| 23 | use_utf8 => [ qw(0|1) ], | ||
| 24 | _sj => [ qw(INTERNAL) ], | ||
| 25 | }, | ||
| 26 | attributes_default => { | ||
| 27 | use_utf8 => 1, | ||
| 28 | overwrite => 1, | ||
| 29 | }, | ||
| 30 | commands => { | ||
| 31 | read => [ qw(input_file|OPTIONAL) ], | ||
| 32 | read_next => [ qw(input_file|OPTIONAL) ], | ||
| 33 | write => [ qw($json_hash output_file|OPTIONAL) ], | ||
| 34 | is_valid => [ qw(input_file|OPTIONAL) ], | ||
| 35 | }, | ||
| 36 | require_modules => { | ||
| 37 | 'Metabrik::File::Write' => [ ], | ||
| 38 | 'Metabrik::String::Json' => [ ], | ||
| 39 | }, | ||
| 40 | }; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub brik_use_properties { | ||
| 44 | my $self = shift; | ||
| 45 | |||
| 46 | return { | ||
| 47 | attributes_default => { | ||
| 48 | encoding => defined($self->global) && $self->global->encoding || 'utf8', | ||
| 49 | }, | ||
| 50 | }; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub brik_init { | ||
| 54 | my $self = shift; | ||
| 55 | |||
| 56 | my $sj = Metabrik::String::Json->new_from_brik_init($self) or return; | ||
| 57 | $self->_sj($sj); | ||
| 58 | |||
| 59 | return $self->SUPER::brik_init; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub read { | ||
| 63 | my $self = shift; | ||
| 64 | my ($input) = @_; | ||
| 65 | |||
| 66 | $input ||= $self->input; | ||
| 67 | $self->brik_help_run_undef_arg('read', $input) or return; | ||
| 68 | |||
| 69 | my $sj = $self->_sj; | ||
| 70 | $sj->use_utf8($self->use_utf8); | ||
| 71 | |||
| 72 | my $data = $self->SUPER::read($input) or return; | ||
| 73 | |||
| 74 | return $sj->decode($data); | ||
| 75 | } | ||
| 76 | |||
| 77 | sub read_next { | ||
| 78 | my $self = shift; | ||
| 79 | my ($input) = @_; | ||
| 80 | |||
| 81 | $input ||= $self->input; | ||
| 82 | $self->brik_help_run_undef_arg('read_next', $input) or return; | ||
| 83 | |||
| 84 | my $sj = $self->_sj; | ||
| 85 | $sj->use_utf8($self->use_utf8); | ||
| 86 | |||
| 87 | my $data = $self->SUPER::read_line($input) or return; | ||
| 88 | |||
| 89 | return $sj->decode($data); | ||
| 90 | } | ||
| 91 | |||
| 92 | sub write { | ||
| 93 | my $self = shift; | ||
| 94 | my ($json_hash, $output) = @_; | ||
| 95 | |||
| 96 | $output ||= $self->output; | ||
| 97 | $self->brik_help_run_undef_arg('write', $json_hash) or return; | ||
| 98 | $self->brik_help_run_invalid_arg('write', $json_hash, 'ARRAY', 'HASH') | ||
| 99 | or return; | ||
| 100 | $self->brik_help_run_undef_arg('write', $output) or return; | ||
| 101 | |||
| 102 | my $sj = $self->_sj; | ||
| 103 | $sj->use_utf8($self->use_utf8); | ||
| 104 | |||
| 105 | # Always make it an ARRAY | ||
| 106 | $json_hash = ref($json_hash) eq 'ARRAY' ? $json_hash : [ $json_hash ]; | ||
| 107 | |||
| 108 | my @data = (); | ||
| 109 | for my $this (@$json_hash) { | ||
| 110 | my $data = $sj->encode($this) or next; | ||
| 111 | push @data, $data; | ||
| 112 | } | ||
| 113 | |||
| 114 | $self->SUPER::write(\@data, $output) or return; | ||
| 115 | |||
| 116 | return $output; | ||
| 117 | } | ||
| 118 | |||
| 119 | sub is_valid { | ||
| 120 | my $self = shift; | ||
| 121 | my ($input) = @_; | ||
| 122 | |||
| 123 | $input ||= $self->input; | ||
| 124 | $self->brik_help_run_undef_arg('is_valid', $input) or return; | ||
| 125 | |||
| 126 | my $data = $self->SUPER::read($input) or return; | ||
| 127 | |||
| 128 | my $sj = $self->_sj; | ||
| 129 | $sj->use_utf8($self->use_utf8); | ||
| 130 | |||
| 131 | return $sj->is_valid($data); | ||
| 132 | } | ||
| 133 | |||
| 134 | 1; | ||
| 135 | |||
| 136 | __END__ | ||