Git Product home page Git Product logo

Comments (6)

tugboat avatar tugboat commented on June 1, 2024 1

@antonivanopoulos we are using oj in the application where I was running into this issue. That is probably the cause for me.

from fhir_client.

trazaq avatar trazaq commented on June 1, 2024

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

from fhir_client.

tugboat avatar tugboat commented on June 1, 2024

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.

from fhir_client.

tugboat avatar tugboat commented on June 1, 2024

@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.

from fhir_client.

trazaq avatar trazaq commented on June 1, 2024

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.

from fhir_client.

antonivanopoulos avatar antonivanopoulos commented on June 1, 2024

@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.

from fhir_client.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.