• Iguana 6
  • Previous Versions
  • API
  • Sample Code
  • Training
  • Create a Ticket
iNTERFACEWARE Help Center
  • Iguana 6
  • Previous Versions
  • API
  • Sample Code
  • Training
  • Create a Ticket

Code Repository

Home›Code Repository›hl7.serialize.lua
Modules

hl7.serialize.lua

Verified Featured
Added by iNTERFACEWARE

Serializes an HL7 message using specified non-standard delimiters and/or escape characters

Source Code
-- hl7.serialize module 
-- http://help.interfaceware.com/code/details/hl7-serialize-lua

-- Will search for instances of Pattern in S.
-- Will call replacement function onUnmatched
-- on unmatched parts of S, and onMatched on
-- matched parts of S.
local function search(S, Pattern, onUnmatched, onMatched)
   Pattern = '^(.-)('..Pattern..')(.*)$'
   local Out = {}
   local function loop(S)
      local Head, Match, Tail = select(3, S:find(Pattern))
      if Match then
         table.insert(Out, (onUnmatched(Head)) )
         table.insert(Out, (onMatched(Match))  )
         return loop(Tail)
      else
         table.insert(Out, (onUnmatched(S)) )
         return table.concat(Out)
      end
   end
   return loop(S)
end
 
local charsToEscape = {['(']=1, [')']=1, ['.']=1, ['%']=1, ['+']=1, ['-']=1, 
   ['*']=1, ['?']=1, ['[']=1, ['^']=1, ['$']=1}


local function charToPattern(C)
   if charsToEscape[C] then
      return '%'..C
   else
      return C
   end
end
 
-- Only for HL7 Messages
-- Accepts a table which must contain the following parameter:
--   data: the HL7 message to be serialized.
-- Also accepts the following optional parameters:
--   delimiters: a list of delimiters to use in the message,
--      if different from the default {'\r', '|', '^', '~', '\\', '&'}.
--   escaped: a list representing the character used to represent
--      an escaped delimiter character, if different from the default
--      {'F', 'S', 'R', 'E', 'T'}.
--
function hl7.serialize(Params)
   Params = Params or {}
   local Msg = Params.data
   local NodeType, ProtocolType = Msg:nodeType()
   if NodeType ~= 'message' or ProtocolType ~= 'hl7' then
      error('Expected hl7 message, got '..ProtocolType..' '..NodeType, 2)
   end
   local Serialized = tostring(Msg)
   local Delimiters = Params.delimiters or {'\r', '|', '^', '~', '\\', '&'}
   if Delimiters and #Delimiters ~= 6 then
      error('Expected delimiter list of size 6, got '..#Delimiters, 2)
   end
   for N,D in ipairs(Delimiters) do
      if #D ~= 1 then
         error('Delimiters must be exactly one character, "'..D..'" is '..#D, 2)
      end
   end
   local Escaped = Params.escaped or {'F', 'S', 'R', 'E', 'T'}
   trace(Escaped)
   if #Escaped ~= 5 then
      error('Expected escaped list of size 5, got '..#Escaped, 2)
   end
   
   local DelimiterMap = {
      ['\r']=Delimiters[1],
      ['|']=Delimiters[2],
      ['^']=Delimiters[3],
      ['~']=Delimiters[4],
      ['\\']=Delimiters[5],
      ['&']=Delimiters[6]
   }
   
   local UnescapeMap = {
      ['\\F\\']='|',
      ['\\S\\']='^', 
      ['\\R\\']='~',
      ['\\E\\']='\\',
      ['\\T\\']='&'
   }
   
   local EscapeNewDelimiterMap = {
      [Delimiters[1]]=Delimiters[5]..'X'..Delimiters[1]:byte(1)..Delimiters[5],
      [Delimiters[2]]=Delimiters[5]..Escaped[1]..Delimiters[5],
      [Delimiters[3]]=Delimiters[5]..Escaped[2]..Delimiters[5],
      [Delimiters[4]]=Delimiters[5]..Escaped[3]..Delimiters[5],
      [Delimiters[5]]=Delimiters[5]..Escaped[4]..Delimiters[5],
      [Delimiters[6]]=Delimiters[5]..Escaped[5]..Delimiters[5]
   }
   trace(EscapeNewDelimiterMap)
   
   local DelimiterPattern = '[\r|%^~\\&'..
   charToPattern(Delimiters[1])..
   charToPattern(Delimiters[2])..
   charToPattern(Delimiters[3])..
   charToPattern(Delimiters[4])..
   charToPattern(Delimiters[5])..
   charToPattern(Delimiters[6])..']'
   
   Serialized = search(Serialized, '\\[FSRET]\\',
      
      function(Unmatched)
         return Unmatched:gsub(DelimiterPattern,
            
            function(Match)
               return DelimiterMap[Match] or EscapeNewDelimiterMap[Match]
            end
         )
      end,
      
      function(Matched)
         local Unescaped = UnescapeMap[Matched]
         local NewEscape = EscapeNewDelimiterMap[Unescaped]
         if NewEscape then
            return NewEscape
         else
            return Unescaped
         end
      end)
   
   return Serialized
end

local hl7_serialize = {
   Title="hl7.serialize";
   Usage="hl7.serialize{data [, {delimeters}] [, {escapes}]",
   SummaryLine="Generate an HL7 message with non-standard delimiters",
   Desc=[[Generates an HL7 message with non-standard delimiters, and optionally non-standard
   escape characters (for embedded delimiters).
   <p>Serializes an HL7 node tree (message) using the supplied delimiters, if no delimiters are
   supplied the HL7 defaults are used. Non-standard escapes for embedded delimiters can also be 
   supplied, if no escapes are supplied it uses the HL7 defaults.
   <p><b>Note</b>: This function only works with HL7 messages.
   ]];
   ["Returns"] = {
      {Desc="The serialized HL7 message <u>string</u>."},
   };
   ParameterTable= true,
   Parameters= {
      {data= {Desc='An HL7 message (node tree) to be serialized <u>hl7 node tree</u>.'}},
      {delimiters= {Desc='A list of delimiters to use in the message <u>table</u>.'}},
      {escapes= {Desc='A list of delimiter escape characters <u>table</u>.'}},
   };
   Examples={
      [[   Msg = hl7.parse{vmd='demo.vmd', data=Data}
   
   hl7.serialize{data=Msg}
 
   hl7.serialize{data=Msg, 
      delimiters = {'\n', '#', '.', '&', '\'', '*'}}
 
   hl7.serialize{data=Msg, 
      escaped = {'A', 'B', 'C', 'D', 'E'}}
   
   hl7.serialize{data = Msg, 
      delimiters = {'\n', '&', '\\', '}', '~', '^'},
      escaped = {'A', 'B', 'C', 'D', 'E'}}
   
   hl7.serialize{data = Msg, 
      delimiters = {'A', '|', '^', '~', '\\', '&'}}
   ]],
   };
   SeeAlso={
      {
         Title="hl7.serialize.lua - in our code repository",
         Link="http://help.interfaceware.com/code/details/hl7-serialize-lua"
      },
      {
         Title="hl7.serialize",
         Link="http://help.interfaceware.com/v6/hl7-serialize"
      }
   }
}

help.set{input_function=hl7.serialize, help_data=hl7_serialize}
Description
Serializes an HL7 message using specified non-standard delimiters and/or escape characters
Attachments
serialize.zip
Usage Details

Have you ever needed to generate an HL7 message with non-standard delimiters?  Sometimes the receiving party is expecting their own flavor of HL7 with strange delimiters, so what do you do?  You can use this function, hl7.serialize{}, which will serialize an HL7 message with a provided set of delimiters (and/or a provided set of “escape characters”).

How to use hl7.serialize.lua:

  • Add the module to a Filter or To Translator script
    • You can also load the example project
  • Use hl7.serialize{} to serialize the code with the desired delimiters and/or escape characters
  • Inspect the code and annotations to see how it works

Here is some sample code for main():

-- This module goes into the hl7 namespace
require 'hl7.serialize'

-- The module allows us to tightly control what delimiters and escape sequences are used to encode an HL7 message
-- https://help.interfaceware.com/code/details/hl7-serialize-lua

function main(Data)
   Msg = hl7.parse{vmd='demo.vmd', data=Data}
   
   hl7.serialize{data=Msg}
 
   hl7.serialize{data=Msg, 
      delimiters = {'\n', '#', '.', '&', '\'', '*'}}
 
   hl7.serialize{data=Msg, 
      escaped = {'A', 'B', 'C', 'D', 'E'}}
   
   hl7.serialize{data = Msg, 
      delimiters = {'\n', '&', '\\', '}', '~', '^'},
      escaped = {'A', 'B', 'C', 'D', 'E'}}
   
   hl7.serialize{data = Msg, 
      delimiters = {'A', '|', '^', '~', '\\', '&'}}
end
More Information
• Serialize an HL7 messages with non-standard delimiters
• Using non-standard HL7 delimiters
Bookmark
  • Reviews
  • Related Listings
Filter
Sort by: Rating
  • Newest First
  • Oldest First
  • Helpfulness
Write a Review
Rating
Keyword
Filter
Sort by: Highest Rated
  • Newest First
  • Oldest First
  • Title
  • Most Reviews
Rating
iNTERFACEWARE
urlcode.lua
Added by iNTERFACEWARE
Modules
A module for parsing URL encoded GET/POST sequences
xml.lua
Added by iNTERFACEWARE
Modules
A collection of helpful XML node functions.
dup.lua
Added by iNTERFACEWARE
Modules
Duplicate message filter.
stringutil.lua
Added by iNTERFACEWARE
Modules
A library of helpful extensions to the standard Lua string library.
store.lua
Added by iNTERFACEWARE
Modules
The "original" store module: Allows you to store key/value pairs in a persistent storage mechanism. We recommend using the new store2 module instead.
sha1.lua
Added by iNTERFACEWARE
Modules
A pure Lua-based implementation of the popular SHA-1 hashing function.
retry.lua
Added by iNTERFACEWARE
Modules
A module for retrying operations which might periodically fail like database operations.
mime.lua
Added by iNTERFACEWARE
Modules
Sends MIME-encoded email attachments using the SMTP protocol. A wrapper around net.smtp.send.
iguanaServer.lua
Added by iNTERFACEWARE
Modules
Provides programmatic access to various operations that can be performed on Iguana channels.
hl7.findSegment.lua
Added by iNTERFACEWARE
Modules
A utility for finding any HL7 segment in a parsed HL7 message node tree.
dateparse.lua
Added by iNTERFACEWARE
Modules
A fuzzy date/time parser that is very useful for automatically translating a wide variety of date/time formats.
Showing 21 - 31 of 31 results
«12»

Topics

  • expandGetting Started
  • expandAdministration
    • expandInstallation
    • expandLicensing
    • expandUpgrades
    • expandDeployment
    • expandConfiguration Management
      • expandCustom Configuration
    • expandBackup and Restore
    • expandSecurity
      • expandHIPAA Compliance
    • expandTroubleshooting
  • expandDeveloping Interfaces
    • expandArchitecture
    • expandInterfaces
      • expandHL7
      • expandDatabase
        • expandConnect
      • expandWeb Services
      • expandCDA
      • expandX12
      • expandOther Interfaces
      • expandUtilities
    • expandRepositories
      • expandBuiltin Repositories
        • expandIguana Upgrade
        • expandIguana Tutorials
        • expandIguana Tools
        • expandIguana Protocols
        • expandIguana Files
        • expandIguana Date/Time
        • expandIguana Webservices
        • expandIguana Excel
      • expandRemote Repositories
      • expandCS Team Repositories
        • expandIguana Channels
    • expandSample Code
      • expandModules
      • expandUsing built-in functions
      • expandWorking with XML
    • expandLua Programming
    • expandPerformance
  • expandFAQs and TIPs
    • expandFrequently Asked Questions
      • expandInstalls and Upgrades
      • expandWeb Services
      • expandConfiguration
      • expandChannels
      • expandTranslator
      • expandOther
      • expandDatabase
      • expandAdministration
      • expandLogs
      • expandChameleon
    • expandTips
      • expandChannels
      • expandChameleon
      • expandWeb Services
      • expandSecurity
      • expandProgramming
      • expandOther
      • expandAdministration
  • expandReference
    • expandIguana Enterprise and Professional
    • expandProgram Settings
    • expandChannel Settings
    • expandDashboard
    • expandChannels
    • expandTranslator
    • expandLogs
      • expandLog Encryption
    • expandHTTP API
    • expandCDA API
    • expandError Messages
    • expandChameleon
    • expandIguana Change Log

Other Links

  • Training Center
  • News & Announcements
  • iNTERFACEWARE Blog
  • Older Documention (IGUANA v4 & Chameleon)
Copyright © iNTERFACEWARE Inc.