Skip to content

Instantly share code, notes, and snippets.

@FROGGS

FROGGS/Ser.pm Secret

Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FROGGS/c82f5d98d957d5240d15 to your computer and use it in GitHub Desktop.
Save FROGGS/c82f5d98d957d5240d15 to your computer and use it in GitHub Desktop.
use lib '.';
use Ser;
'TEST_FILE'.IO.spurt: serialize({ foo => 42 });
say deserialize('TEST_FILE'.IO.slurp);
module Ser;
sub serialize($obj is copy) is export {
my Mu $sh1 := nqp::list_s();
my Mu $sc := nqp::createsc(nqp::unbox_s('FLUBBER'));
nqp::scsetobj($sc, 0, $obj);
nqp::setobjsc($obj, $sc);
my $serialized = nqp::serialize($sc, $sh1);
nqp::p6box_s(nqp::join("\n", $sh1)) ~ "\n" ~ $serialized
}
sub deserialize($b64) is export {
my Mu $sh2 := nqp::list_s();
my @lines = $b64.split("\n");
my str $serialized = nqp::unbox_s(@lines.pop);
nqp::push_s($sh2, nqp::null_s());
nqp::push_s($sh2, nqp::unbox_s($_)) for @lines;
my Mu $dsc2 := nqp::createsc(nqp::unbox_s('FLUBBER'));
nqp::deserialize($serialized, $dsc2, $sh2, nqp::list(), nqp::null());
nqp::scgetobj($dsc2, 0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment