From 2410ee2dd43639b09154eb9ea8f57c276bed395d Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Wed, 14 Jul 2010 16:31:20 -0700 Subject: [PATCH] Add a Hash type to go along with Array types for handling JSON. --- lib/WebGUI/Types.pm | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/WebGUI/Types.pm b/lib/WebGUI/Types.pm index 25e70df5b..3b7dec8ac 100644 --- a/lib/WebGUI/Types.pm +++ b/lib/WebGUI/Types.pm @@ -70,4 +70,18 @@ coerce 'WebGUI::Type::JSONArray' => via { return []; }, ; +subtype 'WebGUI::Type::JSONHash' + => as 'HashRef' +; + +coerce 'WebGUI::Type::JSONHash' + => from Str + => via { my $struct = eval { JSON::from_json($_); }; $struct ||= {}; return $struct; }, +; + +coerce 'WebGUI::Type::JSONHash' + => from Undef + => via { return {}; }, +; + 1;