Sha256: 9114b98bc4f56719de83be5dd7eeba35d752a5575c89f0afa50bac98809c206b

Contents?: true

Size: 748 Bytes

Versions: 1

Compression:

Stored size: 748 Bytes

Contents

require 'json'

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

      def to_json(*_args)
        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::STU3::#{resource_type}")
          resource = klass.new(hash)
        rescue StandardError => e
          FHIR::STU3.logger.error("Failed to deserialize JSON:\n#{e.backtrace}")
          FHIR::STU3.logger.debug("JSON:\n#{json}")
          resource = nil
        end
        resource
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fhir_stu3_models-3.2.0 lib/fhir_stu3_models/bootstrap/json.rb