##-*- Mode: CPerl -*-
## File: DDC::Format::JSON.pm
## Author: Bryan Jurish <moocow@cpan.org>
## Description:
## + DDC Query utilities: output formatting: Data::Dumper
##============================
==========================================
package DDC::Format::JSON;
use JSON;
use Carp;
use strict;
##======================================================================
## Globals
our @ISA = q
##-- json prettification level (default:0)
## jxs => $jxs, ##-- underlying JSON object
## )
sub new {
my $that = shift;
my $fmt = bless {
level=>0,
jxs =>JSON->new->u
====================================================================
package DDC::PP::Object;
use JSON;
use Carp qw(carp confess);
use strict;
##======================================================
ng {
return "$_[0]";
}
sub toJson {
return JSON::to_json( $_[0], {utf8=>1,pretty=>0,canonical=>1,allow_blessed=>1,convert_blessed=>1} );
}
##-- json utils
sub jsonClass {
(my $class = ref($_[0
pts:
## (
## trimClassNames => $bool, ##-- auto-trim class-names?
## json => $bool, ##-- for JSON-ification?
## )
## + returns an object as a (nested) perl hash
## + pure-p
|[]});
my ($w);
foreach (grep {defined $_} @{$hit->{ctx_}||[]}) { ##-- expand deep-encoded json context tokens if available (ddc >= v2.0.38)
foreach (grep {UNIVERSAL::isa($_,'ARRAY')} @$_)
>$w->[$_])} (0..$#$w) };
}
}
return $hit;
}
## $thingy = $obj->TO_JSON()
## + annoying wrapper for JSON
sub TO_JSON {
return { %{$_[0]} };
}
1; ##-- be happy
__END__
##================
Dumper formatting of L<DDC::HitList|DDC::HitList> objects.
=item L<DDC::Format::JSON|DDC::Format::JSON>
Class for JSON formatting of L<DDC::HitList|DDC::HitList> objects.
=item L<DDC::Format::Kwic|
foreach (@{$hl->{hits_}||[]});
return $hl;
}
## $thingy = $obj->TO_JSON()
## + annoying wrapper for JSON, JSON::XS
sub TO_JSON {
return { %{$_[0]} };
}
1; ##-- be happy
__END__
##===========
-conversion (for toJson())
sub toHash {
my ($obj,%opts) = @_;
return $obj->SUPER::toHash(%opts) if (!$opts{json});
return { class=>$obj->jsonClass, $obj->jsonData };
}
sub jsonData {
my $qo =
()),
($qo->{DebugRank} ? (DebugRank=>$qo->{DebugRank}) : qw()),
(Filters=>[map {($_->toHash(json=>1))} @{$qo->{Filters}||[]}]),
($qo->{Subcorpora} && @{$qo->{Subcorpora}} ? (Subcorpora=>$qo->
sub jsonType { return (FilterType=>$DDC::PP::HitSortEnumStrings[$_[0]{Type}]); }
sub jsonMinMax { return (Min=>$_[0]{Arg1}, Max=>$_[0]{Arg2}); }
sub jsonData { return ($_[0]->jsonType, $_[0]->jsonMinM
(for toJson())
sub toHash {
my ($obj,%opts) = @_;
return $obj->SUPER::toHash(%opts) if (!$opts{json});
return { class=>$obj->jsonClass, $obj->jsonData };
}
##-- pseudo-accessors (for json)
__PA
rt;
use strict;
our @ISA = qw(DDC::PP::CQFSort);
sub defaultSort { return 'GreaterByRank'; }
sub jsonMinMax { return qw(); }
##======================================================================
ness of server
our $ilen = 4;
## $JSON_BACKEND
## + underlying JSON module (default='JSON')
our ($JSON_BACKEND);
BEGIN {
$JSON_BACKEND = 'JSON' if (!defined($JSON_BACKEND));
}
##==================
t=>$connectURL
## mode =>$queryMode, ##-- one of 'table', 'html', 'text', 'json', or 'raw'; default='json' ('html' is not yet supported)
## linger =>\@linger, ##-- SO_LINGER socket
y $dc =bless {
##-- connection options
connect=> $connect,
linger => [1,0],
mode =>'json',
encoding => 'UTF-8',
##-- query options (formerly in DDC::Client::Distributed)
start=