se Data::Dumper;
use Try::Tiny;
use File::Path qw/mkpath/;
use File::Temp;
use File::Compare;
use JSON::XS;
use File::stat qw/stat/;
use Mojo::ByteStream qw/b/;
use strict;
use warnings;
our %Bucket2
tateFile;
return $cached if $mod_time && $mod_time == stat($StateFile)->mtime;
our $j ||= JSON::XS->new;
-e $StateFile or LOGDIE "Missing state file $StateFile";
$cached = $j->decode(M
{
my $class = shift;
my $state = shift;
my $dir = dirname($StateFile);
our $j ||= JSON::XS->new;
mkpath $dir;
my $temp = File::Temp->new(DIR => $dir, UNLINK => 0);
print $t
=>'HashRef',
builder=>'_build_normalized');
sub _build_normalized {
return +{
JSON => 'application/json',
JS => 'application/javascript',
PERL => 'application/perl',
HTML => 'tex
sub start : POST Chained('/') CaptureArg(0) { ... }
sub is_json : Chained('start') Consumes('application/json') { ... }
sub is_urlencoded : Chained('start') Consumes('applicat
form-data') { ... }
## Alternatively, for common types...
sub is_json : Chained('start') Consume(JSON) { ... }
sub is_urlencoded : Chained('start') Consumes(UrlEncoded) { ...
;
use WWW::Foursquare::Events;
use WWW::Foursquare::Pages;
use WWW::Foursquare::Pageupdates;
use JSON;
use LWP;
use URI::Escape;
sub new {
my ($class, %params) = @_;
my $self = {};
l" if $self->{debug};
my $res = $self->{request}->{ua}->get($result_url);
return decode_json($res->content()) if $res->code() == 200;
# throw exception
die $res->content();
}
sub _
package WWW::Foursquare::Response;
use strict;
use warnings;
use JSON;
our %ERROR_TYPE = (
invalid_auth => 'OAuth token was not provided or was invalid',
param_error => 'A re
elf, $class;
return $self;
}
sub process {
my ($self, $res) = @_;
my $data = decode_json($res->content());
my $code = $res->code();
# response is OK
return $data->{response}
Point;
use Moose;
use MooseX::Storage;
our $VERSION = '0.01';
with Storage('format' => 'JSON', 'io' => 'File');
has 'x' => (is => 'rw', isa => 'Int');
has 'y' => (is => 'rw', isa => 'In
zation format
## (in this case JSON)
# pack the class into a JSON string
$p->freeze(); # { "__CLASS__" : "Point-0.01", "x" : 10, "y" : 10 }
# unpack the JSON string into a class
my $p2 = P
methods to load/store a class
## on the file system
$p->store('my_point.json');
my $p2 = Point->load('my_point.json');
=head1 DESCRIPTION
MooseX::Storage is a serialization framework for Moo
eX::Attribute::Deflator;
use JSON;
if($ENV{HARNESS_ACTIVE}) {
deflate [qw(ArrayRef HashRef)], via { JSON->new->utf8->canonical->encode($_) },
inline_as {'JSON->new->utf8->canonical->encod
ef)], via { JSON->new->utf8->canonical->decode($_) },
inline_as {'JSON->new->utf8->canonical->decode($value)'};
} else {
deflate [qw(ArrayRef HashRef)], via { JSON::encode_json($_) },
inline_as {'JSON::encode_json($value)'};
inflate [qw(ArrayRef HashRef)], via { JSON::decode_json($_) },
inline_as {'JSON::decode_json($value)'};
}
deflate 'ScalarRef', via {$$_}, inl
tempt to encode C<application/json> since the two most commonly used
approaches (L<Catalyst::View::JSON> and L<Catalyst::Action::REST>) have already configured
their JSON encoders to produce properly
nses. If you are rolling your
own JSON encoding, you may need to set the encoder to do the right thing (or override
the global regular expression to include the JSON media type).
=head2 Encoding wit
Loading L<MooseX::Attribute::Deflator::Moose>
will cause HashRefs and ArrayRefs to be encoded as JSON strings. However, you can simply overwrite
those deflators (and inflators) to deflate to somethin
C<benchmark.pl> tests three ways of deflating the value of a HashRef attribute
to a json encoded string (using L<JSON>).
my $obj = MyBenchmark->new( hashref => { foo => 'bar' } );
my $attr =
ate($obj);
Using the deflate attribute method, supplied by this module.
=item accessor
JSON::encode_json($obj->hashref);
If the attribute comes with an accessor, you can use this
method, to defl
,
'morph without arguments prints json'
);
$result = qx(bin/morph /foo/bar);
like(
$result,
qr/^(5|"5")\n$/,
'morph without arguments prints json'
);
$result = qx(bin/morph --format=
er::MultiPart'
);
my $json_parser = HTTP::Entity::Parser->new();
$json_parser->register(
'application/x-www-form-urlencoded',
'HTTP::Entity::Parser::UrlEncoded'
);
$json_parser->register(
t'
);
$json_parser->register(
'application/json',
'HTTP::Entity::Parser::JSON'
);
my $json_only_parser = HTTP::Entity::Parser->new();
$json_only_parser->register(
'application/json',
'HTTP::Entity::Parser::JSON'
);
sub _build_request_body_parser {
my $self = shift;
if ( $self->env->{'kossy.request.parse_json_body'} ) {
return $json_parser;
}
$default_parse
e JSON::XS ();
use constant DEFAULT_CHARSET => 'utf-8';
my $json = undef;
sub _default_formatter {
my $value = shift;
$json ||= JSON::XS->new->allow_nonref->allow_blessed;
return $json-
value ), returns JavaScript representation for the value.
JSON::XS-based by default.
* charset - Charset, utf-8 by default.
=cut
sub new {
my ( $cl
ad1 SYNOPSIS
package Point;
use Moose;
use MooseX::Storage;
with Storage('format' => 'JSON', 'io' => 'File');
has 'x' => (is => 'rw', isa => 'Int');
has 'y' => (is => 'rw', isa => '
ad1 SYNOPSIS
package Point;
use Moose;
use MooseX::Storage;
with Storage('format' => 'JSON', 'io' => 'File');
has 'x' => (is => 'rw', isa => 'Int');
has 'y' => (is => 'rw', isa => '
YAML string
$p->freeze();
# ----
# __CLASS__: "Point"
# x: 10
# y: 10
# unpack the JSON string into a class
my $p2 = Point->thaw(<<YAML);
----
__CLASS__: "Point"
x: 10
y: 10
::Dumper::Sortkeys = 1;
return Data::Dumper::Dumper(shift);
},
'JSON' => sub { require JSON; return JSON->new->pretty->encode(shift) },
'YAML' => sub { require YAML; return YAML::D
e_json {
my ($class, $json) = @_;
eval { require JSON::Any; JSON::Any->import };
confess "Could not load JSON module because : $@" if $@;
utf8::encode($json) if utf8::is_utf8($json);
my $data = eval { JSON::Any->jsonToObj($json) };
if ($@) {
confess "There was an error when attempting to peek at JSON: $@";
}
return $data;
}
sub _inflate_yaml {
my ($
unfreeze/unpack it.
The C<$data> can be either a perl HASH ref or some kind of serialized
data (JSON, YAML, etc.).
The C<%options> are as follows:
=over 4
=item I<format>
If this is left blank,
head1 SYNOPSIS
package Point;
use Moose;
use MooseX::Storage;
with Storage('format' => 'JSON', 'io' => 'AtomicFile');
has 'x' => (is => 'rw', isa => 'Int');
has 'y' => (is => 'rw', isa
methods to load/store a class
## on the file system
$p->store('my_point.json');
my $p2 = Point->load('my_point.json');
=head1 METHODS
=over 4
=item B<load ($filename)>
=item B<store ($file
head1 SYNOPSIS
package Point;
use Moose;
use MooseX::Storage;
with Storage('format' => 'JSON', 'io' => 'File');
has 'x' => (is => 'rw', isa => 'Int');
has 'y' => (is => 'rw', isa => 'In
methods to load/store a class
## on the file system
$p->store('my_point.json');
my $p2 = Point->load('my_point.json');
=head1 METHODS
=over 4
=item B<load ($filename)>
=item B<store ($file
(
new => 1,
rw => [qw/req res stash args tx debug json_serializer/]
);
use Kossy::Exception;
our $VERSION = '0.63';
# for IE7 JSON venularity.
# see http://www.atmarkit.co.jp/fcoding/articl
es/webapp/05/webapp05a.html
# Copy from Amon2::Plugin::Web::JSON => Fixed to escape only string parts
my %_ESCAPE = (
'+' => '\\u002b', # do not eval as UTF-7
'<' => '\\u003c', # do not eval a
my $body = shift;
$body =~ s!([+<>])!$_ESCAPE{$1}!g;
return qq("$body");
}
sub escape_json {
my $self = shift;
my $body = shift;
# escape only string parts
$body =~ s/"((?:
ON = '0.009';
}
# ABSTRACT: Manage configuration for a given endpoint
use Moo;
use JSON qw(decode_json encode_json);
use File::HomeDir;
use File::Path 2.08 qw(make_path);
has endpoint => (
is =
'HASH'; },
);
sub _build_config {
my $self = shift;
my $config_file = $self->file('config.json');
if (! -e $config_file ) {
warn "creating new config file: $config_file\n";
ode_json({});
close $fh;
}
my $config;
eval {
local $/;
open( my $fh, '<', $config_file ) or die "Unable to open $config_file for reading: $!";
my $json_te