Skip Menu |

This queue is for tickets about the MooseX-Types-JSON CPAN distribution.

Report information
The Basics
Id: 68272
Status: resolved
Priority: 0/
Queue: MooseX-Types-JSON

People
Owner: Nobody in particular
Requestors: shuff [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in:
  • 0.01
  • 0.02
Fixed in: (no value)



Subject: enhancement: type coercions
hello Michael! here's my initial attempt to add coercions to your JSON types; please let me know what you think. the patch is generated against the release tarball of MooseX-Types-JSON-0.02. -Steve -- Steve Huff (SHUFF) * shuff@cpan.org
Subject: perl-MooseX-Types-JSON-0.02_coercions.patch
diff -Naur -x '*.git*' MooseX-Types-JSON-0.02-orig/lib/MooseX/Types/JSON.pm MooseX-Types-JSON-0.02/lib/MooseX/Types/JSON.pm --- MooseX-Types-JSON-0.02-orig/lib/MooseX/Types/JSON.pm 2009-10-25 17:06:38.000000000 -0400 +++ MooseX-Types-JSON-0.02/lib/MooseX/Types/JSON.pm 2011-05-17 14:54:31.194682839 -0400 @@ -26,6 +26,8 @@ 'relaxed' see L<JSON::XS>. All the heavy lifting in the background is also done by L<JSON::XS>. +Coercions from Defined types are included. + =over =item * JSON @@ -48,11 +50,19 @@ where { ref( eval { JSON::XS->new->decode($_) } ) ne '' }, message { "Must be valid JSON" }; +coerce JSON, + from 'Defined', + via { JSON::XS->new->allow_nonref->encode($_) }; + subtype relaxedJSON, as "Str", where { ref( eval { JSON::XS->new->relaxed->decode($_) } ) ne '' }, message { "Must be at least relaxed JSON" }; +coerce relaxedJSON, + from 'Defined', + via { JSON::XS->new->allow_nonref->encode($_) }; + =head1 AUTHOR Michael Langner, C<< <mila at cpan.org> >> diff -Naur -x '*.git*' MooseX-Types-JSON-0.02-orig/t/02-coercions.t MooseX-Types-JSON-0.02/t/02-coercions.t --- MooseX-Types-JSON-0.02-orig/t/02-coercions.t 1969-12-31 19:00:00.000000000 -0500 +++ MooseX-Types-JSON-0.02/t/02-coercions.t 2011-05-17 15:05:21.624171269 -0400 @@ -0,0 +1,29 @@ +use Test::More tests => 4; + +{ + package Foo; + + use Moose; + use Moose::Util::TypeConstraints; + use MooseX::Types::JSON qw( JSON relaxedJSON ); + + has json_strict => ( is => 'rw', isa => JSON, coerce => 1 ); + has json_relaxed => ( is => 'rw', isa => relaxedJSON, coerce => 1 ); +} + +my %json = ( 'foo' => 'bar', 'answer' => '42' ); +my %nojson = ( ); + +my $foo = Foo->new; + +eval { $foo->json_strict(\%json) }; +ok( $@ eq "", "hash => strict" ); + +eval { $foo->json_relaxed(\%json) }; +ok( $@ eq "", "hash => relaxed" ); + +eval { $foo->json_strict(\%nojson) }; +ok( $@ eq "", "nohash => strict" ); + +eval { $foo->json_relaxed(\%nojson) }; +ok( $@ eq "", "nohash => relaxed" );
On Tue May 17 15:25:56 2011, SHUFF wrote: Show quoted text
> hello Michael! > > here's my initial attempt to add coercions to your JSON types; please > let me know what you think. > > the patch is generated against the release tarball of > MooseX-Types-JSON-0.02. > > -Steve >
hello steve, thanks for your patch. will be released as 0.03 soon. michael
On Mon May 23 05:57:12 2011, MILA wrote: Show quoted text
> On Tue May 17 15:25:56 2011, SHUFF wrote:
> > hello Michael! > > > > here's my initial attempt to add coercions to your JSON types; please > > let me know what you think. > > > > the patch is generated against the release tarball of > > MooseX-Types-JSON-0.02. > > > > -Steve > >
> > > hello steve, > > thanks for your patch. will be released as 0.03 soon.
hi Michael! any plans for an 0.03 release? thanks, -steve -- Steve Huff (SHUFF) * shuff@cpan.org