<commit_before>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
        # FIXME The top-level key should be "evidence".
        if @hash.has_key?(:bel_statement)
          @callable.call @hash 
        end
      end

      def hash_set(h,k,v)
        h[k]  = v
        @hash = h
      end

      def array_start
        []
      end

      def array_append(a,v)
        a << v
      end

      def error(message, line, column)
        msg = "Parse error at line #{line}, column #{column}: #{message}"
        raise Oj::ParseError.new(msg)
      end
    end
  end
end<commit_msg>oj adapter; expect the :evidence root name<commit_after>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
        if @hash.has_key?(:evidence)
          @callable.call @hash 
        end
      end

      def hash_set(h,k,v)
        h[k]  = v
        @hash = h
      end

      def array_start
        []
      end

      def array_append(a,v)
        a << v
      end

      def error(message, line, column)
        msg = "Parse error at line #{line}, column #{column}: #{message}"
        raise Oj::ParseError.new(msg)
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.08693808312128923, 'fraction_numerical': 0.0, 'mean_word_length': 1.7083811710677381, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5499164', 'n_tokens_mistral': 750, 'n_tokens_neox': 711, 'n_words': 190}