Sha256: d9edc41ae2a3adaeeb09095f79a64300959356461f0fe6dddef794d1bf9b7f62

Contents?: true

Size: 1.33 KB

Versions: 36

Compression:

Stored size: 1.33 KB

Contents

module Embulk
  module Guess
    class JsonGuessPlugin < GuessPlugin
      Plugin.register_guess('json', self)

      java_import 'com.google.common.collect.Lists'
      java_import 'java.io.ByteArrayInputStream'
      java_import 'org.embulk.spi.Exec'
      java_import 'org.embulk.spi.json.JsonParser'
      java_import 'org.embulk.spi.json.JsonParseException'
      java_import 'org.embulk.spi.util.FileInputInputStream'
      java_import 'org.embulk.spi.util.InputStreamFileInput'

      def guess(config, sample_buffer)
        return {} unless config.fetch("parser", {}).fetch("type", "json") == "json"

        # Use org.embulk.spi.json.JsonParser to respond to multi-line Json
        json_parser = new_json_parser(sample_buffer)
        begin
          while json_parser.next
          end
        rescue JsonParseException
          return {}
        end

        return {"parser" => {"type" => "json"}}
      end

      private

      def new_json_parser(buffer)
        input_streams = Lists::newArrayList(ByteArrayInputStream.new(buffer.to_java_bytes))
        iterator_provider = InputStreamFileInput::IteratorProvider.new(input_streams)
        input = FileInputInputStream.new(InputStreamFileInput.new(Java::SPI::Exec.getBufferAllocator(), iterator_provider))
        input.nextFile
        JsonParser.new.open(input)
      end
    end
  end
end

Version data entries

36 entries across 36 versions & 1 rubygems

Version Path
embulk-0.8.22 lib/embulk/guess/json.rb
embulk-0.8.22-java lib/embulk/guess/json.rb
embulk-0.8.21 lib/embulk/guess/json.rb
embulk-0.8.21-java lib/embulk/guess/json.rb
embulk-0.8.20 lib/embulk/guess/json.rb
embulk-0.8.20-java lib/embulk/guess/json.rb
embulk-0.8.19 lib/embulk/guess/json.rb
embulk-0.8.19-java lib/embulk/guess/json.rb
embulk-0.8.18 lib/embulk/guess/json.rb
embulk-0.8.18-java lib/embulk/guess/json.rb
embulk-0.8.17 lib/embulk/guess/json.rb
embulk-0.8.17-java lib/embulk/guess/json.rb
embulk-0.8.16 lib/embulk/guess/json.rb
embulk-0.8.16-java lib/embulk/guess/json.rb
embulk-0.8.15 lib/embulk/guess/json.rb
embulk-0.8.15-java lib/embulk/guess/json.rb
embulk-0.8.14 lib/embulk/guess/json.rb
embulk-0.8.14-java lib/embulk/guess/json.rb
embulk-0.8.13 lib/embulk/guess/json.rb
embulk-0.8.13-java lib/embulk/guess/json.rb