Sha256: b71f811b38526fc6edf20c76a9e3bc5f31cc7aedc622746423a1ab9cb06458f0

Contents?: true

Size: 663 Bytes

Versions: 3

Compression:

Stored size: 663 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(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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
fhir_models-4.2.2 lib/fhir_models/bootstrap/json.rb
fhir_models-4.2.1 lib/fhir_models/bootstrap/json.rb
fhir_models-4.2.0 lib/fhir_models/bootstrap/json.rb