Module: NWN::Gff::Handler::JSON

Defined in:
lib/nwn/json_support.rb

Class Method Summary collapse

Class Method Details

.dump(struct, io) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/nwn/json_support.rb', line 28

def self.dump struct, io
  d = if NWN.setting(:pretty_json)
    ::JSON.pretty_generate(struct)
  else
    ::JSON.generate(struct)
  end
  io.puts d
  d.size + 1
end

.load(io) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/nwn/json_support.rb', line 16

def self.load io
  json = if io.respond_to?(:to_str)
    io.to_str
  elsif io.respond_to?(:to_io)
    io.to_io.read
  else
    io.read
  end

  NWN::Gff::Struct.unbox!(JSON.parse(json), nil)
end