On Feb 20, 2011, at 6:19 PM, Jiří Pavlovský wrote: > On 15.2.2011 15:04, Stevan Little wrote: >> >>> I'm pondering what way to use to serialize my Moose classes to JSON. I've >>> have been looking to MooseX::Storage but it seems it cannot handle more >>> than basic attribute types. Just the very first class I tried: "Cannot >>> handle type constraint (MooseX::Types::URI::Uri)" >>> There seems to be a way to add custom handlers, but the documentation is >>> scarce. >>> I also tried the MooseX::Storage::Format::JSONpm, but the result is the >>> same. >>> >> Sorry, this feature never really got properly documented, but patches are >> always welcome, and doc patches are especially welcome. >> >> What you want to do is to look at the methods listed here: >> >> http://search.cpan.org/~bobtfish/MooseX-Storage-0.29/lib/MooseX/Storage/Engine.pm#Type_Constraint_Handlers > Hello, > > I was wondering, if there is no serialization handler for a type like for > example "MooseX::Types::URI::Uri" in this case. > What we need to serialize is not really "MooseX::Types::URI::Uri" but URI > object, which has its own stringification method. > One doesn't really need more to serialize constraints like MooseX::Types::URI > or MooseX::Types::Path::Class. > Would it make sense as a last resort to check whether object has its own > stringification method and use that before it gives up and dies? > Or am I just totally off base?
Well, it is not always going to be clear that a stringification method will be able to round tip, it might just be a visual representation of the object. I think making the assumption that stringification will roundtrip is a pretty dangerous and risky one. Additionally I think it is dangerous as well to assume that, like URI and Path::Class, you can pass the serialized string back to ->new and have everything Just Work. In short, stringification methods are not serialization methods and if they were they would probably be called ->serialize and not ->as_string. But really, I highly recommend that you just use the existing mechanisms and write your own inflate/deflate methods for these types. Since it is type based dispatching you really will only ever have to write a handful of them, which is not really a whole lot of extra work in the long run. - Stevan
