| 1 | ############################################################################## | ||
| 2 | # | ||
| 3 | # Data::Stacker is concise text serialization for hash/array nested structs. | ||
| 4 | # Copyright (c) 2016-2022 Vladi Belperchinov-Shabanski "Cade" | ||
| 5 | # <cade@noxrun.com> <cade@bis.bg> <cade@cpan.org> | ||
| 6 | # http://cade.noxrun.com | ||
| 7 | # | ||
| 8 | # GPL | ||
| 9 | # | ||
| 10 | ############################################################################## | ||
| 11 | package Data::Stacker; | ||
| 12 | use strict; | ||
| 13 | use Exporter; | ||
| 14 | use Scalar::Util; | ||
| 15 | use Encode qw( is_utf8 encode decode ); | ||
| 16 | our $VERSION = '1.03'; | ||
| 17 | |||
| 18 | our @ISA = qw( Exporter ); | ||
| 19 | our @EXPORT = qw( | ||
| 20 | |||
| 21 | stack_data | ||
| 22 | unstack_data | ||
| 23 | |||
| 24 | ); | ||
| 25 | |||
| 26 | our %EXPORT_TAGS = ( | ||
| 27 | |||
| 28 | 'all' => \@EXPORT, | ||
| 29 | 'none' => [], | ||
| 30 | |||
| 31 | ); | ||
| 32 | |||
| 33 | |||
| 34 | ### STACK #################################################################### | ||
| 35 | |||
| 36 | # NOTE: escaping/unescaping is intentionally left in-place | ||
| 37 | |||
| 38 | sub stack_data | ||
| 39 | { | ||
| 40 | my $data = shift; | ||
| 41 | |||
| 42 | my $ref = ref $data; | ||
| 43 | if( $ref eq 'HASH' ) | ||
| 44 | { | ||
| 45 | return __stack_hashref( $data ); | ||
| 46 | } | ||
| 47 | elsif( $ref eq 'ARRAY' ) | ||
| 48 | { | ||
| 49 | return __stack_arrayref( $data ); | ||
| 50 | } | ||
| 51 | else | ||
| 52 | { | ||
| 53 | die "unsupported ref type, expected HASH or ARRAY reference, got [$data]"; | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | sub __stack_hashref | ||
| 58 | { | ||
| 59 | my $hr = shift; | ||
| 60 | |||
| 61 | my $str = ""; | ||
| 62 | my $ec = 0; | ||
| 63 | while( my ( $k, $v ) = each %$hr ) | ||
| 64 | { | ||
| 65 | $str .= __utf8_val_encode( $k ); | ||
| 66 | $ec++; | ||
| 67 | my $ref = Scalar::Util::reftype( $v ); | ||
| 68 | if( $ref eq 'HASH' ) | ||
| 69 | { | ||
| 70 | $str .= __stack_hashref( $v ); | ||
| 71 | } | ||
| 72 | elsif( $ref eq 'ARRAY' ) | ||
| 73 | { | ||
| 74 | $str .= __stack_arrayref( $v ); | ||
| 75 | } | ||
| 76 | elsif( $ref eq '' ) | ||
| 77 | { | ||
| 78 | $str .= __utf8_val_encode( $v ); | ||
| 79 | } | ||
| 80 | else | ||
| 81 | { | ||
| 82 | die "unsupported [$ref]"; | ||
| 83 | } | ||
| 84 | } | ||
| 85 | return "%$ec\n" . $str; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub __stack_arrayref | ||
| 89 | { | ||
| 90 | my $ar = shift; | ||
| 91 | |||
| 92 | my $str = ""; | ||
| 93 | my $ec = 0; | ||
| 94 | for my $v ( @$ar ) | ||
| 95 | { | ||
| 96 | $ec++; | ||
| 97 | my $ref = ref $v; | ||
| 98 | if( $ref eq 'HASH' ) | ||
| 99 | { | ||
| 100 | $str .= __stack_hashref( $v ); | ||
| 101 | } | ||
| 102 | elsif( $ref eq 'ARRAY' ) | ||
| 103 | { | ||
| 104 | $str .= __stack_arrayref( $v ); | ||
| 105 | } | ||
| 106 | elsif( $ref eq '' ) | ||
| 107 | { | ||
| 108 | $str .= __utf8_val_encode( $v ); | ||
| 109 | } | ||
| 110 | else | ||
| 111 | { | ||
| 112 | die "unsupported ref type [$ref]"; | ||
| 113 | } | ||
| 114 | } | ||
| 115 | return "\@$ec\n" . $str; | ||
| 116 | } | ||
| 117 | |||
| 118 | ### UNSTACK ################################################################## | ||
| 119 | |||
| 120 | sub unstack_data | ||
| 121 | { | ||
| 122 | my $str = shift; | ||
| 123 | |||
| 124 | my @str = split /\n/, $str; | ||
| 125 | chomp( @str ); | ||
| 126 | |||
| 127 | my ( $res_hr ) = __unstack_data_decode( \@str ); | ||
| 128 | |||
| 129 | return $res_hr; | ||
| 130 | } | ||
| 131 | |||
| 132 | sub __unstack_data_decode | ||
| 133 | { | ||
| 134 | my $data = shift; | ||
| 135 | my $pos = shift; | ||
| 136 | |||
| 137 | while( $pos <= @$data ) | ||
| 138 | { | ||
| 139 | my $line = $data->[ $pos ]; | ||
| 140 | |||
| 141 | # print "pos $pos [$line]\n"; | ||
| 142 | |||
| 143 | if( $line =~ /^\@(\d+)/ ) | ||
| 144 | { | ||
| 145 | my $count = $1; | ||
| 146 | return __unstack_data_decode_array( $data, $pos + 1, $count ); | ||
| 147 | } | ||
| 148 | elsif( $line =~ /^\%(\d+)/ ) | ||
| 149 | { | ||
| 150 | my $count = $1; | ||
| 151 | return __unstack_data_decode_hash( $data, $pos + 1, $count ); | ||
| 152 | } | ||
| 153 | elsif( $line =~ /^[=-]/ ) | ||
| 154 | { | ||
| 155 | return ( __utf8_val_decode( $line ), $pos + 1 ); | ||
| 156 | } | ||
| 157 | else | ||
| 158 | { | ||
| 159 | die "unsupported struct type or other error [$line]"; | ||
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 163 | } | ||
| 164 | |||
| 165 | sub __unstack_data_decode_array | ||
| 166 | { | ||
| 167 | my $data = shift; | ||
| 168 | my $pos = shift; | ||
| 169 | my $count = shift; | ||
| 170 | # print "starting array decode pos $pos count $count line [$data->[$pos]]\n"; | ||
| 171 | |||
| 172 | my @res; | ||
| 173 | while( $pos <= @$data and $count-- ) | ||
| 174 | { | ||
| 175 | my $v; | ||
| 176 | ( $v, $pos ) = __unstack_data_decode( $data, $pos ); | ||
| 177 | push @res, $v; | ||
| 178 | # print "adding array item [$v] at new pos $pos count $count\n"; | ||
| 179 | } | ||
| 180 | |||
| 181 | return ( \@res, $pos ); | ||
| 182 | } | ||
| 183 | |||
| 184 | sub __unstack_data_decode_hash | ||
| 185 | { | ||
| 186 | my $data = shift; | ||
| 187 | my $pos = shift; | ||
| 188 | my $count = shift; | ||
| 189 | # print "starting hash decode pos $pos count $count line [$data->[$pos]]\n"; | ||
| 190 | |||
| 191 | my %res; | ||
| 192 | while( $pos <= @$data and $count-- ) | ||
| 193 | { | ||
| 194 | my $k = __utf8_val_decode( $data->[ $pos ] ); | ||
| 195 | my $v; | ||
| 196 | $pos++; | ||
| 197 | ( $v, $pos ) = __unstack_data_decode( $data, $pos ); | ||
| 198 | $res{ $k } = $v; | ||
| 199 | # print "adding hash item [$k]=[$v] at new pos $pos count $count\n"; | ||
| 200 | } | ||
| 201 | |||
| 202 | return ( \%res, $pos ); | ||
| 203 | } | ||
| 204 | |||
| 205 | ### UTILS #################################################################### | ||
| 206 | |||
| 207 | sub __utf8_val_encode | ||
| 208 | { | ||
| 209 | my $v = shift; | ||
| 210 | $v =~ s/([\\\n])/sprintf("%%%02X",ord($1))/geo; | ||
| 211 | if( is_utf8( $v ) ) | ||
| 212 | { | ||
| 213 | $v = encode( 'UTF-8', $v ); | ||
| 214 | return "-$v\n"; | ||
| 215 | } | ||
| 216 | else | ||
| 217 | { | ||
| 218 | return "=$v\n"; | ||
| 219 | } | ||
| 220 | } | ||
| 221 | |||
| 222 | sub __utf8_val_decode | ||
| 223 | { | ||
| 224 | my $v = shift; | ||
| 225 | if( $v =~ /^-/ ) | ||
| 226 | { | ||
| 227 | $v = decode( 'UTF-8', $v ); | ||
| 228 | } | ||
| 229 | $v =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/geo; | ||
| 230 | return substr( $v, 1 ); | ||
| 231 | } | ||
| 232 | |||
| 233 | ############################################################################## | ||
| 234 | |||
| 235 | =pod | ||
| 236 | |||
| 237 | |||
| 238 | =head1 NAME | ||
| 239 | |||
| 240 | Data::Stacker provides compact text serialization for nested hash/array structs. | ||
| 241 | |||
| 242 | =head1 SYNOPSIS | ||
| 243 | |||
| 244 | use Data::Stacker qw( :all ); # import all functions | ||
| 245 | use Data::Stacker; # the same as :all :) | ||
| 246 | use Data::Stacker qw( :none ); # do not import anything, use full package names | ||
| 247 | |||
| 248 | # -------------------------------------------------------------------------- | ||
| 249 | |||
| 250 | my $str = stack_data( $hash_ref ); | ||
| 251 | my $str = stack_data( $array_ref ); | ||
| 252 | |||
| 253 | my $struct_ref = unstack_data( $str ); | ||
| 254 | |||
| 255 | =head1 FUNCTIONS | ||
| 256 | |||
| 257 | =head2 stack_data( $struct_ref ) | ||
| 258 | |||
| 259 | Serializes hash or array reference ($struct_ref) including all nested data | ||
| 260 | into text. Result text is compact but still human readable. | ||
| 261 | |||
| 262 | =head2 unstack_data( $str_text ) | ||
| 263 | |||
| 264 | Deserializes text data to perl structure (nested hash/array structs). | ||
| 265 | |||
| 266 | =head1 NOTES | ||
| 267 | |||
| 268 | Data::Stacker supports nested structures which include only ref types of | ||
| 269 | HASH, ARRAY and SCALAR. | ||
| 270 | |||
| 271 | Data::Stacker does not need to know values' types. It recognizes them only as | ||
| 272 | text string. | ||
| 273 | |||
| 274 | =head1 SERIALIZED TEXT FORMAT | ||
| 275 | |||
| 276 | The output serialized data was designed to be as short as possible but still | ||
| 277 | human readable (i.e. text). Another goal was that it have to be easily readable | ||
| 278 | by other programs with few parsing checks and in single pass. | ||
| 279 | |||
| 280 | Example source data structure: | ||
| 281 | |||
| 282 | $hr = { | ||
| 283 | 'TESTER2' => { | ||
| 284 | 'RANDOM' => { | ||
| 285 | 'FIELDS' => [ 'CTIME', 'SIZE' ], | ||
| 286 | 'UNIQUE' => 1, | ||
| 287 | }, | ||
| 288 | 'KEY' => { | ||
| 289 | 'FIELDS' => [ 'DES', 'FUNC' ], | ||
| 290 | 'NAME' => 'TEST2', | ||
| 291 | '_ORDER' => 5 | ||
| 292 | } | ||
| 293 | } | ||
| 294 | }; | ||
| 295 | |||
| 296 | Example output text: | ||
| 297 | |||
| 298 | %1 | ||
| 299 | TESTER2 | ||
| 300 | %2 | ||
| 301 | KEY | ||
| 302 | %3 | ||
| 303 | NAME | ||
| 304 | =TEST2 | ||
| 305 | _ORDER | ||
| 306 | =5 | ||
| 307 | FIELDS | ||
| 308 | @2 | ||
| 309 | =DES | ||
| 310 | =FUNC | ||
| 311 | RANDOM | ||
| 312 | %2 | ||
| 313 | UNIQUE | ||
| 314 | =1 | ||
| 315 | FIELDS | ||
| 316 | @2 | ||
| 317 | =CTIME | ||
| 318 | =SIZE | ||
| 319 | |||
| 320 | |||
| 321 | Serialized data represents stacked tree traversal data. Each line can be one | ||
| 322 | of: | ||
| 323 | |||
| 324 | =over 4 | ||
| 325 | |||
| 326 | =item "BEGIN HASH" \%[0-9]+ | ||
| 327 | |||
| 328 | It starts with char '%' followed by key+value pairs count. Each key and value | ||
| 329 | are printed on separated line. | ||
| 330 | |||
| 331 | =item "BEGIN ARRAY" \@[0-9]+ | ||
| 332 | |||
| 333 | It starts with char '@' followed by array entries values count. | ||
| 334 | |||
| 335 | =item "BEGIN DATA" \=.+ | ||
| 336 | |||
| 337 | It represents single line, single string value. It can be either hash key | ||
| 338 | value or array element value. | ||
| 339 | |||
| 340 | NOTE: there is special begin key '-', which represents UTF8 string. It is | ||
| 341 | needed for perl utf8 scalars, so Stacker can encode/decode them properly. | ||
| 342 | |||
| 343 | =item "HASH KEY" .+ | ||
| 344 | |||
| 345 | Hash keys are special case. Their position and purpose is clear, so they do | ||
| 346 | not need designated type chars (as %, @ or =). However, to support properly | ||
| 347 | UTF8 keys as perl utf8 scalars, keys also need '=' (for non-utf8 keys) and | ||
| 348 | '-' for utf8 scalar keys. | ||
| 349 | |||
| 350 | =back | ||
| 351 | |||
| 352 | "BEGIN HASH" and "BEGIN ARRAY" can be found anywhere where "BEGIN DATA" is | ||
| 353 | expected. | ||
| 354 | |||
| 355 | Serialized data is expected to start with any of "BEGIN HASH", "BEGIN ARRAY" | ||
| 356 | or "BEGIN DATA". Starting with "BEGIN DATA" is a special case where output | ||
| 357 | perl structure will hold single scalar reference. | ||
| 358 | |||
| 359 | URL-style (%XX where XX is hex ascii code) is used for escaping of special | ||
| 360 | characters in key names and data values. The only chars that need escaping | ||
| 361 | are the new-line/LF (%0A) char and % (%5C). Unescaping is performed for all | ||
| 362 | found escaped chars (not only for LF and %). | ||
| 363 | |||
| 364 | No comments (neither line nor trailing) are allowed. If added manually, will | ||
| 365 | be either accepted as key name or value data or will break decoding. | ||
| 366 | |||
| 367 | Example source data structure with comments: | ||
| 368 | |||
| 369 | # ( 1) hash A (1 key) | ||
| 370 | $hr = { | ||
| 371 | # ( 2) hash A, key #1 | ||
| 372 | 'TESTER2' => | ||
| 373 | # ( 3) hash A, value #1 == hash B (1 key) | ||
| 374 | { | ||
| 375 | # ( 4) hash B, key #1 | ||
| 376 | 'RANDOM' => | ||
| 377 | # ( 5) hash B, value #1 == hash C (2 keys) | ||
| 378 | { | ||
| 379 | # ( 6) hash C, key #1 | ||
| 380 | 'FIELDS' => | ||
| 381 | # ( 7) hash C, value #1 == array D | ||
| 382 | [ 'CTIME', 'SIZE' ], | ||
| 383 | # ( 8) hash C, key #2 + value #2 == data "1" | ||
| 384 | 'UNIQUE' => 1, | ||
| 385 | }, | ||
| 386 | # ( 9) hash B, key #2 | ||
| 387 | 'KEY' => | ||
| 388 | # (10) hash B, value #2 == hash E (3 keys) | ||
| 389 | { | ||
| 390 | # (11) hash E, key #1 | ||
| 391 | 'FIELDS' => | ||
| 392 | # (12) hash E, value #1 == array F | ||
| 393 | [ 'DES', 'FUNC' ], | ||
| 394 | # (13) hash E, key #2 + value #2 == data "TEST2" | ||
| 395 | 'NAME' => 'TEST2', | ||
| 396 | # (14) hash E, key #3 + value #3 == data "5" | ||
| 397 | '_ORDER' => 5 | ||
| 398 | } | ||
| 399 | } | ||
| 400 | }; | ||
| 401 | |||
| 402 | Note that order of key+value pairs in hashes is as reported by the language | ||
| 403 | (i.e. random). | ||
| 404 | |||
| 405 | Serialized output data with comments: | ||
| 406 | (as noted, comments here are invalid! only used as | ||
| 407 | |||
| 408 | %1 # ( 1) hash A (1 key) | ||
| 409 | TESTER2 # ( 2) hash A, key #1 | ||
| 410 | %2 # ( 3) hash A, value #1 == hash B (1 key) | ||
| 411 | KEY # ( 9) hash B, key #2 | ||
| 412 | %3 # (10) hash B, value #2 == hash E (3 keys) | ||
| 413 | NAME # (13) hash E, key #2 | ||
| 414 | =TEST2 # (13) hash E, value #2 == data "TEST2" | ||
| 415 | _ORDER # (14) hash E, key #3 | ||
| 416 | =5 # (14) hash E, value #3 == data "5" | ||
| 417 | FIELDS # (11) hash E, key #1 | ||
| 418 | @2 # (12) hash E, value #1 == array F (2 elements) | ||
| 419 | =DES # (12) array F, element #2 == data "DES" | ||
| 420 | =FUNC # (12) array F, element #2 == data "DES" | ||
| 421 | RANDOM # ( 4) hash B, key #1 | ||
| 422 | %2 # ( 5) hash B, value #1 == hash C (2 keys) | ||
| 423 | UNIQUE # ( 8) hash C, key #2 | ||
| 424 | =1 # ( 8) hash C, value #2 == data "1" | ||
| 425 | FIELDS # (11) hash E, key #1 | ||
| 426 | @2 # ( 7) hash C, value #1 == array D | ||
| 427 | =CTIME # ( 7) array D, element #1 == data "CTIME" | ||
| 428 | =SIZE # ( 7) array D, element #1 == data "SIZE" | ||
| 429 | |||
| 430 | =head1 TODO | ||
| 431 | |||
| 432 | * Objects | ||
| 433 | * Ordered hashes (i.e. Objects support for Tie::IxHash etc.) | ||
| 434 | * Circular structures | ||
| 435 | |||
| 436 | =head1 KNOWN BUGS | ||
| 437 | |||
| 438 | Escaping probably will not work with all unicode new-line chars or when | ||
| 439 | reading from file with different record separator. | ||
| 440 | |||
| 441 | Will not work with circular (self-referred) structures. | ||
| 442 | |||
| 443 | =head1 SEE ALSO | ||
| 444 | |||
| 445 | Few similar-task perl modules: | ||
| 446 | |||
| 447 | * Storable | ||
| 448 | * Sereal | ||
| 449 | * Data::MessagePack | ||
| 450 | * JSON | ||
| 451 | |||
| 452 | =head1 GITHUB REPOSITORY | ||
| 453 | |||
| 454 | git@github.com:cade-vs/perl-data-stacker.git | ||
| 455 | |||
| 456 | git clone git://github.com/cade-vs/perl-data-stacker.git | ||
| 457 | |||
| 458 | =head1 AUTHOR | ||
| 459 | |||
| 460 | Vladi Belperchinov-Shabanski "Cade" | ||
| 461 | |||
| 462 | <cade@noxrun.com> <cade@bis.bg> <cade@cpan.org> | ||
| 463 | http://cade.noxrun.com | ||
| 464 | |||
| 465 | =cut | ||
| 466 | |||
| 467 | ############################################################################## | ||
| 468 | 1; |