Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/JSON/Tiny.pm
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ multi to-json(Mu:D $s) {
die "Can't serialize an object of type " ~ $s.WHAT.perl
}

multi to-json ( $_ where *.can('TO_JSON') ) {
to-json .TO_JSON
}

sub from-json($text) is export {
my $a = JSON::Tiny::Actions.new();
my $o = JSON::Tiny::Grammar.parse($text, :actions($a));
Expand Down
Empty file.
20 changes: 20 additions & 0 deletions t/07-TO_JSON.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use v6;
use Test;
use JSON::Tiny;
plan 2;

class Something {
has Str $.foo;
has Str $.bar;

method TO_JSON {
return {
foo => $.foo
}
}
}

my %something = from-json( to-json Something.new( foo => 'Interested', bar => 'Not interested' ) );

is( %something<foo>, 'Interested', 'Include property' );
isnt( %something<bar>, 'Not interested', 'Exclude property');