Skip to content
Jake Aitchison edited this page Jan 10, 2022 · 3 revisions

As of nHapi version 3.1.0 we introduced ParserOptions which lets you configure the following things when parsing a message.

DefaultObx2Type

Default behaviour for OBX-2

In a normal OBX (Observation) segment, OBX-2 provides the datatype to be applied to OBX-5. So, for instance if an OBX repetition was conveying a timestamp (TS), it might look like this.

OBX||TS|||200901011259

If the OBX-2 value is missing (which sometimes happens because of a misbehaving sending system), you may see an error such as

OBX-5 is valued, but OBX-2 is not. A datatype for OBX-5 must be specified using OBX-2.

In this case, you may specify that nHapi should use a default type when none is found by specifying a default OBX-2 type using the ParserOptions.

var parser = new PipeParser();
var options = new ParserOptions { DefaultObx2Type = "ST" };

var parsed = parser.Parse(message, options);

If this property is set, the value provides a default datatype (ST, NM, etc) for an OBX segment with a missing OBX-2 value. This is useful when parsing messages from systems which do not correctly populate OBX-2.

For example, if this property is set to ST, and the following OBX segment is encountered:

OBX|||||This is a value

It will be parsed as though it had read:

OBX||ST|||This is a value

If the OBX-2 value exists but is invalid, meaning that it has a value but that value is not a valid HL7 datatype (e.g. String instead of ST), you may specify a default type to assume if the OBX-2 value cannot be understood, see the option below.

InvalidObx2Type

If this property is set, the value provides a default datatype (ST, NM, etc) for an OBX segment with an invalid OBX-2 value. This is useful when parsing messages from systems which do not correctly populate OBX-2.

For example, if this property is set to ST, and the following OBX segment is encountered:

OBX||INVALID|||This is a value

It will be parsed as though it had read:

OBX||ST|||This is a value

var parser = new PipeParser();
var options = new ParserOptions { InvalidObx2Type = "ST" };

var parsed = parser.Parse(message, options);

UnexpectedSegmentBehaviour

If you are processing messages with nonstandard segments (e.g. Z-segments) or segments which appear in a nonstandard location, the parser by default will use the AddInline option and add them where it finds them.

UnspectedSegmentBehaviour has 3 options.

Option Description
AddInline (Default) Add the segment as a nonstandard segment at the current location, even if the current location is in a child group within the message. You can read more about this here with examples.
DropToRoot Return the parser back to the root of the message (even if the last segment was in a group) and add the unexpected segment as a nonstandard segment.
ThrowHl7Exception Throw an HL7Exception

example usage.

var parser = new PipeParser();
var options = new ParserOptions
{
    UnexpectedSegmentBehaviour = UnexpectedSegmentBehaviour.DropToRoot
};

var msg = parser.Parse(message, options);

NonGreedyMode

Used to give the parser hints on how to proceed when the choice of where to put the next segment is ambiguous between moving to a child group later in the message structure or moving to a new repetition of a parent structure.

If set to true (default is false), pipe parser will be put in non-greedy mode. This setting applies only to the PipeParser and will have no effect on the XmlParser.

In non-greedy mode, if the message structure being parsed has an ambiguous choice of where to put a segment because there is a segment matching the current segment name in both a later position in the message, and in an earlier position as a part of a repeating group, the earlier position will be chosen.

This mode is useful for example when parsing OML^O21 messages containing multiple orders.

MSH
GROUP_1 (start)
{
   AAA
   BBB
   GROUP_2 (start)
   {
      AAA
   }
   GROUP_2 (end)
}
GROUP_1 (end)

For the above example, consider a message containing the following segments:

  • MSH
  • AAA
  • BBB
  • AAA

In this example, when the second AAA segment is encountered, there are two possible choices. It would be placed in GROUP_2, or it could be placed in a second repetition of GROUP_1. By default, it will be placed in GROUP_2, but in non-greedy mode it will be put in a new repetition of GROUP_1.

var parser = new PipeParser();
var options = new ParserOptions { NonGreedyMode = true };

var parsed = parser.Parse(message, options);
Clone this wiki locally