diff --git a/lib/JSON/Tiny.pm b/lib/JSON/Tiny.pm index 342f365..063b127 100644 --- a/lib/JSON/Tiny.pm +++ b/lib/JSON/Tiny.pm @@ -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)); diff --git a/nbproject/perl_project_existing_sources.xml b/nbproject/perl_project_existing_sources.xml new file mode 100644 index 0000000..e69de29 diff --git a/t/07-TO_JSON.t b/t/07-TO_JSON.t new file mode 100644 index 0000000..ae6d857 --- /dev/null +++ b/t/07-TO_JSON.t @@ -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, 'Interested', 'Include property' ); +isnt( %something, 'Not interested', 'Exclude property'); \ No newline at end of file