Beispiel #1
0
        /// <summary>
        /// Deserialize JSON into a FHIR Bundle#Link
        /// </summary>
        public static void DeserializeJson(this Bundle.LinkComponent current, ref Utf8JsonReader reader, JsonSerializerOptions options)
        {
            string propertyName;

            while (reader.Read())
            {
                if (reader.TokenType == JsonTokenType.EndObject)
                {
                    return;
                }

                if (reader.TokenType == JsonTokenType.PropertyName)
                {
                    propertyName = reader.GetString();
                    if (Hl7.Fhir.Serialization.FhirSerializerOptions.Debug)
                    {
                        Console.WriteLine($"Bundle.LinkComponent >>> Bundle#Link.{propertyName}, depth: {reader.CurrentDepth}, pos: {reader.BytesConsumed}");
                    }
                    reader.Read();
                    current.DeserializeJsonProperty(ref reader, options, propertyName);
                }
            }

            throw new JsonException($"Bundle.LinkComponent: invalid state! depth: {reader.CurrentDepth}, pos: {reader.BytesConsumed}");
        }
Beispiel #2
0
        /// <summary>
        /// Deserialize JSON into a FHIR Bundle#Link
        /// </summary>
        public static void DeserializeJsonProperty(this Bundle.LinkComponent current, ref Utf8JsonReader reader, JsonSerializerOptions options, string propertyName)
        {
            switch (propertyName)
            {
            case "relation":
                if (reader.TokenType == JsonTokenType.Null)
                {
                    current.RelationElement = new FhirString();
                    reader.Skip();
                }
                else
                {
                    current.RelationElement = new FhirString(reader.GetString());
                }
                break;

            case "_relation":
                if (current.RelationElement == null)
                {
                    current.RelationElement = new FhirString();
                }
                ((Hl7.Fhir.Model.Element)current.RelationElement).DeserializeJson(ref reader, options);
                break;

            case "url":
                if (reader.TokenType == JsonTokenType.Null)
                {
                    current.UrlElement = new FhirUri();
                    reader.Skip();
                }
                else
                {
                    current.UrlElement = new FhirUri(reader.GetString());
                }
                break;

            case "_url":
                if (current.UrlElement == null)
                {
                    current.UrlElement = new FhirUri();
                }
                ((Hl7.Fhir.Model.Element)current.UrlElement).DeserializeJson(ref reader, options);
                break;

            // Complex: link, Export: LinkComponent, Base: BackboneElement
            default:
                ((Hl7.Fhir.Model.BackboneElement)current).DeserializeJsonProperty(ref reader, options, propertyName);
                break;
            }
        }
Beispiel #3
0
        /// <summary>
        /// Serialize a FHIR Bundle#Link into JSON
        /// </summary>
        public static void SerializeJson(this Bundle.LinkComponent current, Utf8JsonWriter writer, JsonSerializerOptions options, bool includeStartObject = true)
        {
            if (includeStartObject)
            {
                writer.WriteStartObject();
            }
            // Component: Bundle#Link, Export: LinkComponent, Base: BackboneElement (BackboneElement)
            ((Hl7.Fhir.Model.BackboneElement)current).SerializeJson(writer, options, false);

            writer.WriteString("relation", current.RelationElement.Value);

            writer.WriteString("url", current.UrlElement.Value);

            if (includeStartObject)
            {
                writer.WriteEndObject();
            }
        }
        //TODO: test this
        public Bundle ToBundle <T>(FhirRequest request, List <T> resources, Guid?bundleId) where T : Resource
        {
            var meta = new Meta();

            meta.LastUpdated = DateTime.UtcNow;

            var selfLink = new Bundle.LinkComponent();

            selfLink.Relation = "_self";
            selfLink.Url      = request.RequestUrl.AbsoluteUri;

            var links = new List <Bundle.LinkComponent>();

            links.Add(selfLink);

            var entryResources = request.IsSummary ? new List <T>() : resources;
            var entries        = new List <Bundle.EntryComponent>();

            entryResources.ForEach((r) =>
            {
                var urlPort = new int[] { 80, 443 }.Contains(request.RequestUrl.Port) ? "" : ":" + request.RequestUrl.Port;

                var search  = new Bundle.SearchComponent();
                search.Mode = Bundle.SearchEntryMode.Match;

                var entry      = new Bundle.EntryComponent();
                entry.Search   = search;
                entry.FullUrl  = $"{request.RequestUrl.Scheme}://{request.RequestUrl.Host}{urlPort}/nrls-ri/{request.ResourceType}/{r.Id}";
                entry.Resource = r;

                entries.Add(entry);
            });

            var bundle = new Bundle();

            bundle.Id    = $"{(bundleId.HasValue ? bundleId.Value : Guid.NewGuid())}";
            bundle.Meta  = meta;
            bundle.Type  = Bundle.BundleType.Searchset;
            bundle.Total = resources.Count;
            bundle.Link  = links;
            bundle.Entry = entries;

            return(bundle);
        }