Sha256: 42efb04cd1196d8b499939da99ed5d95ceab1cbc507c0510e1fad1dfe8691469

Contents?: true

Size: 1022 Bytes

Versions: 4

Compression:

Stored size: 1022 Bytes

Contents

module Chronicle
  module ETL
    class JSONLoader < Chronicle::ETL::Loader
      register_connector do |r|
        r.description = 'json'
      end

      setting :serializer
      setting :output, default: $stdout

      def start
        if @config.output == $stdout
          @output = @config.output
        else
          @output = File.open(@config.output, "w")
        end
      end

      def load(record)
        serialized = serializer.serialize(record)

        # When dealing with raw data, we can get improperly encoded strings
        # (eg from sqlite database columns). We force conversion to UTF-8
        # before converting into JSON
        encoded = serialized.transform_values do |value|
          next value unless value.is_a?(String)

          force_utf8(value)
        end
        @output.puts encoded.to_json
      end

      def finish
        @output.close
      end

      private

      def serializer
        @config.serializer || Chronicle::ETL::RawSerializer
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
chronicle-etl-0.5.0 lib/chronicle/etl/loaders/json_loader.rb
chronicle-etl-0.4.4 lib/chronicle/etl/loaders/json_loader.rb
chronicle-etl-0.4.3 lib/chronicle/etl/loaders/json_loader.rb
chronicle-etl-0.4.2 lib/chronicle/etl/loaders/json_loader.rb