package Valiant::JSON::JSONBuilder;
use Moo;
use Carp;
use Scalar::Util 'blessed';
use Module::Runtime 'use_module';
use Cpanel::JSON::XS ();
use Valiant::HTML::Util::Collection;
use Valiant::Naming;
has 'json_args' => (
is => 'ro',
required => 1,
default => sub { +{} },
);
has 'json' => (
is => 'ro',
required => 1,
lazy => 1,
default => sub {
my %args = %{ shift->json_args
};
return Cpanel::JSON::XS->new(%args, utf8=>1, pretty=>1);
},
handles => {
encode_json => 'encode',
},
);
sub reset {
my ($self) = @_;
$self->data(+{ data => +{} });
$self->clear
package Valiant::JSON::Util;
use warnings;
use strict;
use Exporter 'import';
our @EXPORT_OK = qw(escape_javascript);
our %EXPORT_TAGS = (all => \@EXPORT_OK);
my %JS_ESCAPE_MAP = (
'\\' => '\\\
}
1;
=head1 NAME
Valiant::Util - Importable utility methods;
=head1 SYNOPSIS
use Valiant::JSON::Util 'escape_javascript';
=head1 DESCRIPTION
Just a place to stick various utility functio
HTML::Util::TagBuilder;
use Moo;
use Sub::Util;
use Carp;
use Scalar::Util;
use Cpanel::JSON::XS ('encode_json');
use overload
bool => sub {1},
'""' => sub { shift->to_string },
fallback => 1
;
my $value = defined($_[0]) ? shift() : '';
if(ref($value) eq 'HASH') {
$value = encode_json($value);
$value = $self->safe($value);
} else {
$value = $self->safe($value);
}
re
[] }
];
}
return %hash;
}
sub as_json {
my ($self, @args) = @_;
return $self->object->errors_as_json($self) if $self->object->can('errors_as_json');
return $self->as_rfc_7807;
}
sub as
rrors
$rfc{fields}->{$field} = $hash{$field};
}
}
return \%rfc;
}
sub TO_JSON { shift->as_json }
# Adds +message+ to the error messages and used validator type to +details+ on +attri
ing an error response compatible with
RFC-7807, an open standard for returning errors formated in JSON from web APIs.
Example response:
{
fields => {
attendees => [
"Atten
olumns;
return $columns{$attribute} if exists($columns{$attribute});
}
sub get_attribute_for_json {
my ($self, $attribute) = @_;
return $self->is_marked_for_deletion if $attribute eq '_delete
Module::Runtime;
use Valiant::HTML::SafeString;
use Valiant::HTML::Util::TagBuilder;
use Valiant::JSON::Util;
use Scalar::Util;
use Sub::Util;
use URI::Escape ();
use Carp;
extends 'Catalyst::View::B
eturn Valiant::HTML::SafeString::escape_html(@_) }
sub escape_javascript { shift; return Valiant::JSON::Util::escape_javascript(@_) }
sub escape_js { shift->escape_javascript(@_) }
sub uri_escape {
:SafeString> for more information on the safe string methods.
See L<Valiant::JSON::Util> for more information on the JSON methods.
=head1 SUBCLASSING
Create a base class in your project:
packa
atalyst::View::Valiant::JSONBuilder;
use Moo;
use Carp;
use Module::Runtime 'use_module';
extends 'Catalyst::View::BasePerRequest';
our $MODEL_BUILDER = 'Valiant::JSON::JSONBuilder';
has 'model_bu
b get_model_for_json {
my ($self, $name) = @_;
return $self->to_model if $self->can('to_model');
return $self->get_attribute_for_json($name) if $self->can('get_attribute_for_json');
return $se
lf->$name if $self->can($name);
croak "Can't find model for $name";
}
sub json_builder {
my ($self, $model) = @_;
return my $jb = use_module($self->model_builder)->new(view => $self, model=>$mo