Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hash as_json handle unlimited items #876

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was overwhelming the stack with the huge amount of items and causing a 'Stack too deep' error.

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