Class: OpenC3::JsonAccessor
- Defined in:
- lib/openc3/accessors/json_accessor.rb
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from Accessor
Class Method Summary collapse
- .read_item(item, buffer) ⇒ Object
- .read_items(items, buffer) ⇒ Object
- .write_item(item, value, buffer) ⇒ Object
- .write_item_internal(item, value, decoded) ⇒ Object
- .write_items(items, values, buffer) ⇒ Object
Instance Method Summary collapse
-
#enforce_derived_write_conversion(_item) ⇒ Object
If this is true it will enforce that COSMOS DERIVED items must have a write_conversion to be written.
-
#enforce_encoding ⇒ Object
If this is set it will enforce that buffer data is encoded in a specific encoding.
-
#enforce_length ⇒ Object
This affects whether the Packet class enforces the buffer length at all.
-
#enforce_short_buffer_allowed ⇒ Object
This sets the short_buffer_allowed flag in the Packet class which allows packets that have a buffer shorter than the defined size.
Methods inherited from Accessor
#args, convert_to_type, #initialize, #read_item, #read_items, #write_item, #write_items
Constructor Details
This class inherits a constructor from OpenC3::Accessor
Class Method Details
.read_item(item, buffer) ⇒ Object
35 36 37 38 39 |
# File 'lib/openc3/accessors/json_accessor.rb', line 35 def self.read_item(item, buffer) return nil if item.data_type == :DERIVED value = JsonPath.on(buffer, item.key).first return convert_to_type(value, item) end |
.read_items(items, buffer) ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/openc3/accessors/json_accessor.rb', line 62 def self.read_items(items, buffer) # Prevent JsonPath from decoding every call if String === buffer decoded = JSON.parse(buffer, :allow_nan => true, :create_additions => true) else decoded = buffer end super(items, decoded) end |
.write_item(item, value, buffer) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/openc3/accessors/json_accessor.rb', line 41 def self.write_item(item, value, buffer) return nil if item.data_type == :DERIVED # Convert to ruby objects if String === buffer decoded = JSON.parse(buffer, :allow_nan => true, :create_additions => true) else decoded = buffer end # Write the value write_item_internal(item, value, decoded) # Update buffer if String === buffer buffer.replace(JSON.generate(decoded.as_json, :allow_nan => true)) end return value end |
.write_item_internal(item, value, decoded) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/openc3/accessors/json_accessor.rb', line 95 def self.write_item_internal(item, value, decoded) return nil if item.data_type == :DERIVED # Save traversal state parent_node = nil parent_key = nil node = decoded # Parse the JsonPath json_path = JsonPath.new(item.key) # Handle each token json_path.path.each do |token| case token when '$' # Ignore start - it is implied next when /\[.*\]/ # Array or Hash Index if token.index("'") # Hash index key = token[2..-3] if not (Hash === node) node = {} parent_node[parent_key] = node end parent_node = node parent_key = key node = node[key] else # Array index key = token[1..-2].to_i if not (Array === node) node = [] parent_node[parent_key] = node end parent_node = node parent_key = key node = node[key] end else raise "Unsupported key/token: #{item.key} - #{token}" end end value = convert_to_type(value, item) if parent_node parent_node[parent_key] = value else decoded.replace(value) end return decoded end |
.write_items(items, values, buffer) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/openc3/accessors/json_accessor.rb', line 72 def self.write_items(items, values, buffer) # Start with an empty object if no buffer buffer.replace("{}") if buffer.length == 0 or buffer[0] == "\x00" # Convert to ruby objects if String === buffer decoded = JSON.parse(buffer, :allow_nan => true) else decoded = buffer end items.each_with_index do |item, index| write_item_internal(item, values[index], decoded) end # Update buffer if String === buffer buffer.replace(JSON.generate(decoded, :allow_nan => true)) end return values end |
Instance Method Details
#enforce_derived_write_conversion(_item) ⇒ Object
If this is true it will enforce that COSMOS DERIVED items must have a write_conversion to be written
168 169 170 |
# File 'lib/openc3/accessors/json_accessor.rb', line 168 def enforce_derived_write_conversion(_item) return true end |
#enforce_encoding ⇒ Object
If this is set it will enforce that buffer data is encoded in a specific encoding
148 149 150 |
# File 'lib/openc3/accessors/json_accessor.rb', line 148 def enforce_encoding return nil end |
#enforce_length ⇒ Object
This affects whether the Packet class enforces the buffer length at all. Set to false to remove any correlation between buffer length and defined sizes of items in COSMOS
155 156 157 |
# File 'lib/openc3/accessors/json_accessor.rb', line 155 def enforce_length return false end |
#enforce_short_buffer_allowed ⇒ Object
This sets the short_buffer_allowed flag in the Packet class which allows packets that have a buffer shorter than the defined size. Note that the buffer is still resized to the defined length
162 163 164 |
# File 'lib/openc3/accessors/json_accessor.rb', line 162 def enforce_short_buffer_allowed return true end |