Sha256: 26f49e677765c8849e58b088784208490c0fd4a5b09eb7d34b774f4ece7eabac

Contents?: true

Size: 646 Bytes

Versions: 3

Compression:

Stored size: 646 Bytes

Contents

require 'json'

module FHIR
  module Json
    #
    #  This module includes methods to serialize or deserialize FHIR resources to and from JSON.
    #

    def to_json
      JSON.pretty_unparse(to_hash)
    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 => ex
        FHIR.logger.error("Failed to deserialize JSON:\n#{ex.backtrace}")
        FHIR.logger.debug("JSON:\n#{json}")
        resource = nil
      end
      resource
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
fhir_models-4.1.1 lib/fhir_models/bootstrap/json.rb
fhir_models-4.1.0 lib/fhir_models/bootstrap/json.rb
fhir_models-4.0.2 lib/fhir_models/bootstrap/json.rb