Skip to content

Instantly share code, notes, and snippets.

@cho45
Created March 26, 2014 08:17
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 cho45/9778771 to your computer and use it in GitHub Desktop.
Save cho45/9778771 to your computer and use it in GitHub Desktop.
use Mouse::Meta::Class;
use JSON::XS;
sub TO_JSON {
my ($self) = @_;
my $klass = +Mouse::Meta::Class->initialize(ref $self);
+{
map {
my $key = $_;
my $val = $self->{$key};
my $attr = $klass->get_attribute($key);
if ($attr) {
if ($attr->type_constraint->is_a_type_of('Bool')) {
$val = $val ? JSON::XS::true : JSON::XS::false;
}
($key => $val);
} else {
();
}
}
keys %{ $self }
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment