/// <summary>
        /// Parse Extension
        /// </summary>
        public static Hl7.Fhir.Model.Extension ParseExtension(IFhirReader reader, ErrorList errors, Hl7.Fhir.Model.Extension existingInstance = null)
        {
            Hl7.Fhir.Model.Extension result = existingInstance != null ? existingInstance : new Hl7.Fhir.Model.Extension();
            string currentElementName       = reader.CurrentElementName;

            reader.EnterElement();

            while (reader.HasMoreElements())
            {
                var atName = reader.CurrentElementName;
                // Parse element extension
                if (atName == "extension")
                {
                    result.Extension = new List <Hl7.Fhir.Model.Extension>();
                    reader.EnterArray();

                    while (ParserUtils.IsAtArrayElement(reader, "extension"))
                    {
                        result.Extension.Add(ExtensionParser.ParseExtension(reader, errors));
                    }

                    reader.LeaveArray();
                }

                // Parse element _id
                else if (atName == "_id")
                {
                    result.LocalIdElement = Id.Parse(reader.ReadPrimitiveContents(typeof(Id)));
                }

                // Parse element url
                else if (atName == "url")
                {
                    result.UrlElement = FhirUriParser.ParseFhirUri(reader, errors);
                }

                // Parse element isModifier
                else if (atName == "isModifier")
                {
                    result.IsModifierElement = FhirBooleanParser.ParseFhirBoolean(reader, errors);
                }

                // Parse element value
                else if (atName.StartsWith("value"))
                {
                    result.Value = FhirParser.ParseElement(reader, errors);
                }

                else
                {
                    errors.Add(String.Format("Encountered unknown element {0} while parsing {1}", reader.CurrentElementName, currentElementName), reader);
                    reader.SkipSubElementsFor(currentElementName);
                    result = null;
                }
            }

            reader.LeaveElement();
            return(result);
        }
        /// <summary>
        /// Add an extension with the given uri and value.
        /// </summary>
        /// <param name="extendable"></param>
        /// <param name="uri"></param>
        /// <param name="value"></param>
        /// <returns>The newly added Extension</returns>
        public static Extension AddExtension(this IExtendable extendable, Uri uri, Element value)
        {
            if (extendable.Extension == null)
                extendable.Extension = new List<Extension>();

            var newExtension = new Extension() { Url = uri, Value = value };
            extendable.Extension.Add(newExtension);

            return newExtension;
        }
        public static void SerializeExtension(Hl7.Fhir.Model.Extension value, IFhirWriter writer, bool summary)
        {
            writer.WriteStartComplexContent();

            // Serialize element _id
            if (value.LocalIdElement != null)
            {
                writer.WritePrimitiveContents("_id", value.LocalIdElement, XmlSerializationHint.Attribute);
            }

            // Serialize element extension
            if (value.Extension != null && !summary && value.Extension.Count > 0)
            {
                writer.WriteStartArrayElement("extension");
                foreach (var item in value.Extension)
                {
                    writer.WriteStartArrayMember("extension");
                    ExtensionSerializer.SerializeExtension(item, writer, summary);
                    writer.WriteEndArrayMember();
                }
                writer.WriteEndArrayElement();
            }

            // Serialize element url
            if (value.UrlElement != null)
            {
                writer.WriteStartElement("url");
                FhirUriSerializer.SerializeFhirUri(value.UrlElement, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element isModifier
            if (value.IsModifierElement != null)
            {
                writer.WriteStartElement("isModifier");
                FhirBooleanSerializer.SerializeFhirBoolean(value.IsModifierElement, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element value
            if (value.Value != null)
            {
                writer.WriteStartElement(SerializationUtil.BuildPolymorphicName("value", value.Value.GetType()));
                FhirSerializer.SerializeElement(value.Value, writer, summary);
                writer.WriteEndElement();
            }


            writer.WriteEndComplexContent();
        }
        private void AddRaceCode(Patient patient, XElement element)
        {
            const string url = "http://hl7.org/fhir/us/core/StructureDefinition/us-core-race";

            var race = FromXml(new CodingParser(), element);

            if (race == null)
            {
                return;
            }

            // Remove display text
            var display = race.Display;

            race.Display = null;

            var raceExtension = patient.Extension.FirstOrDefault(e => e.Url == url);


            if (raceExtension == null)
            {
                raceExtension = new Hl7.Fhir.Model.Extension {
                    Url = url
                };
                patient.Extension.Add(raceExtension);
            }

            switch (race.Code)
            {
            case "1002-5":
            case "2028-9":
            case "2054-5":
            case "2076-8":
            case "2106-3":
            case "UNK":
            case "ASKU":
                raceExtension.AddExtension("ombCategory", race);
                break;

            default:
                raceExtension.AddExtension("detailed", race);
                break;
            }

            if (raceExtension.Extension.All(e => e.Url != "text"))
            {
                raceExtension.AddExtension("text", new FhirString(display));
            }
        }
        private void AddEthnicGroupCode(Patient patient, XElement element)
        {
            const string url = "http://hl7.org/fhir/us/core/StructureDefinition/us-core-ethnicity";

            var ethnicity = FromXml(new CodingParser(), element);

            if (ethnicity == null)
            {
                return;
            }

            var display = ethnicity.Display;

            ethnicity.Display = null;

            var ethnicityExtension = patient.Extension.FirstOrDefault(e => e.Url == url);


            if (ethnicityExtension == null)
            {
                ethnicityExtension = new Hl7.Fhir.Model.Extension {
                    Url = url
                };
                patient.Extension.Add(ethnicityExtension);
            }

            switch (ethnicity.Code)
            {
            case "2135-2":
            case "2186-5":
                ethnicityExtension.AddExtension("ombCategory", ethnicity);
                break;

            default:
                ethnicityExtension.AddExtension("detail", ethnicity);
                break;
            }

            if (ethnicityExtension.Extension.All(e => e.Url != "text"))
            {
                ethnicityExtension.AddExtension("text", new FhirString(display));
            }
        }
Beispiel #6
0
        /// <summary>
        /// Deserialize JSON into a FHIR BackboneElement
        /// </summary>
        public static void DeserializeJsonProperty(this BackboneElement current, ref Utf8JsonReader reader, JsonSerializerOptions options, string propertyName)
        {
            switch (propertyName)
            {
            case "modifierExtension":
                if ((reader.TokenType != JsonTokenType.StartArray) || (!reader.Read()))
                {
                    throw new JsonException($"BackboneElement error reading 'modifierExtension' expected StartArray, found {reader.TokenType}! depth: {reader.CurrentDepth}, pos: {reader.BytesConsumed}");
                }

                current.ModifierExtension = new List <Extension>();

                while (reader.TokenType != JsonTokenType.EndArray)
                {
                    Hl7.Fhir.Model.Extension v_ModifierExtension = new Hl7.Fhir.Model.Extension();
                    v_ModifierExtension.DeserializeJson(ref reader, options);
                    current.ModifierExtension.Add(v_ModifierExtension);

                    if (!reader.Read())
                    {
                        throw new JsonException($"BackboneElement error reading 'modifierExtension' array, read failed! depth: {reader.CurrentDepth}, pos: {reader.BytesConsumed}");
                    }
                    if (reader.TokenType == JsonTokenType.EndObject)
                    {
                        reader.Read();
                    }
                }

                if (current.ModifierExtension.Count == 0)
                {
                    current.ModifierExtension = null;
                }
                break;

            // Complex: BackboneElement, Export: BackboneElement, Base: Element
            default:
                ((Hl7.Fhir.Model.Element)current).DeserializeJsonProperty(ref reader, options, propertyName);
                break;
            }
        }
Beispiel #7
0
        /// <summary>
        /// Deserialize JSON into a FHIR Element
        /// </summary>
        public static void DeserializeJsonProperty(this Element current, ref Utf8JsonReader reader, JsonSerializerOptions options, string propertyName)
        {
            switch (propertyName)
            {
            case "id":
                current.ElementId = reader.GetString();
                break;

            case "extension":
                if ((reader.TokenType != JsonTokenType.StartArray) || (!reader.Read()))
                {
                    throw new JsonException($"Element error reading 'extension' expected StartArray, found {reader.TokenType}! depth: {reader.CurrentDepth}, pos: {reader.BytesConsumed}");
                }

                current.Extension = new List <Extension>();

                while (reader.TokenType != JsonTokenType.EndArray)
                {
                    Hl7.Fhir.Model.Extension v_Extension = new Hl7.Fhir.Model.Extension();
                    v_Extension.DeserializeJson(ref reader, options);
                    current.Extension.Add(v_Extension);

                    if (!reader.Read())
                    {
                        throw new JsonException($"Element error reading 'extension' array, read failed! depth: {reader.CurrentDepth}, pos: {reader.BytesConsumed}");
                    }
                    if (reader.TokenType == JsonTokenType.EndObject)
                    {
                        reader.Read();
                    }
                }

                if (current.Extension.Count == 0)
                {
                    current.Extension = null;
                }
                break;
            }
        }
        /// <summary>
        /// Deserialize JSON into a FHIR DomainResource
        /// </summary>
        public static void DeserializeJsonProperty(this DomainResource current, ref Utf8JsonReader reader, JsonSerializerOptions options, string propertyName)
        {
            switch (propertyName)
            {
            case "text":
                current.Text = new Hl7.Fhir.Model.Narrative();
                ((Hl7.Fhir.Model.Narrative)current.Text).DeserializeJson(ref reader, options);
                break;

            case "contained":
                if ((reader.TokenType != JsonTokenType.StartArray) || (!reader.Read()))
                {
                    throw new JsonException($"DomainResource error reading 'contained' expected StartArray, found {reader.TokenType}! depth: {reader.CurrentDepth}, pos: {reader.BytesConsumed}");
                }

                current.Contained = new List <Resource>();

                while (reader.TokenType != JsonTokenType.EndArray)
                {
                    current.Contained.Add(JsonStreamResourceConverter.PolymorphicRead(ref reader, typeof(Resource), options));

                    if (!reader.Read())
                    {
                        throw new JsonException($"DomainResource error reading 'contained' array, read failed! depth: {reader.CurrentDepth}, pos: {reader.BytesConsumed}");
                    }
                    if (reader.TokenType == JsonTokenType.EndObject)
                    {
                        reader.Read();
                    }
                }

                if (current.Contained.Count == 0)
                {
                    current.Contained = null;
                }
                break;

            case "extension":
                if ((reader.TokenType != JsonTokenType.StartArray) || (!reader.Read()))
                {
                    throw new JsonException($"DomainResource error reading 'extension' expected StartArray, found {reader.TokenType}! depth: {reader.CurrentDepth}, pos: {reader.BytesConsumed}");
                }

                current.Extension = new List <Extension>();

                while (reader.TokenType != JsonTokenType.EndArray)
                {
                    Hl7.Fhir.Model.Extension v_Extension = new Hl7.Fhir.Model.Extension();
                    v_Extension.DeserializeJson(ref reader, options);
                    current.Extension.Add(v_Extension);

                    if (!reader.Read())
                    {
                        throw new JsonException($"DomainResource error reading 'extension' array, read failed! depth: {reader.CurrentDepth}, pos: {reader.BytesConsumed}");
                    }
                    if (reader.TokenType == JsonTokenType.EndObject)
                    {
                        reader.Read();
                    }
                }

                if (current.Extension.Count == 0)
                {
                    current.Extension = null;
                }
                break;

            case "modifierExtension":
                if ((reader.TokenType != JsonTokenType.StartArray) || (!reader.Read()))
                {
                    throw new JsonException($"DomainResource error reading 'modifierExtension' expected StartArray, found {reader.TokenType}! depth: {reader.CurrentDepth}, pos: {reader.BytesConsumed}");
                }

                current.ModifierExtension = new List <Extension>();

                while (reader.TokenType != JsonTokenType.EndArray)
                {
                    Hl7.Fhir.Model.Extension v_ModifierExtension = new Hl7.Fhir.Model.Extension();
                    v_ModifierExtension.DeserializeJson(ref reader, options);
                    current.ModifierExtension.Add(v_ModifierExtension);

                    if (!reader.Read())
                    {
                        throw new JsonException($"DomainResource error reading 'modifierExtension' array, read failed! depth: {reader.CurrentDepth}, pos: {reader.BytesConsumed}");
                    }
                    if (reader.TokenType == JsonTokenType.EndObject)
                    {
                        reader.Read();
                    }
                }

                if (current.ModifierExtension.Count == 0)
                {
                    current.ModifierExtension = null;
                }
                break;

            // Complex: DomainResource, Export: DomainResource, Base: Resource
            default:
                ((Hl7.Fhir.Model.Resource)current).DeserializeJsonProperty(ref reader, options, propertyName);
                break;
            }
        }
Beispiel #9
0
 public static Criterium Parse(Extension parameter)
 {
     return Parse(Query.ExtractParamKey(parameter), Query.ExtractParamValue(parameter));
 }
Beispiel #10
0
        internal static bool IsReserved(this Extension parameter)
        {
            string key = Query.ExtractParamKey(parameter).Split(new char[] { Query.SEARCH_MODIFIERSEPARATOR }).First();

            return(Query.RESERVED_PARAMETERS.Contains(key));
        }
        public void PolymorphAndArraySerialization()
        {
            Extension ext = new Extension()
                {
                    Url = new Uri("http://hl7.org/fhir/profiles/@3141#test"),
                    Value = new FhirBoolean(true),
                    Extension = new List<Extension>()
                        {
                            new Extension()
                            {
                                Value = new Coding() { Code = "R51", System = new Uri("http://hl7.org/fhir/sid/icd-10") }
                            }
                        }
                };

            Assert.AreEqual(@"<?xml version=""1.0"" encoding=""utf-16""?>" +
                @"<element xmlns=""http://hl7.org/fhir"">" +
                @"<extension><valueCoding><system value=""http://hl7.org/fhir/sid/icd-10"" /><code value=""R51"" /></valueCoding></extension>" +
                @"<url value=""http://hl7.org/fhir/profiles/@3141#test"" />" +
                @"<valueBoolean value=""true"" />" +
                @"</element>", FhirSerializer.SerializeElementAsXml(ext));
            Assert.AreEqual(
                @"{" +
                @"""extension"":[{""valueCoding"":{""system"":{""value"":""http://hl7.org/fhir/sid/icd-10""},""code"":{""value"":""R51""}}}]," +
                @"""url"":{""value"":""http://hl7.org/fhir/profiles/@3141#test""}," +
                @"""valueBoolean"":{""value"":true}" +
                @"}", FhirSerializer.SerializeElementAsJson(ext));
        }