从 FHIR JSON 输出中删除不需要的值属性?

问题描述 投票:0回答:1

我正在开发一个 .NET Core 应用程序,在其中根据数据库的结果创建 FHIR 资源。但是,在为 FHIR 包生成 JSON 响应时,我在输出中得到了不需要的值结构。我想完全删除这些值级别,以便 JSON 响应看起来像没有嵌套值字段的标准 FHIR 响应。

当前 JSON 输出:

{
  "id": {
    "value": "bundle-example"
  },
  "type": {
    "value": "searchset"
  },
  "total": {
    "value": 5
  },
  "entry": [
    {
      "fullUrl": {
        "value": "http://example.org/fhir/Observation/8393939"
      },
      "resource": {
        "id": {
          "value": "8393939"
        },
        "status": {
          "value": "final"
        },
        "code": {
          "coding": [
            {
              "system": {
                "value": "http://loinc.org"
              },
              "code": {
                "value": "1988-5"
              },
              "display": {
                "value": "CRP"
              }
            }
          ]
        }
        // ... other fields
      }
    }
    // ... other entries
  ]
}

所需的 JSON 结构:

{
  "resourceType": "Bundle",
  "id": "bundle-example",
  "type": "searchset",
  "total": 5,
  "entry": [
    {
      "fullUrl": "http://example.org/fhir/Observation/8393939",
      "resource": {
        "resourceType": "Observation",
        "id": "8393939",
        "status": "final",
        "code": {
          "coding": [
            {
              "system": "http://loinc.org",
              "code": "1988-5",
              "display": "CRP"
            }
          ]
        }
        // ... other fields
      }
    }
    // ... other entries
  ]
}

这是我的 ObservationService 类中负责生成 FHIR 包的相关代码:

using Hl7.Fhir.Model;
using System;
using System.Data;
using System.Collections.Generic;
using FHIR.PoC.Data;

namespace FHIR.PoC.Service
{
    public class ObservationService
    {
        private readonly SpecimenRepository m_specimenRepository;

        public ObservationService(SpecimenRepository specimenRepository)
        {
            m_specimenRepository = specimenRepository;
        }

        public Bundle MapToObservations(DataTable dtDbResults, bool bIncludeSpecimen)
        {
            var bundle = new Bundle
            {
                Id = "bundle-example",
                Type = Bundle.BundleType.Searchset,
                Total = dtDbResults.Rows.Count,
                Entry = new List<Bundle.EntryComponent>()
            };

            foreach (DataRow drRow in dtDbResults.Rows)
            {
                var observation = new Observation
                {
                    Id = drRow["zissample_id"].ToString() ?? "null",
                    Status = drRow["ZISRESULTSTATUS_ID"].ToString() == "8000013" ? ObservationStatus.Final : ObservationStatus.Preliminary,
                    Code = new CodeableConcept
                    {
                        Coding = new List<Coding>
                        {
                            new Coding
                            {
                                System = "http://loinc.org",
                                Code = drRow["LOINC_CODE"] != DBNull.Value ? drRow["LOINC_CODE"].ToString() : null,
                                Display = drRow["MEASURE_CODE"] != DBNull.Value ? drRow["MEASURE_CODE"].ToString() : null
                            }
                        }
                    },
                    Subject = new ResourceReference($"Patient/{drRow["SOURCE_ID"].ToString()}"),
                    Effective = GetEffectiveDate(drRow),
                    Issued = drRow["FIRSTREPORTDATETIME"] != DBNull.Value ? (DateTimeOffset?)Convert.ToDateTime(drRow["FIRSTREPORTDATETIME"]) : null,
                    Performer = new List<ResourceReference>
                    {
                        new ResourceReference("Practitioner/VasteWaardeLabTrain")
                    },
                    Value = GetObservationValue(drRow)
                };

                ProcessReferenceRangeIfPresent(drRow, observation);

                if (bIncludeSpecimen)
                {
                    ProcessSpecimen(drRow, observation);
                }

                bundle.Entry.Add(new Bundle.EntryComponent
                {
                    FullUrl = $"http://example.org/fhir/Observation/{observation.Id}",
                    Resource = observation
                });
            }

            return bundle;
        }

我当前正在设置 FHIR 资源的属性,但 JSON 响应仍然包含值结构。我还尝试直接分配值而不将它们包装在对象中,但结果保持不变。

如何从 FHIR 观察资源生成的 JSON 响应中完全删除值级别?我应该在代码中调整哪些内容以确保响应具有正确的结构,如上面所需的 JSON 结构所示?

c# json hl7-fhir hapi-fhir
1个回答
0
投票

我猜你正在使用“常规”Json 序列化器,而不是 FHIR 序列化器。

使用 Hl7.Fhir.Serialization;

FhirJson序列化器

简单的例子:

private static void PrettyPrintBundle(Bundle bundle)
{
    FhirJsonSerializer serializer = new FhirJsonSerializer(new SerializerSettings()
    {
        Pretty = true
    });

    Console.WriteLine(serializer.SerializeToString(bundle));
}
© www.soinside.com 2019 - 2024. All rights reserved.