Skip to content

Commit

Permalink
Merge pull request #876 from OpenC3/as_json
Browse files Browse the repository at this point in the history
Hash as_json handle unlimited items
  • Loading branch information
jmthomas authored Oct 17, 2023
2 parents fc401ed + 84d13af commit 0959cbd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
12 changes: 6 additions & 6 deletions openc3/lib/openc3/io/json_rpc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ def as_json(options = nil) #:nodoc:

class Struct #:nodoc:
def as_json(options = nil)
pairs = []
self.each_pair { |k, v| pairs << k.to_s; pairs << v.as_json(options) }
Hash[*pairs]
hash = {}
self.each_pair { |k, v| hash[k.to_s] = v.as_json(options) }
hash
end
end

Expand Down Expand Up @@ -115,9 +115,9 @@ def as_json(options = nil) #:nodoc:

class Hash
def as_json(options = nil) #:nodoc:
pairs = []
self.each { |k, v| pairs << k.to_s; pairs << v.as_json(options) }
Hash[*pairs]
hash = {}
self.each {|k,v| hash[k.to_s] = v.as_json(options) }
hash
end
end

Expand Down
16 changes: 16 additions & 0 deletions openc3/spec/packets/packet_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1557,6 +1557,22 @@ module OpenC3
expect(json['accessor']).to eql "OpenC3::BinaryAccessor"
expect(json['template']).to eql Base64.encode64("\x00\x01\x02\x03")
end

it "handles many items" do
packet = Packet.new("tgt", "pkt")
(1..2048).each do |idx|
(1..100).each do |field|
packet.append_item("field_X#{field}_#{idx}", 64, :UINT)
end
end
expect(packet.sorted_items.length).to eql 204800
json_hash = Hash.new
packet.sorted_items.each do |item|
json_hash[item.name] = nil
end
json = json_hash.as_json(:allow_nan => true)
expect(json.length).to eql 204800
end
end

describe "self.from_json" do
Expand Down

0 comments on commit 0959cbd

Please sign in to comment.