Skip to content

Commit

Permalink
Version Json and Xml
Browse files Browse the repository at this point in the history
  • Loading branch information
360dgries committed Sep 23, 2024
1 parent b05d9ea commit bac063d
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 163 deletions.
18 changes: 6 additions & 12 deletions lib/fhir_models/bootstrap/json.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
require 'json'

module FHIR
module Json
module ClassJson
#
# 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)
def from_json(json)
hash = JSON.parse(json)
resource = nil
begin
Expand All @@ -24,13 +20,11 @@ def self.from_json(json)
end
resource
end
end

def self.fhir_version_string
FHIR.fhir_version_string
end

def self.versioned_fhir_module
FHIR.versioned_fhir_module
module InstanceJson
def to_json(opts = nil)
JSON.pretty_generate(to_hash, opts)
end
end
end
151 changes: 70 additions & 81 deletions lib/fhir_models/bootstrap/xml.rb
Original file line number Diff line number Diff line change
@@ -1,75 +1,7 @@
require 'nokogiri'
module FHIR
module Xml
extend FHIR::Deprecate
#
# This module includes methods to serialize or deserialize FHIR resources to and from XML.
#

def to_xml
hash = to_hash
hash.delete('resourceType')
doc = Nokogiri::XML::Document.new
doc.encoding = 'utf-8'
doc.root = hash_to_xml_node(resourceType, hash, doc)
doc.root.default_namespace = 'http://hl7.org/fhir'
doc.to_xml
end

# Hash keys are ordered in Ruby 1.9 and later, so we can rely on their order
# to be the correct order for the XML serialization.
def hash_to_xml_node(name, hash, doc)
node = Nokogiri::XML::Node.new(name, doc)

# if hash contains resourceType
# create a child node with the name==resourceType
# fill that, and place the child under the above `node`
if hash['resourceType'].is_a?(String) && name != 'instance'
child_name = hash['resourceType']
hash.delete('resourceType')
child = hash_to_xml_node(child_name, hash, doc)
node.add_child(child)
return node
end

hash.each do |key, value|
next if ['extension', 'modifierExtension'].include?(name) && key == 'url'
next if key == 'id' && !versioned_fhir_module::RESOURCES.include?(name)

case value
when Hash
node.add_child(hash_to_xml_node(key, value, doc))
when Array
value.each do |v|
if v.is_a?(Hash)
node.add_child(hash_to_xml_node(key, v, doc))
else
child = Nokogiri::XML::Node.new(key, doc)
child.set_attribute('value', v)
node.add_child(child)
end
end
else
child = Nokogiri::XML::Node.new(key, doc)
if name == 'text' && key == 'div'
child.set_attribute('xmlns', 'http://www.w3.org/1999/xhtml')
html = value.strip
if html.start_with?('<div') && html.end_with?('</div>')
html = html[html.index('>') + 1..-7]
end
child.inner_html = html
else
child.set_attribute('value', value)
end
node.add_child(child)
end
end
node.set_attribute('url', hash['url']) if ['extension', 'modifierExtension'].include?(name)
node.set_attribute('id', hash['id']) if hash['id'] && !versioned_fhir_module::RESOURCES.include?(name)
node
end

def self.from_xml(xml)
module ClassXml
def from_xml(xml)
doc = Nokogiri::XML(xml)
doc.root.add_namespace_definition('f', 'http://hl7.org/fhir')
doc.root.add_namespace_definition('x', 'http://www.w3.org/1999/xhtml')
Expand All @@ -88,7 +20,7 @@ def self.from_xml(xml)
resource
end

def self.xml_node_to_hash(node)
def xml_node_to_hash(node)
hash = {}
node.children.each do |child|
next if [Nokogiri::XML::Text, Nokogiri::XML::Comment].include?(child.class)
Expand Down Expand Up @@ -126,27 +58,84 @@ def self.xml_node_to_hash(node)
end
end

def self.valid?(xml)
def valid?(xml)
validate(xml).empty?
end
deprecate :is_valid?, :valid?

def self.validate(xml)
def validate(xml)
defns = File.expand_path '../definitions/schema', File.dirname(File.absolute_path(__FILE__))
schema = File.join(defns, 'fhir-all.xsd')
xsd = Nokogiri::XML::Schema(File.new(schema))
xsd.validate(Nokogiri::XML(xml))
end
end

def self.fhir_version_string
FHIR.fhir_version_string
end
module InstanceXml
#
# This module includes methods to serialize or deserialize FHIR resources to and from XML.
#

def self.versioned_fhir_module
FHIR.versioned_fhir_module
def to_xml
hash = to_hash
hash.delete('resourceType')
doc = Nokogiri::XML::Document.new
doc.encoding = 'utf-8'
doc.root = hash_to_xml_node(resourceType, hash, doc)
doc.root.default_namespace = 'http://hl7.org/fhir'
doc.to_xml
end

private :hash_to_xml_node
private_class_method :xml_node_to_hash
# Hash keys are ordered in Ruby 1.9 and later, so we can rely on their order
# to be the correct order for the XML serialization.
def hash_to_xml_node(name, hash, doc)
node = Nokogiri::XML::Node.new(name, doc)

# if hash contains resourceType
# create a child node with the name==resourceType
# fill that, and place the child under the above `node`
if hash['resourceType'].is_a?(String) && name != 'instance'
child_name = hash['resourceType']
hash.delete('resourceType')
child = hash_to_xml_node(child_name, hash, doc)
node.add_child(child)
return node
end

hash.each do |key, value|
next if ['extension', 'modifierExtension'].include?(name) && key == 'url'
next if key == 'id' && !versioned_fhir_module::RESOURCES.include?(name)

case value
when Hash
node.add_child(hash_to_xml_node(key, value, doc))
when Array
value.each do |v|
if v.is_a?(Hash)
node.add_child(hash_to_xml_node(key, v, doc))
else
child = Nokogiri::XML::Node.new(key, doc)
child.set_attribute('value', v)
node.add_child(child)
end
end
else
child = Nokogiri::XML::Node.new(key, doc)
if name == 'text' && key == 'div'
child.set_attribute('xmlns', 'http://www.w3.org/1999/xhtml')
html = value.strip
if html.start_with?('<div') && html.end_with?('</div>')
html = html[html.index('>') + 1..-7]
end
child.inner_html = html
else
child.set_attribute('value', value)
end
node.add_child(child)
end
end
node.set_attribute('url', hash['url']) if ['extension', 'modifierExtension'].include?(name)
node.set_attribute('id', hash['id']) if hash['id'] && !versioned_fhir_module::RESOURCES.include?(name)
node
end
end
end
8 changes: 5 additions & 3 deletions lib/fhir_models/r4.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@

module FHIR
module R4
def self.fhir_version_string
module_function

def fhir_version_string
'R4'
end

def self.versioned_fhir_module
self
def versioned_fhir_module
FHIR::R4
end
end
end
17 changes: 0 additions & 17 deletions lib/fhir_models/r4/fhir_ext/element_definition.rb

This file was deleted.

17 changes: 0 additions & 17 deletions lib/fhir_models/r4/fhir_ext/structure_definition.rb

This file was deleted.

8 changes: 5 additions & 3 deletions lib/fhir_models/r4b.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@

module FHIR
module R4B
def self.fhir_version_string
module_function

def fhir_version_string
'R4B'
end

def self.versioned_fhir_module
self
def versioned_fhir_module
FHIR::R4B
end
end
end
15 changes: 0 additions & 15 deletions lib/fhir_models/r4b/fhir_ext/element_definition.rb

This file was deleted.

15 changes: 0 additions & 15 deletions lib/fhir_models/r4b/fhir_ext/structure_definition.rb

This file was deleted.

0 comments on commit bac063d

Please sign in to comment.