Open trazaq opened 2 years ago
It doesn't seem to like the JSON.pretty_generate(to_hash, opts) if I'm reading it correctly
require 'json'
module FHIR
module Json
#
# This module includes methods to serialize or deserialize FHIR resources to and from JSON.
#
def to_json(opts = nil)
JSON.pretty_generate(to_hash, opts)
end
def self.from_json(json)
hash = JSON.parse(json)
resource = nil
begin
resource_type = hash['resourceType']
klass = Module.const_get("FHIR::#{resource_type}")
resource = klass.new(hash)
rescue => e
FHIR.logger.error("Failed to deserialize JSON:\n#{e.backtrace}")
FHIR.logger.debug("JSON:\n#{json}")
resource = nil
end
resource
end
end
end
I am having the same issue. We are running ruby version 3.0.1, could that be a potential cause of this?
If I make a call to to_json
and pass an empty hash as an argument it seems to serialize correctly.
@trazaq, for now I was able to work around this by overriding the to_json
method to pass an empty hash.
I am using the gem in a rails app so I was able to create a file at config/initializers/overrides.rb
with the following contents.
module FHIR
module Json
def to_json(opts = {})
super
end
end
end
That has me going for now, but I am not sure what the implications of doing something like that might be for your code, so YMMV.
I appreciate the authors of this gem! It does indeed come in handy!
@tugboat
Ah! That's one way of going about it!
Anyway, For me, I couldn't really wait around for an answer because I'm on a timeline so I decided to use FHIR models only of this gem. (I do realize there's a separate gem for models only, too)
I'm not using this gem's internal HTTP client.
I construct the models and use HTTParty to do the actual sending/receiving of data as my HTTP client.
Works well so far. Just have to remember to convert your model to a hash, then to json in my case before sending it to the receiving system.
@trazaq @tugboat Do either of you use Oj? I ran into the same issue and it was due to our current use of Oj's optimize_rails
method that redefines the JSON module, introducing the behaviour you're seeing on #pretty_generate
. I found that if just using the json
gem, it doesn't behave that way.
@antonivanopoulos we are using oj in the application where I was running into this issue. That is probably the cause for me.
HI,
Thanks for this gem!
I'm getting a error after following the transaction example in the README.
Any ideas?