<commit_before>require 'json'
module Noid::Persistence
  class JSON < Base
    FILENAME = 'NOID.js'
    def initialize args = {}
      @file = args[:filename] || FILENAME
      data = load_json
      super data.merge(args)
      save
    end

    protected
    def load_json
      data = ::JSON.parse(File.read(@file)) if File.exists? @file
      data ||= {}
      data = data.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo} unless data.empty?
      data[:counters] = data[:counters].map { |x| x.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo} } if data[:counters]
      data[:identifier] = { :class => Kernel.const_get(data[:identifier]['class']) } if data[:identifier]
      data
    end

    def save
      File.open(@file, 'w') do |f|
          str = { :identifier => { :class => identifier }, :s => @s, :counters => @counters, :seed => @seed, }.to_json
        f.write(str)
      end
    end

  end
end<commit_msg>make it possible to use json persistence beyond the file system<commit_after>require 'json'
module Noid::Persistence
  class JSON < Base
    FILENAME = 'NOID.js'
    def initialize args = {}
      @file = args[:filename] || FILENAME
      @json = args[:json] || File.read(@file) rescue nil
      data = load_json if @json
      data ||= {}
      super data.merge(args)
      save
    end

    protected
    def load_json
      data = ::JSON.parse(@json) rescue nil
      data ||= {}
      data = data.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo} unless data.empty?
      data[:counters] = data[:counters].map { |x| x.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo} } if data[:counters]
      data[:identifier] = { :class => Kernel.const_get(data[:identifier]['class']) } if data[:identifier]
      data
    end

    def save
      str = self.to_json
      File.open(@file, 'w') do |f|
        f.write(str)
      end unless @file == '/dev/null'

      str
    end

    def to_json
      str = { :template => @template, :identifier => { :class => identifier.name }, :s => @s, :counters => @counters, :seed => @seed }.to_json
    end

  end
end
{'fraction_non_alphanumeric': 0.16658619024625784, 'fraction_numerical': 0.0, 'mean_word_length': 2.680284191829485, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 16, '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': '6060007', 'n_tokens_mistral': 713, 'n_tokens_neox': 695, 'n_words': 177}