How to save boolean type from YAML document in Perl 5?

I have a YAML document containing booleans:

--- ok: false 

I want to load it in Perl 5 and save the type "boolean" so that later I can model the document correctly in JSON using true / false values, and not "" / "1" .

The following converter that I wrote does not store boolean values:

 #!/usr/bin/env perl use strict; use warnings; use YAML::XS qw<LoadFile>; use JSON::MaybeXS (); print JSON::MaybeXS->new->ascii->pretty->canonical->encode(LoadFile shift) 

Here is the (damaged) output:

 { "fine" : "" } 

I hope some hooks exist in some YAML loader to display true / false in JSON::true / JSON::false or $Types::Serialiser::true / $Types::Serialiser::false .

+5
source share
1 answer

If such a YAML module exists, it should be rather obscure. The one you use here, YAML::XS simply translates the logical values ​​in the YAML data to the standard internal values PL_sv_yes and PL_sv_no , and those (as far as I can see) cannot be recognized as special ones.

On the plus side, it seems pretty simple to schedule YAML::XS to use Types::Serialiser for booleans and send a pull request.

0
source

Source: https://habr.com/ru/post/1232197/


All Articles