Class: OpenC3::JsonAccessor

Inherits:
Accessor show all
Defined in:
lib/openc3/accessors/json_accessor.rb

Direct Known Subclasses

CborAccessor

Instance Attribute Summary

Attributes inherited from Accessor

#packet

Class Method Summary collapse

Instance Method Summary collapse

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



33
34
35
36
37
# File 'lib/openc3/accessors/json_accessor.rb', line 33

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



60
61
62
63
64
65
66
67
68
# File 'lib/openc3/accessors/json_accessor.rb', line 60

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



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/openc3/accessors/json_accessor.rb', line 39

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



93
94
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
# File 'lib/openc3/accessors/json_accessor.rb', line 93

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



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/openc3/accessors/json_accessor.rb', line 70

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 enfore that COSMOS DERIVED items must have a write_conversion to be written



166
167
168
# File 'lib/openc3/accessors/json_accessor.rb', line 166

def enforce_derived_write_conversion(_item)
  return true
end

#enforce_encodingObject

If this is set it will enforce that buffer data is encoded in a specific encoding



146
147
148
# File 'lib/openc3/accessors/json_accessor.rb', line 146

def enforce_encoding
  return nil
end

#enforce_lengthObject

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



153
154
155
# File 'lib/openc3/accessors/json_accessor.rb', line 153

def enforce_length
  return false
end

#enforce_short_buffer_allowedObject

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



160
161
162
# File 'lib/openc3/accessors/json_accessor.rb', line 160

def enforce_short_buffer_allowed
  return true
end