| 1 | #/usr/bin/env perl | ||
| 2 | ############################################################################### | ||
| 3 | # | ||
| 4 | # @file Json.pm | ||
| 5 | # | ||
| 6 | # @brief Eulerian Data Warehouse REST Json Parser Module definition. | ||
| 7 | # | ||
| 8 | # @author Thorillon Xavier:x.thorillon@eulerian.com | ||
| 9 | # | ||
| 10 | # @date 26/11/2021 | ||
| 11 | # | ||
| 12 | # @version 1.0 | ||
| 13 | # | ||
| 14 | ############################################################################### | ||
| 15 | # | ||
| 16 | # Setup module name | ||
| 17 | # | ||
| 18 | package API::Eulerian::EDW::Parser::JSON; | ||
| 19 | # | ||
| 20 | # Enforce compilor rules | ||
| 21 | # | ||
| 22 | use strict; use warnings; | ||
| 23 | # | ||
| 24 | # Inherited interface from API::Eulerian::EDW::Parser | ||
| 25 | # | ||
| 26 | use parent 'API::Eulerian::EDW::Parser'; | ||
| 27 | # | ||
| 28 | # | ||
| 29 | # | ||
| 30 | use JSON::Streaming::Reader; | ||
| 31 | # | ||
| 32 | # | ||
| 33 | # | ||
| 34 | use FileHandle; | ||
| 35 | # | ||
| 36 | # @brief | ||
| 37 | # | ||
| 38 | # @param $class - API::Eulerian::EDW::Parser class. | ||
| 39 | # @param $path - File Path. | ||
| 40 | # @param $uuid - Request UUID. | ||
| 41 | # | ||
| 42 | # @return API::Eulerian::EDW::Json Parser. | ||
| 43 | # | ||
| 44 | sub new | ||
| 45 | { | ||
| 46 | my ( $class, $path, $uuid ) = @_; | ||
| 47 | my $self; | ||
| 48 | my $fd; | ||
| 49 | |||
| 50 | # Setup base class instance | ||
| 51 | $self = $class->SUPER::new( $path, $uuid ); | ||
| 52 | |||
| 53 | # Create a new FileHandle | ||
| 54 | $self->{ _FD } = $fd = FileHandle->new(); | ||
| 55 | |||
| 56 | # Open FileHandle | ||
| 57 | $fd->open( "< $path" ); | ||
| 58 | |||
| 59 | # Create Json parser | ||
| 60 | $self->{ _PARSER } = JSON::Streaming::Reader->for_stream( $fd ); | ||
| 61 | |||
| 62 | return $self; | ||
| 63 | } | ||
| 64 | # | ||
| 65 | # @brief | ||
| 66 | # | ||
| 67 | # @param $self | ||
| 68 | # | ||
| 69 | # @return | ||
| 70 | # | ||
| 71 | sub parser | ||
| 72 | { | ||
| 73 | return shift->{ _PARSER }; | ||
| 74 | } | ||
| 75 | # | ||
| 76 | # @brief | ||
| 77 | # | ||
| 78 | # @param $self - API::Eulerian::EDW::Parser | ||
| 79 | # | ||
| 80 | use Data::Dumper; | ||
| 81 | sub do | ||
| 82 | { | ||
| 83 | my ( $self, $hooks ) = @_; | ||
| 84 | my $parser = $self->parser(); | ||
| 85 | my $depth = -1; | ||
| 86 | my @in = (); | ||
| 87 | my $uuid; | ||
| 88 | my $msg; | ||
| 89 | my $key; | ||
| 90 | |||
| 91 | # Parse JSON stream | ||
| 92 | $parser->process_tokens( | ||
| 93 | # Property begin | ||
| 94 | start_property => sub | ||
| 95 | { | ||
| 96 | $key = shift; | ||
| 97 | }, | ||
| 98 | # Property end | ||
| 99 | end_property => sub | ||
| 100 | { | ||
| 101 | }, | ||
| 102 | # String | ||
| 103 | add_string => sub | ||
| 104 | { | ||
| 105 | my $parent = $in[ $depth ]; | ||
| 106 | if( ref( $parent ) eq 'ARRAY' ) { | ||
| 107 | $parent->[ scalar( @$parent ) ] = shift; | ||
| 108 | } elsif( ref( $parent ) eq 'HASH' ) { | ||
| 109 | $parent->{ $key } = shift; | ||
| 110 | } | ||
| 111 | }, | ||
| 112 | # Number | ||
| 113 | add_number => sub | ||
| 114 | { | ||
| 115 | my $parent = $in[ $depth ]; | ||
| 116 | if( ref( $parent ) eq 'ARRAY' ) { | ||
| 117 | $parent->[ scalar( @$parent ) ] = shift; | ||
| 118 | } elsif( ref( $parent ) eq 'HASH' ) { | ||
| 119 | $parent->{ $key } = shift; | ||
| 120 | } | ||
| 121 | }, | ||
| 122 | # Null | ||
| 123 | add_null => sub | ||
| 124 | { | ||
| 125 | my $parent = $in[ $depth ]; | ||
| 126 | if( ref( $parent ) eq 'ARRAY' ) { | ||
| 127 | $parent->[ scalar( @$parent ) ] = undef; | ||
| 128 | } elsif( ref( $parent ) eq 'HASH' ) { | ||
| 129 | $parent->{ $key } = undef; | ||
| 130 | } | ||
| 131 | }, | ||
| 132 | # Object begin | ||
| 133 | start_object => sub | ||
| 134 | { | ||
| 135 | $in[ ++$depth ] = {}; | ||
| 136 | }, | ||
| 137 | # Object end | ||
| 138 | end_object => sub | ||
| 139 | { | ||
| 140 | my $parent = $in[ $depth - 1 ] if $depth > 0; | ||
| 141 | if( ref( $parent ) eq 'ARRAY' ) { | ||
| 142 | $parent->[ scalar( @$parent ) ] = $in[ $depth ]; | ||
| 143 | } elsif( ref( $parent ) eq 'HASH' ) { | ||
| 144 | $parent->{ $key } = $in[ $depth ]; | ||
| 145 | } | ||
| 146 | if( $depth == 1 ) { | ||
| 147 | $msg = $in[ $depth ]; | ||
| 148 | #print( Dumper( $msg ) . "\n" ); die ""; | ||
| 149 | $uuid = $msg->{ uuid }; | ||
| 150 | print( "UUID : $uuid\n" ); | ||
| 151 | $hooks->on_headers( | ||
| 152 | $uuid, $msg->{ from }, $msg->{ to }, | ||
| 153 | $msg->{ schema } | ||
| 154 | ); | ||
| 155 | } | ||
| 156 | $depth--; | ||
| 157 | }, | ||
| 158 | # Array begin | ||
| 159 | start_array => sub | ||
| 160 | { | ||
| 161 | $in[ ++$depth ] = []; | ||
| 162 | }, | ||
| 163 | # Array end | ||
| 164 | end_array => sub | ||
| 165 | { | ||
| 166 | my $parent = $in[ $depth - 1 ] if $depth > 0; | ||
| 167 | if( ref( $parent ) eq 'ARRAY' ) { | ||
| 168 | $parent->[ scalar( @$parent ) ] = $in[ $depth ]; | ||
| 169 | } elsif( ref( $parent ) eq 'HASH' ) { | ||
| 170 | $parent->{ $key } = $in[ $depth ]; | ||
| 171 | } | ||
| 172 | if( $depth == 2 && $uuid ) { | ||
| 173 | $hooks->on_add( $uuid, [ $in[ $depth ] ] ); | ||
| 174 | } | ||
| 175 | $depth--; | ||
| 176 | }, | ||
| 177 | ); | ||
| 178 | $hooks->on_status( $uuid, '', 0, 'Success', 0 ); | ||
| 179 | |||
| 180 | } | ||
| 181 | # | ||
| 182 | # Endup module properly | ||
| 183 | # | ||
| 184 | 1; |