Sha256: 73960a2e67bf00191ffda9aa82670233cb6ac622a5cba91df8ae9a8d9121f014

Contents?: true

Size: 1.18 KB

Versions: 13

Compression:

Stored size: 1.18 KB

Contents

require 'oj'

module BEL::Extension::Format
  module JSONImplementation

    class JSONReader
      def initialize(data)
        @data = data
      end

      def each(&block)
        if block_given?
          Oj.sc_parse(EvidenceHandler.new(block), @data, :symbol_keys => true)
        else
          to_enum(:each)
        end
      end
    end

    class JSONWriter

      def write_json_object(json_object)
        Oj.dump(json_object, :mode => :compat)
      end
    end

    private

    class EvidenceHandler < Oj::ScHandler

      def initialize(callable)
        @callable = callable
      end

      def hash_start
        {}
      end

      def hash_end
        @callable.call @hash 
      end

      def hash_set(hash, key, value)
        hash[key]  = value
        @hash = hash
      end

      def array_start
        @array = []
        @array
      end

      def array_append(array, value)
        array << value
        @array = array
      end

      def array_end()
        @callable.call @array
      end

      def error(message, line, column)
        msg = "Parse error at line #{line}, column #{column}: #{message}"
        raise Oj::ParseError.new(msg)
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
bel-0.3.3-x64-mingw32 lib/bel/extensions/json/oj.rb
bel-0.3.3-x86-mingw32 lib/bel/extensions/json/oj.rb
bel-0.3.3 lib/bel/extensions/json/oj.rb
bel-0.3.2-x64-mingw32 lib/bel/extensions/json/oj.rb
bel-0.3.2-x86-mingw32 lib/bel/extensions/json/oj.rb
bel-0.3.2 lib/bel/extensions/json/oj.rb
bel-0.3.1-x64-mingw32 lib/bel/extensions/json/oj.rb
bel-0.3.1-x86-mingw32 lib/bel/extensions/json/oj.rb
bel-0.3.1 lib/bel/extensions/json/oj.rb
bel-0.3.0-x86-mingw32 lib/bel/extensions/json/oj.rb
bel-0.3.0-x64-mingw32 lib/bel/extensions/json/oj.rb
bel-0.3.0 lib/bel/extensions/json/oj.rb
bel-0.3.0.beta6 lib/bel/extensions/json/oj.rb