forked from plack/Plack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JSON.pm
39 lines (33 loc) · 802 Bytes
/
JSON.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package Plack::BodyParser::JSON;
use strict;
use warnings;
use utf8;
use JSON ();
use Encode qw(encode_utf8);
use Hash::MultiValue;
sub new {
my $class = shift;
bless {buffer => ''}, $class;
}
sub add {
my $self = shift;
$self->{buffer} .= $_[0] if defined $_[0];
}
sub finalize {
my $self = shift;
my $p = JSON::decode_json($self->{buffer});
my $params = Hash::MultiValue->new();
if (ref $p eq 'HASH') {
while (my ($k, $v) = each %$p) {
if (ref $v eq 'ARRAY') {
for (@$v) {
$params->add(encode_utf8($k), encode_utf8($_));
}
} else {
$params->add(encode_utf8($k), encode_utf8($v));
}
}
}
return ($params, Hash::MultiValue->new());
}
1;