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



25
26
27
28
29
# File 'lib/openc3/accessors/json_accessor.rb', line 25

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



52
53
54
55
56
57
58
59
60
# File 'lib/openc3/accessors/json_accessor.rb', line 52

def self.read_items(items, buffer)
  # Prevent JsonPath from decoding every call
  if String === buffer
    decoded = JSON.parse(buffer, :allow_nan => true)
  else
    decoded = buffer
  end
  super(items, decoded)
end

.write_item(item, value, buffer) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/openc3/accessors/json_accessor.rb', line 31

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)
  else
    decoded = buffer
  end

  # Write the value
  write_item_internal(item, value, decoded)

  # Update buffer
  if String === buffer
    buffer.replace(JSON.generate(decoded, :allow_nan => true))
  end

  return value
end

.write_item_internal(item, value, decoded) ⇒ Object



85
86
87
88
89
90
91
92
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
# File 'lib/openc3/accessors/json_accessor.rb', line 85

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



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/openc3/accessors/json_accessor.rb', line 62

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



158
159
160
# File 'lib/openc3/accessors/json_accessor.rb', line 158

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



138
139
140
# File 'lib/openc3/accessors/json_accessor.rb', line 138

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



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

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



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

def enforce_short_buffer_allowed
  return true
end