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

def self.read_item(item, buffer)
  return nil if item.data_type == :DERIVED
  return JsonPath.on(buffer, item.key).first
end

.read_items(items, buffer) ⇒ Object



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

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



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

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



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

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
  if parent_node
    parent_node[parent_key] = value
  else
    decoded.replace(value)
  end
  return decoded
end

.write_items(items, values, buffer) ⇒ Object



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

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



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

def enforce_derived_write_conversion(item)
  return true
end

#enforce_encodingObject



134
135
136
# File 'lib/openc3/accessors/json_accessor.rb', line 134

def enforce_encoding
  return nil
end

#enforce_lengthObject



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

def enforce_length
  return false
end

#enforce_short_buffer_allowedObject



142
143
144
# File 'lib/openc3/accessors/json_accessor.rb', line 142

def enforce_short_buffer_allowed
  return true
end