API-Eulerian

 view release on metacpan or  search on metacpan

lib/API/Eulerian/EDW/Parser/JSON.pm  view on Meta::CPAN

#/usr/bin/env perl
###############################################################################
#
# @file Json.pm
#
# @brief Eulerian Data Warehouse REST Json Parser Module definition.
#
# @author Thorillon Xavier:x.thorillon@eulerian.com
#
# @date 26/11/2021
#
# @version 1.0
#
###############################################################################
#
# Setup module name
#
package API::Eulerian::EDW::Parser::JSON;
#
# Enforce compilor rules
#
use strict; use warnings;
#
# Inherited interface from API::Eulerian::EDW::Parser
#
use parent 'API::Eulerian::EDW::Parser';
#
#
#
use JSON::Streaming::Reader;
#
#
#
use FileHandle;
#
# @brief
#
# @param $class - API::Eulerian::EDW::Parser class.
# @param $path - File Path.
# @param $uuid - Request UUID.
#
# @return API::Eulerian::EDW::Json Parser.
#
sub new
{
  my ( $class, $path, $uuid ) = @_;
  my $self;
  my $fd;

  # Setup base class instance
  $self = $class->SUPER::new( $path, $uuid );

  # Create a new FileHandle
  $self->{ _FD } = $fd = FileHandle->new();

  # Open FileHandle
  $fd->open( "< $path" );

  # Create Json parser
  $self->{ _PARSER } = JSON::Streaming::Reader->for_stream( $fd );

  return $self;
}
#
# @brief
#
# @param $self
#
# @return
#
sub parser
{
  return shift->{ _PARSER };
}
#
# @brief
#
# @param $self - API::Eulerian::EDW::Parser
#
use Data::Dumper;
sub do
{
  my ( $self, $hooks ) = @_;
  my $parser = $self->parser();
  my $depth = -1;
  my @in = ();
  my $uuid;
  my $msg;
  my $key;

  # Parse JSON stream
  $parser->process_tokens(
    # Property begin
    start_property => sub
    {
      $key = shift;
    },
    # Property end
    end_property => sub
    {
    },
    # String
    add_string => sub
    {
      my $parent = $in[ $depth ];
      if( ref( $parent ) eq 'ARRAY' ) {
        $parent->[ scalar( @$parent ) ] = shift;
      } elsif( ref( $parent ) eq 'HASH' ) {
        $parent->{ $key } = shift;
      }
    },



( run in 0.981 second using v1.01-cache-2.11-cpan-39bf76dae61 )