0
|
1 package lib::json ;
|
|
2
|
|
3 use strict;
|
|
4 use warnings ;
|
|
5 use Exporter ;
|
|
6 use Carp ;
|
|
7
|
|
8 use Data::Dumper ;
|
|
9
|
|
10 ## dedicate lib
|
|
11 use JSON qw( );
|
|
12
|
|
13 use vars qw($VERSION @ISA @EXPORT %EXPORT_TAGS);
|
|
14
|
|
15 our $VERSION = "1.0";
|
|
16 our @ISA = qw(Exporter);
|
|
17 our @EXPORT = qw(decode_json_file);
|
|
18 our %EXPORT_TAGS = ( ALL => [qw(decode_json_file)] );
|
|
19
|
|
20 =head1 NAME
|
|
21
|
|
22 My::Module - An example module
|
|
23
|
|
24 =head1 SYNOPSIS
|
|
25
|
|
26 use My::Module;
|
|
27 my $object = My::Module->new();
|
|
28 print $object->as_string;
|
|
29
|
|
30 =head1 DESCRIPTION
|
|
31
|
|
32 This module does not really exist, it
|
|
33 was made for the sole purpose of
|
|
34 demonstrating how POD works.
|
|
35
|
|
36 =head1 METHODS
|
|
37
|
|
38 Methods are :
|
|
39
|
|
40 =head2 METHOD new
|
|
41
|
|
42 ## Description : new
|
|
43 ## Input : $self
|
|
44 ## Ouput : bless $self ;
|
|
45 ## Usage : new() ;
|
|
46
|
|
47 =cut
|
|
48
|
|
49 sub new {
|
|
50 ## Variables
|
|
51 my $self={};
|
|
52 bless($self) ;
|
|
53 return $self ;
|
|
54 }
|
|
55 ### END of SUB
|
|
56
|
|
57 =head2 METHOD decode_json_file
|
|
58
|
|
59 ## Description : decode a json file and return a json perl object
|
|
60 ## Input : $ref_file
|
|
61 ## Output : $ojson
|
|
62 ## Usage : my ( $ojson ) = decode_json_file( $file ) ;
|
|
63
|
|
64 =cut
|
|
65 ## START of SUB
|
|
66 sub decode_json_file {
|
|
67 ## Retrieve Values
|
|
68 my $self = shift ;
|
|
69 my ( $ref_file ) = @_ ;
|
|
70
|
|
71 my $ojson = undef ;
|
|
72
|
|
73 if ( ( defined $ref_file ) and ( -e $$ref_file ) ) {
|
|
74 my $filename = $$ref_file ;
|
|
75 my $json_text = do {
|
|
76 open( my $json_fh, "<:encoding(UTF-8)", $filename) or die("Can't open \$filename\": $!\n");
|
|
77 local $/;
|
|
78 <$json_fh>
|
|
79 };
|
|
80
|
|
81 my $json = JSON->new;
|
|
82 $ojson = $json->decode($json_text);
|
|
83 }
|
|
84 else {
|
|
85 croak "Can't decode any file : it doesn't exist or defined\n" ;
|
|
86 }
|
|
87 return(\$ojson) ;
|
|
88 }
|
|
89 ## END of SUB
|
|
90
|
|
91 =head2 METHOD decode_json_stdout
|
|
92
|
|
93 ## Description : decode a perl unicode string and return a json perl object
|
|
94 ## Input : $stdout
|
|
95 ## Output : $perl_scalar
|
|
96 ## Usage : my ( $perl_scalar ) = decode_json_stdout( $stdout ) ;
|
|
97
|
|
98 =cut
|
|
99 ## START of SUB
|
|
100 sub decode_json_stdout {
|
|
101 ## Retrieve Values
|
|
102 my $self = shift ;
|
|
103 my ( $stdout ) = @_ ;
|
|
104
|
|
105 my $perl_scalar = undef ;
|
|
106 if ( defined $stdout ) {
|
|
107 my $string = $$stdout ;
|
|
108 # $string =~ s/\\/\\\\/g ; ## bug patched : jsons are clean now
|
|
109
|
|
110 $perl_scalar = JSON->new->decode($string) ;
|
|
111 }
|
|
112 else {
|
|
113 croak "Can't decode any string : it doesn't defined\n" ;
|
|
114 }
|
|
115 return(\$perl_scalar) ;
|
|
116 }
|
|
117 ## END of SUB
|
|
118
|
|
119 1 ;
|
|
120
|
|
121
|
|
122 __END__
|
|
123
|
|
124 =head1 SUPPORT
|
|
125
|
|
126 You can find documentation for this module with the perldoc command.
|
|
127
|
|
128 perldoc XXX.pm
|
|
129
|
|
130 =head1 Exports
|
|
131
|
|
132 =over 4
|
|
133
|
|
134 =item :ALL is ...
|
|
135
|
|
136 =back
|
|
137
|
|
138 =head1 AUTHOR
|
|
139
|
|
140 Franck Giacomoni E<lt>franck.giacomoni@clermont.inra.frE<gt>
|
|
141
|
|
142 =head1 LICENSE
|
|
143
|
|
144 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
|
|
145
|
|
146 =head1 VERSION
|
|
147
|
|
148 version 1 : xx / xx / 201x
|
|
149
|
|
150 version 2 : ??
|
|
151
|
|
152 =cut |