| 1 | ## -*- Mode: CPerl; coding: utf-8 -*- | ||
| 2 | ## | ||
| 3 | ## File: DTA::CAB::Format::JSON.pm | ||
| 4 | ## Author: Bryan Jurish <moocow@cpan.org> | ||
| 5 | ## Description: Datum parser|formatter: YML code (generic) | ||
| 6 | |||
| 7 | package DTA::CAB::Format::JSON; | ||
| 8 | use DTA::CAB::Format; | ||
| 9 | use DTA::CAB::Datum ':all'; | ||
| 10 | use IO::File; | ||
| 11 | use JSON::XS; | ||
| 12 | use Carp; | ||
| 13 | use strict; | ||
| 14 | |||
| 15 | ##============================================================================== | ||
| 16 | ## Globals | ||
| 17 | ##============================================================================== | ||
| 18 | |||
| 19 | our @ISA = qw(DTA::CAB::Format); | ||
| 20 | |||
| 21 | BEGIN { | ||
| 22 | DTA::CAB::Format->registerFormat(name=>__PACKAGE__, short=>'json-xs', filenameRegex=>qr/\.(?i:json(?:[\.\-\_]xs)?)$/); | ||
| 23 | DTA::CAB::Format->registerFormat(name=>__PACKAGE__, short=>'json'); | ||
| 24 | } | ||
| 25 | |||
| 26 | ##============================================================================== | ||
| 27 | ## Constructors etc. | ||
| 28 | ##============================================================================== | ||
| 29 | |||
| 30 | ## $fmt = CLASS_OR_OBJ->new(%args) | ||
| 31 | ## + object structure: assumed HASH | ||
| 32 | ## ( | ||
| 33 | ## ##---- Input | ||
| 34 | ## doc => $doc, ##-- buffered input document | ||
| 35 | ## raw => $bool, ##-- if true, format parses raw data | ||
| 36 | ## | ||
| 37 | ## ##---- INHERITED from DTA::CAB::Format | ||
| 38 | ## #utf8 => $bool, ##-- always true | ||
| 39 | ## level => $formatLevel, ##-- 0:compressed, 1:formatted, ... | ||
| 40 | ## #outbuf => $stringBuffer, ##-- buffered output | ||
| 41 | ## ) | ||
| 42 | sub new { | ||
| 43 | my $that = shift; | ||
| 44 | return $that->SUPER::new( | ||
| 45 | ##-- I/O common | ||
| 46 | raw => 0, | ||
| 47 | |||
| 48 | ##-- Input | ||
| 49 | #doc => undef, | ||
| 50 | |||
| 51 | ##-- Output | ||
| 52 | level => 0, | ||
| 53 | #outbuf => '', | ||
| 54 | |||
| 55 | ##-- common | ||
| 56 | utf8 => 1, | ||
| 57 | jxs => undef, ##-- see jsonxs() method, below | ||
| 58 | |||
| 59 | ##-- user args | ||
| 60 | @_ | ||
| 61 | ); | ||
| 62 | } | ||
| 63 | |||
| 64 | ## $jxs = $CLASS_OR_OBJECT->jsonxs() | ||
| 65 | ## + returns a (new) JSON::XS object | ||
| 66 | ## + returns $obj->{jxs} if defined | ||
| 67 | ## + otherwise caches $obj->{jxs} as new JSON::XS object | ||
| 68 | sub jsonxs { | ||
| 69 | return $_[0]{jxs} if (ref($_[0]) && defined($_[0]{jxs})); | ||
| 70 | my $jxs = JSON::XS->new->utf8(0)->relaxed(1)->canonical(0)->allow_blessed(1)->convert_blessed(1)->allow_nonref(1); | ||
| 71 | $_[0]{jxs} = $jxs if (ref($_[0])); | ||
| 72 | return $jxs; | ||
| 73 | } | ||
| 74 | |||
| 75 | ##============================================================================== | ||
| 76 | ## Methods: Persistence | ||
| 77 | ##============================================================================== | ||
| 78 | |||
| 79 | ## @keys = $class_or_obj->noSaveKeys() | ||
| 80 | ## + returns list of keys not to be saved | ||
| 81 | sub noSaveKeys { | ||
| 82 | return ($_[0]->SUPER::noSaveKeys, qw(doc outbuf jxs)); | ||
| 83 | } | ||
| 84 | |||
| 85 | ##============================================================================== | ||
| 86 | ## Methods: I/O: generic | ||
| 87 | ##============================================================================== | ||
| 88 | |||
| 89 | ## @layers = $fmt->iolayers() | ||
| 90 | ## + override returns only ':raw' | ||
| 91 | sub iolayers { | ||
| 92 | return qw(:raw); | ||
| 93 | } | ||
| 94 | |||
| 95 | ##============================================================================== | ||
| 96 | ## Methods: Input | ||
| 97 | ##============================================================================== | ||
| 98 | |||
| 99 | ##-------------------------------------------------------------- | ||
| 100 | ## Methods: Input: Input selection | ||
| 101 | |||
| 102 | ## $fmt = $fmt->fromFh($fh) | ||
| 103 | ## + override calls $fmt->fromFh_str | ||
| 104 | sub fromFh { | ||
| 105 | return $_[0]->fromFh_str(@_[1..$#_]); | ||
| 106 | } | ||
| 107 | |||
| 108 | ## $fmt = $fmt->fromString(\$string) | ||
| 109 | sub fromString { | ||
| 110 | my $fmt = shift; | ||
| 111 | $fmt->close(); | ||
| 112 | return $fmt->parseJsonString(ref($_[0]) ? $_[0] : \$_[0]); | ||
| 113 | } | ||
| 114 | |||
| 115 | ##-------------------------------------------------------------- | ||
| 116 | ## Methods: Input: Local | ||
| 117 | |||
| 118 | ## $fmt = $fmt->parseJsonString(\$str) | ||
| 119 | ## + must be defined by child classes! | ||
| 120 | ## + if $rawMode is true, no document massaging will be performed | ||
| 121 | sub parseJsonString { | ||
| 122 | my $fmt = shift; | ||
| 123 | my $doc = $fmt->jsonxs->utf8( utf8::is_utf8(${$_[0]}) ? 0 : 1 )->decode(${$_[0]}) | ||
| 124 | or $fmt->logcluck("parseJsonString(): JSON::XS::decode() failed: $!"); | ||
| 125 | $fmt->{doc} = $fmt->{raw} ? $doc : $fmt->forceDocument($doc); | ||
| 126 | return $fmt; | ||
| 127 | } | ||
| 128 | |||
| 129 | ##-------------------------------------------------------------- | ||
| 130 | ## Methods: Input: Generic API | ||
| 131 | |||
| 132 | ## $doc = $fmt->parseDocument() | ||
| 133 | sub parseDocument { return $_[0]{doc}; } | ||
| 134 | |||
| 135 | ##============================================================================== | ||
| 136 | ## Methods: Output | ||
| 137 | ##============================================================================== | ||
| 138 | |||
| 139 | ##-------------------------------------------------------------- | ||
| 140 | ## Methods: Output: Generic | ||
| 141 | |||
| 142 | ## $type = $fmt->mimeType() | ||
| 143 | ## + override | ||
| 144 | sub mimeType { return 'application/json'; } | ||
| 145 | |||
| 146 | ## $ext = $fmt->defaultExtension() | ||
| 147 | ## + returns default filename extension for this format | ||
| 148 | sub defaultExtension { return '.json'; } | ||
| 149 | |||
| 150 | ## $short = $fmt->formatName() | ||
| 151 | ## + returns "official" short name for this format | ||
| 152 | ## + default just returns package suffix | ||
| 153 | sub shortName { | ||
| 154 | return 'json'; | ||
| 155 | } | ||
| 156 | |||
| 157 | |||
| 158 | ##-------------------------------------------------------------- | ||
| 159 | ## Methods: Output: output selection | ||
| 160 | |||
| 161 | ## $fmt_or_undef = $fmt->toFh($fh,$formatLevel) | ||
| 162 | ## + select output to filehandle $fh | ||
| 163 | sub toFh { | ||
| 164 | $_[0]->DTA::CAB::Format::toFh(@_[1..$#_]); | ||
| 165 | $_[0]->jsonxs->utf8((grep {$_ eq 'utf8'} PerlIO::get_layers($_[1])) ? 0 : 1)->pretty(($_[0]{level}||0)>0 ? 1 : 0); | ||
| 166 | return $_[0]; | ||
| 167 | } | ||
| 168 | |||
| 169 | ##-------------------------------------------------------------- | ||
| 170 | ## Methods: Output: Generic API | ||
| 171 | ## + these methods just dump raw json | ||
| 172 | ## + you're pretty much restricted to dumping a single document here | ||
| 173 | |||
| 174 | ## $fmt = $fmt->putRef($thingy) | ||
| 175 | ## + puts a raw thingy with $fmt->{jxs}->encode() | ||
| 176 | sub putRef { | ||
| 177 | $_[0]{fh}->print($_[0]{jxs}->encode($_[1])); | ||
| 178 | return $_[0]; | ||
| 179 | } | ||
| 180 | |||
| 181 | ## $fmt = $fmt->putDocument($doc) | ||
| 182 | sub putDocument { | ||
| 183 | my $fmt = shift; | ||
| 184 | my $doc = {%{$_[0]}}; | ||
| 185 | delete @$doc{grep {UNIVERSAL::isa($doc->{$_},'SCALAR') || UNIVERSAL::isa($doc->{$_},'CODE')} keys %$doc}; | ||
| 186 | return $fmt->putRef($doc); | ||
| 187 | } | ||
| 188 | |||
| 189 | ## $fmt = $fmt->putToken($tok) | ||
| 190 | ## $fmt = $fmt->putSentence($sent) | ||
| 191 | ## $fmt = $fmt->putDocument($doc) | ||
| 192 | ## $fmt = $fmt->putData($data) | ||
| 193 | BEGIN { | ||
| 194 | *putToken = \&putRef; | ||
| 195 | *putSentence = \&putRef; | ||
| 196 | *putData = \&putRef; | ||
| 197 | } | ||
| 198 | |||
| 199 | 1; ##-- be happy | ||
| 200 | |||
| 201 | __END__ |