Changeset 37184


Ignore:
Timestamp:
2023-01-25T23:15:40+13:00 (43 hours ago)
Author:
davidb
Message:

Further refinement of idea, with emphasis on using plugins arguments rather than having things hardwired

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone2/perllib/plugins/SplitJSONFile.pm

    r37182 r37184  
    5757      'deft' => "",
    5858      'reqd' => "no" },
    59     { 'name' => "metadata_exp",
     59    { 'name' => "file_exp",
    6060      'desc' => "{SplitJSONFile.file_exp}",
    6161      'type' => "string",
     
    105105
    106106    my $json = JSON->new();
     107    my $json_text_content = JSON::from_json($$textref);
     108
     109    my $json_array = [ ];
    107110   
    108111    # Split the text into several smaller segments
    109112    my $split_exp = $self->{'split_exp'};
    110113
     114    my @split_exps = split(/\s*,\s*/,$split_exp);
     115   
     116    if (scalar(@split_exps) == 0) {
     117    @split_exps = ("");
     118    }
     119   
     120    foreach my $full_json_field (@split_exps) {
    111121
    112     my $json_text_content = JSON::from_json($$textref);
    113    
    114     my $json_array1 = $json_text_content->{'contentGroups'};
    115     my $json_array2 = $json_text_content->{'contentItems'};
     122    $full_json_field =~ s/^\.?//;
     123   
     124    my @json_field_parts = ($full_json_field eq "") ? () : split(/\./,$full_json_field);
    116125
    117     # my $json_array = [ @$json_array1, @$json_array2 ];
     126    ## print STDERR "**** json_field_parts = ", JSON::to_json(\@json_field_parts), "\n";
     127    my $json_cursor = $json_text_content;
    118128
    119     my $json_array = [  @$json_array2 ];
    120    
    121     if ($verbosity>1) {
     129    my $had_error = 0;
     130   
     131    foreach my $json_field_part (@json_field_parts) {
     132        $json_cursor = $json_cursor->{$json_field_part};
     133        if (!defined $json_cursor) {
     134        print STDERR "Warning: failed to find '$full_json_field' in JSON file.  '$json_field_part' did not exist\n";
     135        print STDERR "-> Skipping\n";
     136        $had_error = 1;
     137        last;
     138        }
     139    }
     140
     141    if (!$had_error) {
     142       
     143        if (ref($json_cursor) eq "ARRAY") {
     144        push(@$json_array,@$json_cursor);
     145        }
     146        else {
     147        if ($full_json_field eq "") {
     148            print STDERR "Warning: The top-level of the JSON file is not an array field\n";
     149        }
     150        else {
     151            print STDERR "Warning: The specified field '$full_json_field' was detected in the JSON file, however it is not an array field\n";
     152        }
     153        print STDERR "-> Skipping\n";
     154        }
     155    }
     156    }
     157
     158    if ($verbosity>=3) {
    122159    print $outhandle "----------\n";
    123160    print $outhandle "SplitJSONFile -- Segments\n";
     
    133170    my $seg_json_unicode_str = $json->encode($seg_json_rec); # expects unicode string
    134171
    135     if ($verbosity>1) {
     172    if ($verbosity>=3) {
    136173        my $seg_json_utf8_printable_str = Encode::encode("utf8",$seg_json_unicode_str);
    137174
Note: See TracChangeset for help on using the changeset viewer.