Module: AsaJson

Defined in:
lib/asa_json.rb,
lib/asa_json/version.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
'0.1.0'

Class Method Summary collapse

Class Method Details

.read_json(file, json_options = {}) ⇒ Object

JSONファイルの読み取り



31
32
33
34
35
36
# File 'lib/asa_json.rb', line 31

def self.read_json(file, json_options = {})
  result = nil
  File.open(file, 'r') do |f|
    result = JSON.parse(f.read, json_options)
  end
end

.read_jsonl(file, json_options = {}) ⇒ Object

JSONLファイルの読み取り



11
12
13
14
15
16
17
18
19
# File 'lib/asa_json.rb', line 11

def self.read_jsonl(file, json_options = {})
  result = []
  File.open(file, 'r') do |f|
    f.each_line do |line|
      result << JSON.parse(line, json_options)
    end
  end
  result
end

.write_json(file, data) ⇒ Object

JSONファイルの書き込み



39
40
41
42
43
# File 'lib/asa_json.rb', line 39

def self.write_json(file, data)
  File.open(file, 'w') do |f|
    f.write(JSON.pretty_generate(data))
  end
end

.write_jsonl(file, lines) ⇒ Object

JSONLファイルの書き込み



22
23
24
25
26
27
28
# File 'lib/asa_json.rb', line 22

def self.write_jsonl(file, lines)
  File.open(file, 'w') do |f|
    lines.each do |line|
      f.write("#{line.to_json}\n")
    end
  end
end