| 1 | # Copyrights 2023 by [Mark Overmeer <markov@cpan.org>]. | ||
| 2 | # For other contributors see ChangeLog. | ||
| 3 | # See the manual pages for details on the licensing terms. | ||
| 4 | # Pod stripped from pm file by OODoc 2.03. | ||
| 5 | package Math::Formula::Config::JSON; | ||
| 6 | use vars '$VERSION'; | ||
| 7 | $VERSION = '0.16'; | ||
| 8 | |||
| 9 | use base 'Math::Formula::Config'; | ||
| 10 | |||
| 11 | use warnings; | ||
| 12 | use strict; | ||
| 13 | |||
| 14 | use Log::Report 'math-formula'; | ||
| 15 | use Scalar::Util 'blessed'; | ||
| 16 | use File::Slurper 'read_binary'; | ||
| 17 | use Cpanel::JSON::XS (); | ||
| 18 | |||
| 19 | my $json = Cpanel::JSON::XS->new->pretty->utf8->canonical(1); | ||
| 20 | |||
| 21 | |||
| 22 | #---------------------- | ||
| 23 | |||
| 24 | sub save($%) | ||
| 25 | { my ($self, $context, %args) = @_; | ||
| 26 | my $name = $context->name; | ||
| 27 | |||
| 28 | my $index = $context->_index; | ||
| 29 | my $tree = $self->_set($index->{attributes}); | ||
| 30 | $tree->{formulas} = $self->_set($index->{formulas}); | ||
| 31 | |||
| 32 | my $fn = $self->path_for($args{filename} || "$name.json"); | ||
| 33 | open my $fh, '>:raw', $fn | ||
| 34 | or fault __x"Trying to save context '{name}' to {fn}", name => $name, fn => $fn; | ||
| 35 | |||
| 36 | $fh->print($json->encode($tree)); | ||
| 37 | $fh->close | ||
| 38 | or fault __x"Error on close while saving '{name}' to {fn}", name => $name, fn => $fn; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub _set($) | ||
| 42 | { my ($self, $set) = @_; | ||
| 43 | my %data; | ||
| 44 | $data{$_ =~ s/^ctx_//r} = $self->_serialize($_, $set->{$_}) for keys %$set; | ||
| 45 | \%data; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub _serialize($$) | ||
| 49 | { my ($self, $name, $what) = @_; | ||
| 50 | my %attrs; | ||
| 51 | |||
| 52 | if(blessed $what && $what->isa('Math::Formula')) | ||
| 53 | { if(my $r = $what->returns) { $attrs{returns} = $r }; | ||
| 54 | $what = $what->expression; | ||
| 55 | } | ||
| 56 | |||
| 57 | my $v = ''; | ||
| 58 | if(blessed $what && $what->isa('Math::Formula::Type')) | ||
| 59 | { # strings without quote | ||
| 60 | $v = $what->isa('MF::STRING') ? $what->value | ||
| 61 | : $what->isa('MF::BOOLEAN') ? ($what->value ? Cpanel::JSON::XS::true : Cpanel::JSON::XS::false) | ||
| 62 | : $what->isa('MF::FLOAT') ? $what->value # otherwise JSON writes a string | ||
| 63 | : $what->token; | ||
| 64 | } | ||
| 65 | elsif(ref $what eq 'CODE') | ||
| 66 | { warning __x"cannot (yet) save CODE, skipped '{name}'", name => $name; | ||
| 67 | return undef; | ||
| 68 | } | ||
| 69 | elsif(length $what) | ||
| 70 | { $v = '=' . $what; | ||
| 71 | } | ||
| 72 | |||
| 73 | if(keys %attrs) | ||
| 74 | { $v .= '; ' . (join ', ', map "$_='$attrs{$_}'", sort keys %attrs); | ||
| 75 | } | ||
| 76 | |||
| 77 | return $v; | ||
| 78 | } | ||
| 79 | |||
| 80 | |||
| 81 | |||
| 82 | sub load($%) | ||
| 83 | { my ($self, $name, %args) = @_; | ||
| 84 | my $fn = $self->path_for($args{filename} || "$name.json"); | ||
| 85 | |||
| 86 | my $tree = $json->decode(read_binary $fn); | ||
| 87 | my $formulas = delete $tree->{formulas}; | ||
| 88 | |||
| 89 | my $attrs = $self->_set_decode($tree); | ||
| 90 | Math::Formula::Context->new(name => $name, | ||
| 91 | %$attrs, | ||
| 92 | formulas => $self->_set_decode($formulas), | ||
| 93 | ); | ||
| 94 | } | ||
| 95 | |||
| 96 | sub _set_decode($) | ||
| 97 | { my ($self, $set) = @_; | ||
| 98 | $set or return {}; | ||
| 99 | |||
| 100 | my %forms; | ||
| 101 | $forms{$_} = $self->_unpack($_, $set->{$_}) for keys %$set; | ||
| 102 | \%forms; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub _unpack($$) | ||
| 106 | { my ($self, $name, $encoded) = @_; | ||
| 107 | my $dummy = Math::Formula->new('dummy', '7'); | ||
| 108 | |||
| 109 | if(ref $encoded eq 'JSON::PP::Boolean') | ||
| 110 | { return MF::BOOLEAN->new(undef, $encoded); | ||
| 111 | } | ||
| 112 | |||
| 113 | if($encoded =~ m/^\=(.*?)(?:;\s*(.*))?$/) | ||
| 114 | { my ($expr, $attrs) = ($1, $2 // ''); | ||
| 115 | my %attrs = $attrs =~ m/(\w+)\='([^']+)'/g; | ||
| 116 | return Math::Formula->new($name, $expr =~ s/\\"/"/gr, %attrs); | ||
| 117 | } | ||
| 118 | |||
| 119 | # No JSON implementation parses floats and ints cleanly into SV | ||
| 120 | # So, we need to check it by hand. Gladly, ints are converted | ||
| 121 | # to strings again when that was the intention. | ||
| 122 | |||
| 123 | $encoded =~ qr/^[0-9]+$/ ? MF::INTEGER->new(undef, $encoded + 0) | ||
| 124 | : $encoded =~ qr/^[0-9][0-9.e+\-]+$/ ? MF::FLOAT->new(undef, $encoded + 0.0) | ||
| 125 | : MF::STRING->new(undef, $encoded); | ||
| 126 | } | ||
| 127 | |||
| 128 | #---------------------- | ||
| 129 | |||
| 130 | 1; |