Module: NWN::Gff::Handler::JSON
- Defined in:
- lib/nwn/json_support.rb
Class Method Summary collapse
- .dump(struct, io) ⇒ Object
- .json_unbox_field(element, parent_label, parent) ⇒ Object
- .json_unbox_struct(o, parent = nil) ⇒ Object
- .load(io) ⇒ Object
Class Method Details
.dump(struct, io) ⇒ Object
108 109 110 111 112 113 114 115 116 |
# File 'lib/nwn/json_support.rb', line 108 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 |
.json_unbox_field(element, parent_label, parent) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/nwn/json_support.rb', line 44 def self.json_unbox_field element, parent_label, parent element.extend(NWN::Gff::Field) element.field_label = parent_label element.parent = parent element.str_ref ||= NWN::Gff::Field::DEFAULT_STR_REF if element.respond_to?('str_ref=') element. case element.field_type when :cexolocstr element.field_value.keys.each {|key| val = element.field_value.delete(key) element.field_value[key.to_i] = val } when :void element.field_value = Base64::strict_decode64(element.field_value) when :list mod = {} element.field_value.each_with_index {|x,idx| mod[idx] = self.json_unbox_struct(x, element) } mod.each {|x,y| element.field_value[x] = y } when :struct element.field_value = self.json_unbox_struct(element.field_value, element) end element.validate element end |
.json_unbox_struct(o, parent = nil) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/nwn/json_support.rb', line 76 def self.json_unbox_struct o, parent = nil o.extend(NWN::Gff::Struct) o.element = parent if parent o.struct_id = o.delete('__struct_id') o.data_type = o.delete('__data_type') o.data_version = o.delete('__data_version') o.data_version ||= NWN::Gff::Struct::DEFAULT_DATA_VERSION NWN.log_debug("Unboxed without a root data type") if !parent && !o.data_type NWN.log_debug("Unboxed with explicit data type #{o.data_type.inspect}") if parent && o.data_type o.each {|label,element| o[label] = self.json_unbox_field(element, label, o) } o end |
.load(io) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/nwn/json_support.rb', line 96 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 self.json_unbox_struct(JSON.parse(json), nil) end |