| 1 | # use strict; | ||
| 2 | # use warnings; | ||
| 3 | # package Mojo::File::Role::JSON; | ||
| 4 | |||
| 5 | # # ABSTRACT: adds a json method to Mojo::File to store and retrieve JSON | ||
| 6 | |||
| 7 | # use Mojo::Base -role; | ||
| 8 | # use Mojo::Util qw/dumper/; | ||
| 9 | |||
| 10 | # use JSON::MaybeXS; | ||
| 11 | # use Hash::Merge; | ||
| 12 | |||
| 13 | # our $pretty = 1; | ||
| 14 | # our $policy = "die"; | ||
| 15 | |||
| 16 | # use Want; | ||
| 17 | |||
| 18 | # sub json { | ||
| 19 | # my $self = shift; | ||
| 20 | # if (@_) { | ||
| 21 | # unless (-f $self) { $self->dirname->make_path } | ||
| 22 | # my $json = JSON::MaybeXS->new->pretty($pretty)->utf8->encode(shift()); | ||
| 23 | # $self->spew($json); | ||
| 24 | # if (want('OBJECT')) { | ||
| 25 | # rreturn $self; | ||
| 26 | # } else { | ||
| 27 | # rreturn $self->json; | ||
| 28 | # } | ||
| 29 | # } | ||
| 30 | # return undef unless -f $self; | ||
| 31 | |||
| 32 | # # if ($self->policy eq "die") { | ||
| 33 | # # die sprintf qq(File "%s" not found\n) unless -f $self; | ||
| 34 | # # } | ||
| 35 | # # elsif ($self->policy eq "undef") { | ||
| 36 | # # return undef unless -f $self | ||
| 37 | # # } | ||
| 38 | # # elsif ($self->policy eq "auto") { | ||
| 39 | # # return {} unless -f $self | ||
| 40 | # # } | ||
| 41 | # # else { | ||
| 42 | # # die sprintf qq("%s" is not a valid policy\n), $policy; | ||
| 43 | # # } | ||
| 44 | # my $json = decode_json $self->slurp; | ||
| 45 | # return $json; | ||
| 46 | # } | ||
| 47 | |||
| 48 | # =head2 json | ||
| 49 | |||
| 50 | # my $file = path("foo.json"); | ||
| 51 | # $file->json({ foo => "bar" }); # stores a variable as json in the file | ||
| 52 | |||
| 53 | # my $foo = $file->json(); # retrieves the decoded variable | ||
| 54 | |||
| 55 | # Stores or retrieves vars into the file. | ||
| 56 | |||
| 57 | # Returns the stored value or - when dereferenced - the object upon storing. | ||
| 58 | |||
| 59 | # Returns the stored value upon retrieving. | ||
| 60 | |||
| 61 | # Returns undef if the file does not exist. | ||
| 62 | |||
| 63 | # =cut | ||
| 64 | |||
| 65 | # sub store { | ||
| 66 | # my $self = shift; | ||
| 67 | # die "Need something to store\n" unless @_; | ||
| 68 | # $self->json(shift()) | ||
| 69 | # } | ||
| 70 | |||
| 71 | # sub retrieve { | ||
| 72 | # my $self = shift; | ||
| 73 | # $self->json(); | ||
| 74 | # } | ||
| 75 | |||
| 76 | |||
| 77 | # sub pretty { | ||
| 78 | # my $self = shift; | ||
| 79 | # $pretty = shift if @_; | ||
| 80 | # return $self; | ||
| 81 | # } | ||
| 82 | |||
| 83 | # sub policy { | ||
| 84 | # my $self = shift; | ||
| 85 | # $policy = shift if @_; | ||
| 86 | # return $self; | ||
| 87 | # } | ||
| 88 | |||
| 89 | |||
| 90 | # =head2 pretty | ||
| 91 | |||
| 92 | # my $file = path("foo.json")->pretty(1)->json({ foo => "bar" }); # | ||
| 93 | |||
| 94 | # Sets the storage to be prettified JSON or not. | ||
| 95 | |||
| 96 | # =cut | ||
| 97 | |||
| 98 | # sub merge { | ||
| 99 | # my ($self, $json, $merger) = @_; | ||
| 100 | # $self->json(Hash::Merge::merge $self->json, $json); | ||
| 101 | # } | ||
| 102 | |||
| 103 | # =head2 pretty | ||
| 104 | |||
| 105 | # my $file = path("foo.json"); | ||
| 106 | # $file->json({ foo => "bar" }); # now {"foo":"bar"} | ||
| 107 | |||
| 108 | # $file->merge({ baz => 12 }); # now {"foo":"bar","baz":12} | ||
| 109 | |||
| 110 | # Merges new data and saves it to file. | ||
| 111 | |||
| 112 | # =cut | ||
| 113 | |||
| 114 | # =head2 do | ||
| 115 | |||
| 116 | # Executes a sub with the json content of the file as input, and stores the result back into the file. | ||
| 117 | |||
| 118 | # =cut | ||
| 119 | |||
| 120 | # sub do { | ||
| 121 | # my ($self, $sub) = @_; | ||
| 122 | # my $val = sub { local $_ = $self->json; $sub->($_); return $_ }->(); | ||
| 123 | # $self->json($val); | ||
| 124 | # } | ||
| 125 | |||
| 126 | # 1; | ||
| 127 | |||
| 128 | use strict; | ||
| 129 | use warnings; | ||
| 130 | package Mojo::File::Role::JSON; | ||
| 131 | |||
| 132 | # ABSTRACT: adds a json method to Mojo::File to store and retrieve JSON | ||
| 133 | |||
| 134 | use Mojo::Base -role; | ||
| 135 | use Mojo::Util qw/dumper/; | ||
| 136 | |||
| 137 | use JSON::MaybeXS; | ||
| 138 | use Hash::Merge; | ||
| 139 | |||
| 140 | our $pretty = 1; | ||
| 141 | our $policy = "die"; | ||
| 142 | |||
| 143 | use Want; | ||
| 144 | |||
| 145 | |||
| 146 | =head1 NAME | ||
| 147 | |||
| 148 | Mojo::File::Role::JSON - Adds a json method to Mojo::File to store and retrieve JSON | ||
| 149 | |||
| 150 | =head1 SYNOPSIS | ||
| 151 | |||
| 152 | use Mojo::File qw/path/; | ||
| 153 | |||
| 154 | my $file = path("data.json")->with_roles('+JSON'); | ||
| 155 | |||
| 156 | # Store JSON | ||
| 157 | $file->json({foo => "bar"}); | ||
| 158 | |||
| 159 | # Retrieve JSON | ||
| 160 | my $data = $file->json; | ||
| 161 | |||
| 162 | # Store with pretty formatting | ||
| 163 | $file->pretty(1)->json({foo => "bar"}); | ||
| 164 | |||
| 165 | # Merge new data into the existing file | ||
| 166 | $file->merge({baz => 42}); | ||
| 167 | |||
| 168 | # Run transformation on existing JSON | ||
| 169 | $file->do(sub { $_->{hello} = "world" }); | ||
| 170 | |||
| 171 | =head1 DESCRIPTION | ||
| 172 | |||
| 173 | This role adds JSON serialization methods to L<Mojo::File> objects, allowing | ||
| 174 | them to easily store and retrieve JSON data. It also includes support for merging | ||
| 175 | and updating JSON content, and optional pretty-printing. | ||
| 176 | |||
| 177 | =head1 METHODS | ||
| 178 | |||
| 179 | =head2 json | ||
| 180 | |||
| 181 | $file->json({ foo => "bar" }); # stores a variable as JSON in the file | ||
| 182 | |||
| 183 | my $data = $file->json(); # retrieves the decoded JSON from the file | ||
| 184 | |||
| 185 | Stores or retrieves JSON data from the file. | ||
| 186 | |||
| 187 | =over 4 | ||
| 188 | |||
| 189 | =item * | ||
| 190 | |||
| 191 | When called with an argument, encodes it as JSON and writes it to the file. If the file's parent directory does not exist, it will be created. | ||
| 192 | |||
| 193 | =item * | ||
| 194 | |||
| 195 | Returns the deserialized data structure when reading. | ||
| 196 | |||
| 197 | =item * | ||
| 198 | |||
| 199 | If called in object context during storage (e.g. `$file->json(...)`), returns the object. | ||
| 200 | |||
| 201 | =item * | ||
| 202 | |||
| 203 | Returns undef if the file does not exist when reading. | ||
| 204 | |||
| 205 | =back | ||
| 206 | |||
| 207 | =cut | ||
| 208 | |||
| 209 | sub json { | ||
| 210 | my $self = shift; | ||
| 211 | if (@_) { | ||
| 212 | unless (-f $self) { $self->dirname->make_path } | ||
| 213 | my $json = JSON::MaybeXS->new->pretty($pretty)->utf8->encode(shift()); | ||
| 214 | $self->spew($json); | ||
| 215 | if (want('OBJECT')) { | ||
| 216 | rreturn $self; | ||
| 217 | } else { | ||
| 218 | rreturn $self->json; | ||
| 219 | } | ||
| 220 | } | ||
| 221 | return undef unless -f $self; | ||
| 222 | |||
| 223 | my $json = decode_json $self->slurp; | ||
| 224 | return $json; | ||
| 225 | } | ||
| 226 | |||
| 227 | =head2 store | ||
| 228 | |||
| 229 | $file->store($data); | ||
| 230 | |||
| 231 | Stores the given data structure as JSON into the file. Alias for C<json()> in write mode. | ||
| 232 | |||
| 233 | Dies if no data is passed. | ||
| 234 | |||
| 235 | =cut | ||
| 236 | |||
| 237 | sub store { | ||
| 238 | my $self = shift; | ||
| 239 | die "Need something to store\n" unless @_; | ||
| 240 | $self->json(shift()) | ||
| 241 | } | ||
| 242 | |||
| 243 | =head2 retrieve | ||
| 244 | |||
| 245 | my $data = $file->retrieve; | ||
| 246 | |||
| 247 | Retrieves and returns the JSON content of the file. | ||
| 248 | |||
| 249 | Alias for C<json()> in read mode. | ||
| 250 | |||
| 251 | =cut | ||
| 252 | |||
| 253 | sub retrieve { | ||
| 254 | my $self = shift; | ||
| 255 | $self->json(); | ||
| 256 | } | ||
| 257 | |||
| 258 | =head2 pretty | ||
| 259 | |||
| 260 | $file->pretty(1); | ||
| 261 | |||
| 262 | Enables or disables pretty-printed JSON when writing. | ||
| 263 | |||
| 264 | Returns the file object itself. | ||
| 265 | |||
| 266 | =cut | ||
| 267 | |||
| 268 | sub pretty { | ||
| 269 | my $self = shift; | ||
| 270 | $pretty = shift if @_; | ||
| 271 | return $self; | ||
| 272 | } | ||
| 273 | |||
| 274 | =head2 policy | ||
| 275 | |||
| 276 | $file->policy("die"); | ||
| 277 | |||
| 278 | Sets the policy for file-not-found handling. | ||
| 279 | |||
| 280 | B<Note:> Currently unused, reserved for future use. | ||
| 281 | |||
| 282 | =cut | ||
| 283 | |||
| 284 | sub policy { | ||
| 285 | my $self = shift; | ||
| 286 | $policy = shift if @_; | ||
| 287 | return $self; | ||
| 288 | } | ||
| 289 | |||
| 290 | =head2 merge | ||
| 291 | |||
| 292 | $file->merge({ bar => 42 }); | ||
| 293 | |||
| 294 | Merges the provided hash into the current JSON structure in the file and stores the result. | ||
| 295 | |||
| 296 | Uses L<Hash::Merge> for merging. | ||
| 297 | |||
| 298 | =cut | ||
| 299 | |||
| 300 | sub merge { | ||
| 301 | my ($self, $json, $merger) = @_; | ||
| 302 | $self->json(Hash::Merge::merge $self->json, $json); | ||
| 303 | } | ||
| 304 | |||
| 305 | =head2 do | ||
| 306 | |||
| 307 | $file->do(sub { $_->{count}++ }); | ||
| 308 | |||
| 309 | Executes a subroutine on the current JSON content. | ||
| 310 | |||
| 311 | The sub receives the data structure in C<$_>, and its return value is stored back in the file. | ||
| 312 | |||
| 313 | =cut | ||
| 314 | |||
| 315 | sub do { | ||
| 316 | my ($self, $sub) = @_; | ||
| 317 | my $val = sub { local $_ = $self->json; $sub->($_); return $_ }->(); | ||
| 318 | $self->json($val); | ||
| 319 | } | ||
| 320 | |||
| 321 | 1; | ||
| 322 | |||
| 323 | =head1 SEE ALSO | ||
| 324 | |||
| 325 | L<Mojo::File>, L<JSON::MaybeXS>, L<Hash::Merge>, L<Want> | ||
| 326 | |||
| 327 | =head1 AUTHOR | ||
| 328 | |||
| 329 | Simone Cesano | ||
| 330 | |||
| 331 | This software is copyright (c) 2025 by Simone Cesano. | ||
| 332 | |||
| 333 | =head1 COPYRIGHT AND LICENSE | ||
| 334 | |||
| 335 | This software is copyright (c) 2025 by Simone Cesano. | ||
| 336 | |||
| 337 | This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. | ||
| 338 | |||
| 339 | =cut |