Data Formats
44.1 JSON
use JSON::Fast; # zef install JSON::Fast
my %data = name => 'Ana', tags => <raku perl>, active => True, score => 9.5;
my $json = to-json %data, :sorted-keys; # pretty by default; :!pretty for compact
say $json;
my $parsed = from-json '{"x": 1, "list": [1, 2, 3], "nested": {"ok": true}}';
say $parsed<list>.sum, ' ', $parsed<nested><ok>;
'data.json'.IO.spurt(to-json(%data));
my %loaded = from-json('data.json'.IO.slurp);44.2 CSV
use Text::CSV; # zef install Text::CSV
my $csv = Text::CSV.new;
my @rows = $csv.getline_all(open 'people.csv'); # array of arrays
my @aoh = csv(in => 'people.csv', headers => 'auto'); # array of hashes
for @aoh -> %row { say "%row<name>: %row<email>" }
csv(in => [['name', 'note'], ['Ana', 'Says "hi", then leaves']], out => 'out.csv');44.3 YAML, XML and Others
And, of course, write your own grammar for custom formats (Module 29). That's often the most Raku-ish solution.
use YAMLish; # zef install YAMLish
my $config = load-yaml('config.yml'.IO.slurp);
say save-yaml({ port => 8080, hosts => <a b> });
use LibXML; # zef install LibXML
my $doc = LibXML.parse(string => '<books><book price="25"><title>Raku</title></book></books>');
for $doc.find('//book[@price > 20]') -> $book {
say $book.find('title')[0].textContent, ': ', $book.getAttribute('price');
}
# Also: TOML (TOML), INI (Config::INI), MessagePack (Data::MessagePack), Base64 (Base64 / MIME::Base64)