File-SOPS
view release on metacpan or search on metacpan
lib/File/SOPS/Format/JSON.pm view on Meta::CPAN
C<sops> section removed. Returns nothing when the text cannot be read that way.
The contract is L<File::SOPS::Format::YAML/parse_in_document_order>'s, and so
is the implementation: a JSON document is one YAML 1.2 document, and this
distribution keeps a single order-preserving reader for both formats
(C<docs/adr/0001>, C<docs/adr/0036>). Only order and shape are taken from it,
never a value.
=head2 serialize
my $json = File::SOPS::Format::JSON->serialize(
data => \%data,
metadata => $metadata_obj,
);
Class method to serialize data and metadata to JSON.
The C<data> parameter must be a HashRef. The C<metadata> parameter must be
a L<File::SOPS::Metadata> object.
Dies if C<data> has a top-level C<sops> key: that is where the metadata section
is written, so the value would be overwritten. Until 0.003 it was, silently,
and the resulting document failed its own MAC because the digest had already
covered the discarded value.
Returns a pretty-printed, canonically-ordered JSON string with the C<sops>
section included.
=head2 emit
my $json = File::SOPS::Format::JSON->emit(\%data);
Class method to emit a data structure as JSON, with no C<sops> section and no
metadata of any kind -- a plain document. This is what L<File::SOPS/decrypt_file>
writes and what L<File::SOPS/edit> hands to the editor.
Returns UTF-8 encoded bytes, unconditionally (the encoder is built with
C<utf8 =E<gt> 1>, which encodes regardless of whether the strings carry Perl's
UTF-8 flag), pretty-printed and canonically ordered. See
L<File::SOPS/Character encoding>.
L</serialize> is this method plus the metadata section, so both go through the
same encoder rather than two copies of its options. Those options are not
cosmetic -- C<canonical> is what makes key order sorted, which the MAC's encrypt
side relies on -- so a change here moves the encrypted document as well as the
plaintext one.
B<Floats are written in a form that parses back to the same double.>
L<Cpanel::JSON::XS> renders a float through C<%.15g>, while the MAC digest
covers the shortest decimal that round-trips -- up to 17 significant digits. For
a value needing 16 or 17 the document stated one number and the digest another,
and the file failed its own verification; because this backend keeps no copy of
the text it parsed, that applied to floats read out of a sops-written document
as much as to computed ones, so C<rotate> could destroy a value the reference
implementation had written correctly. This method now reparses its own output
and substitutes the canonical decimal from
L<File::SOPS::Encrypted/value_to_bytes> only where the value does not survive,
carried as a L<Math::BigFloat> under C<allow_bignum> -- the only wrapper
measured to reach JSON as a bare number instead of a quoted string. A float that
already emitted faithfully keeps exactly the bytes it had, C<-0.0> included.
C<NaN> and C<Inf> are unchanged. See
L<docs/adr/0006|https://github.com/Getty/p5-file-sops/blob/main/docs/adr/0006-floats-are-emitted-in-a-form-that-parses-back-to-the-same-double.md>.
B<A float leaf carrying its own string form is written as a number.>
L<Cpanel::JSON::XS> writes a scalar with a public string half as a quoted JSON
string whenever that half differs from its own rendering of the number, so such
a leaf reached the document as a B<string> where the caller passed a number. It
failed nothing: the digest covers the canonical decimal either way, the file
verifies, and C<sops -d> exits 0 and reads a string back -- the value's type
changed and nothing said so. The round-trip check above could not see it,
because the reparsed string re-derives the very text the digest covers. It now
also asks whether the reparsed leaf is still a float and, where it is not,
sends the leaf to the same L<Math::BigFloat> carrier, which writes the
canonical decimal as a bare number.
The usual source is L<File::SOPS/extract>, which returns a
L<Scalar::Util/dualvar> for a float leaf (see
L<File::SOPS::Encrypted/canonical_float_dualvar>); feeding that return value
back into L<File::SOPS/encrypt> under an unencrypted key is what reaches this.
The other is a float that arrived through a YAML parse, since L<YAML::XS>
retains the source text of every scalar it parses. An B<encrypted> slot is
unaffected -- the leaf is an C<ENC[...]> string before this method sees it --
and L<File::SOPS::Format::YAML> has always written the string half bare and
correctly, which is the document this now produces too. A dualvar whose text is
what Cpanel would have written anyway, such as C<1.5> or C<-0.0>, still emits
as the number it always did, byte for byte. See k78,
L<docs/adr/0011|https://github.com/Getty/p5-file-sops/blob/main/docs/adr/0011-a-float-leaf-that-carries-its-own-string-form-is-repaired.md>
and
L<docs/adr/0010|https://github.com/Getty/p5-file-sops/blob/main/docs/adr/0010-extract-returns-a-float-that-prints-all-its-digits.md>.
B<An integer leaf carrying its own, different string form is refused.>
L<Cpanel::JSON::XS> writes it as a quoted string -- C<"007"> for a C<7> a YAML
parser kept the source spelling of, C<"five"> for a
C<Scalar::Util/dualvar> -- while L<File::SOPS::Encrypted/detect_type> calls the
leaf an C<int>, so the MAC digest covers the B<number>. The document and its
own MAC then state different things and neither sops nor this module can read
the file back: measured against sops 3.13.3, C<sops -d> exit 51 for C<007>,
C<+7>, C<-0>, C<1e3> and every contradicting dualvar. The refusal names the
leaf's key path and neither half of the value. Refused rather than repaired
because nothing measurable separates a spelling from a contradiction; an
B<encrypted> slot is unaffected, and L<File::SOPS::Format::YAML> refuses only
the contradicting ones, because it writes a source spelling back faithfully.
See k84 and
L<docs/adr/0012|https://github.com/Getty/p5-file-sops/blob/main/docs/adr/0012-an-integer-leaf-whose-string-half-disagrees-is-refused.md>.
B<A reference as a leaf value is refused>, with one exception. L<Cpanel::JSON::XS>
under C<allow_bignum> writes a C<Math::BigFloat> / C<Math::BigInt> as a bare
number, and an unblessed C<\1> / C<\0> as bare C<true> / C<false> -- the
documented JSON::XS convention for SCALAR refs -- while
L<File::SOPS::Encrypted/detect_type> calls the leaf C<str> so the MAC digest
covers its B<stringification>. The document and its own MAC then state different
things, and the file is unreadable to sops and to this module alike. Until
0.003 it was written anyway, without a word; now it dies naming the class or
reference kind (never the value).
The exception is an exact L<JSON::PP::Boolean>, which this emitter writes as
bare C<true> / C<false> -- the one reference whose document form and digest
agree. A B<subclass> of it is not covered: C<detect_type> calls it C<bool> but
Cpanel refuses it (or the carrier pipeline writes it as a tag), so it is
refused like any other object.
( run in 0.936 second using v1.01-cache-2.11-cpan-60884a8df76 )