You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何反序列化StructureDefinition.Snapshot.Element列表(Hl7.Fhir.STU3)

问题描述

我需要处理NHS LFPSE的FHIR资源,目标是把StructureDefinition.Snapshot.Element列表存入数据库。序列化和数据库保存环节都正常,但反序列化时遇到报错:

var snapshot = JsonConvert.DeserializeObject<StructureDefinition.SnapshotComponent>(mySnapshotString);  

错误信息:

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize readonly or fixed size dictionary: Hl7.Fhir.Model.StructureDefinition+SnapshotComponent. Path 'element', line 1, position 11.'

我用的是Hl7.Fhir.STU3,还试过其他序列化库:

  • FhirJsonSerializer:找不到Deserialise方法
  • JsonSerializer:触发同样的错误

序列化后的快照元素JSON示例:

{
    "element": [
        {
            "id": "Extension",
            "path": {
                "value": "Extension"
            },
            "short": {
                "value": "Information about the estimated date/time of the event"
            },
            "definition": {
                "value": "Information about the estimated date/time of the event"
            },
            "min": {
                "value": 0
            },
            "max": {
                "value": "1"
            },
            "base": {
                "path": {
                    "value": "Element"
                },
                "min": {
                    "value": 0
                },
                "max": {
                    "value": "*"
                }
            },
            "condition": [
                {
                    "value": "ele-1"
                }
            ],
            "constraint": [
                {
                    "key": {
                        "value": "ele-1"
                    },
                    "severity": {
                        "value": "error"
                    },
                    "human": {
                        "value": "All FHIR elements must have a @value or children"
                    },
                    "expression": {
                        "value": "hasValue() | (children().count() > id.count())"
                    },
                    "xpath": {
                        "value": "@value|f:*|h:div"
                    },
                    "source": {
                        "value": "Element"
                    }
                },
                {
                    "key": {
                        "value": "ext-1"
                    },
                    "severity": {
                        "value": "error"
                    },
                    "human": {
                        "value": "Must have either extensions or value[x], not both"
                    },
                    "expression": {
                        "value": "extension.exists() != value.exists()"
                    },
                    "xpath": {
                        "value": "exists(f:extension)!=exists(f:*[starts-with(local-name(.), 'value')])"
                    }
                }
            ],
            "mapping": [
                {
                    "identity": {
                        "value": "rim"
                    },
                    "map": {
                        "value": "n/a"
                    }
                },
                {
                    "identity": {
                        "value": "rim"
                    },
                    "map": {
                        "value": "N/A"
                    }
                }
            ]
        },
        {
            "id": "Extension.id",
            "path": {
                "value": "Extension.id"
            },
            "representation": [
                {
                    "value": "xmlAttr"
                }
            ],
            "short": {
                "value": "xml:id (or equivalent in JSON)"
            },
            "definition": {
                "value": "unique id for the element within a resource (for internal references). This may be any string value that does not contain spaces."
            },
            "min": {
                "value": 0
            },
            "max": {
                "value": "1"
            },
            "base": {
                "path": {
                    "value": "Element.id"
                },
                "min": {
                    "value": 0
                },
                "max": {
                    "value": "1"
                }
            },
            "type": [
                {
                    "code": {
                        "value": "string"
                    }
                }
            ],
            "mapping": [
                {
                    "identity": {
                        "value": "rim"
                    },
                    "map": {
                        "value": "n/a"
                    }
                }
            ]
        },
        {
            "id": "Extension.extension",
            "path": {
                "value": "Extension.extension"
            },
            "slicing": {
                "discriminator": [
                    {
                        "type": {
                            "value": "value"
                        },
                        "path": {
                            "value": "url"
                        }
                    }
                ],
                "description": {
                    "value": "Extensions are always sliced by (at least) url"
                },
                "rules": {
                    "value": "open"
                }
            },
            "short": {
                "value": "Additional Content defined by implementations"
            },
            "definition": {
                "value": "May be used to represent additional information that is not part of the basic definition of the element. In order to make the use of extensions safe and manageable, there is a strict set of governance  applied to the definition and use of extensions. Though any implementer is allowed to define an extension, there is a set of requirements that SHALL be met as part of the definition of the extension."
            },
            "comment": {
                "value": "There can be no stigma associated with the use of extensions by any application, project, or standard - regardless of the institution or jurisdiction that uses or defines the extensions.  The use of extensions is what allows the FHIR specification to retain a core level of simplicity for everyone."
            },
            "alias": [
                {
                    "value": "extensions"
                },
                {
                    "value": "user content"
                }
            ],
            "min": {
                "value": 0
            },
            "max": {
                "value": "*"
            },
            "base": {
                "path": {
                    "value": "Element.extension"
                },
                "min": {
                    "value": 0
                },
                "max": {
                    "value": "*"
                }
            },
            "type": [
                {
                    "code": {
                        "value": "Extension"
                    }
                }
            ],
            "mapping": [
                {
                    "identity": {
                        "value": "rim"
                    },
                    "map": {
                        "value": "n/a"
                    }
                }
            ]
        },
        {
            "id": "Extension.extension:IncidentOccurredToday",
            "path": {
                "value": "Extension.extension"
            },
            "sliceName": {
                "value": "IncidentOccurredToday"
            },
            "short": {
                "value": "Indication of whether the event occurred today"
            },
            "definition": {
                "value": "Indication of whether the event occurred today"
            },
            "comment": {
                "value": "There can be no stigma associated with the use of extensions by any application, project, or standard - regardless of the institution or jurisdiction that uses or defines the extensions.  The use of extensions is what allows the FHIR specification to retain a core level of simplicity for everyone."
            },
            "alias": [
                {
                    "value": "extensions"
                },
                {
                    "value": "user content"
                }
            ],
            "min": {
                "value": 0
            },
            "max": {
                "value": "1"
            },
            "base": {
                "path": {
                    "value": "Element.extension"
                },
                "min": {
                    "value": 0
                },
                "max": {
                    "value": "*"
                }
            },
            "type": [
                {
                    "code": {
                        "value": "Extension"
                    }
                }
            ],
            "mapping": [
                {
                    "identity": {
                        "value": "rim"
                    },
                    "map": {
                        "value": "n/a"
                    }
                }
            ]
        },
        {
            "id": "Extension.extension:IncidentOccurredToday.id",
            "path": {
                "value": "Extension.extension.id"
            },
            "representation": [
                {
                    "value": "xmlAttr"
                }
            ],
            "short": {
                "value": "xml:id (or equivalent in JSON)"
            },
            "definition": {
                "value": "unique id for the element within a resource (for internal references). This may be any string value that does not contain spaces."
            },
            "min": {
                "value": 0
            },
            "max": {
                "value": "1"
            },
            "base": {
                "path": {
                    "value": "Element.id"
                },
                "min": {
                    "value": 0
                },
                "max": {
                    "value": "1"
                }
            },
            "type": [
                {
                    "code": {
                        "value": "string"
                    }
                }
            ],
            "mapping": [
                {
                    "identity": {
                        "value": "rim"
                    },
                    "map": {
                        "value": "n/a"
                    }
                }
            ]
        },
        {
            "id": "Extension.extension:IncidentOccurredToday.extension",
            "path": {
                "value": "Extension.extension.extension"
            },
            "slicing": {
                "discriminator": [
                    {
                        "type": {
                            "value": "value"
                        },
                        "path": {
                            "value": "url"
                        }
                    }
                ],
                "description": {
                    "value": "Extensions are always sliced by (at least) url"
                },
                "rules": {
                    "value": "open"
                }
            },
            "short": {
                "value": "Additional Content defined by implementations"
            },
            "definition": {
                "value": "May be used to represent additional information that is not part of the basic definition of the element. In order to make the use of extensions safe and manageable, there is a strict set of governance  applied to the definition and use of extensions. Though any implementer is allowed to define an extension, there is a set of requirements that SHALL be met as part of the definition of the extension."
            },
            "comment": {
                "value": "There can be no stigma associated with the use of extensions by any application, project, or standard - regardless of the institution or jurisdiction that uses or defines the extensions.  The use of extensions is what allows the FHIR specification to retain a core level of simplicity for everyone."
            },
            "alias": [
                {
                    "value": "extensions"
                },
                {
                    "value": "user content"
                }
            ],
            "min": {
                "value": 0
            },
            "max": {
                "value": "0"
            },
            "base": {
                "path": {
                    "value": "Element.extension"
                },
                "min": {
                    "value": 0
                },
                "max": {
                    "value": "*"
                }
            },
            "type": [
                {
                    "code": {
                        "value": "Extension"
                    }
                }
            ],
            "mapping": [
                {
                    "identity": {
                        "value": "rim"
                    },
                    "map": {
                        "value": "n/a"
                    }
                }
            ]
        },
        {
            "id": "Extension.extension:IncidentOccurredToday.url",
            "path": {
                "value": "Extension.extension.url"
            },
            "representation": [
                {
                    "value": "xmlAttr"
                }
            ],
            "short": {
                "value": "identifies the meaning of the extension"
            },
            "definition": {
                "value": "Source of the definition for the extension code - a logical name or a URL."
            },
            "comment": {
                "value": "The definition may point directly to a computable or human-readable definition of the extensibility codes, or it may be a logical URI as declared in some other specification. The definition SHALL be a URI for the Structure Definition defining the extension."
            },
            "min": {
                "value": 1
            },
            "max": {
                "value": "1"
            },
            "base": {
                "path": {
                    "value": "Extension.url"
                },
                "min": {
                    "value": 1
                },
                "max": {
                    "value": "1"
                }
            },
            "type": [
                {
                    "code": {
                        "value": "uri"
                    }
                }
            ],
            "fixed": {
                "value": "IncidentOccurredToday"
            },
            "mapping": [
                {
                    "identity": {
                        "value": "rim"
                    },
                    "map": {
                        "value": "N/A"
                    }
                }
            ]
        },
        {
            "id": "Extension.extension:IncidentOccurredToday.valueCode",
            "path": {
                "value": "Extension.extension.valueCode"
            },
            "short": {
                "value": "Value of extension"
            },
            "definition": {
                "value": "Value of extension - may be a resource or one of a constrained set of the data types (see Extensibility in the spec for list)."
            },
            "min": {
                "value": 0
            },
            "max": {
                "value": "1"
            },
            "base": {
                "path": {
                    "value": "Extension.value[x]"
                },
                "min": {
                    "value": 0
                },
                "max": {
                    "value": "1"
                }
            },
            "type": [
                {
                    "code": {
                        "value": "code"
                    }
                }
            ],
            "binding": {
                "strength": {
                    "value": "required"
                },
                "description": {
                    "value": "Indication of whether the event occurred today"
                },
                "valueSet": {
                    "reference": {
                        "value": "https://psims-uat.azure-api.net/taxonomy/fhir/ValueSet/yes-no-unknown-codes-4"
                    }
                }
            },
            "mapping": [
                {
                    "identity": {
                        "value": "rim"
                    },
                    "map": {
                        "value": "N/A"
                    }
                }
            ]
        }
    ]
}
解决方案

方法1:用FHIR官方序列化器直接解析元素列表

STU3的FhirJsonSerializer没有Deserialise方法,但可以用Parse逐元素解析。直接把JSON里的element数组解析成List<ElementDefinition>:

var serializer = new FhirJsonSerializer(new SerializerSettings { Format = Format.Json });
using var stringReader = new StringReader(mySnapshotString);
using var jsonReader = new JsonTextReader(stringReader);

// 定位到element数组
jsonReader.Read(); // 读取对象起始
jsonReader.Read(); // 读取"element"属性名
jsonReader.Read(); // 读取数组起始

var elements = new List<ElementDefinition>();
while (jsonReader.TokenType != JsonToken.EndArray)
{
    var element = serializer.Parse<ElementDefinition>(jsonReader);
    elements.Add(element);
    jsonReader.Read();
}

// elements就是你要的快照元素列表

方法2:自定义Newtonsoft.Json转换器绕过只读字典限制

如果非要用Newtonsoft.Json,写个转换器手动处理SnapshotComponent的反序列化:

public class SnapshotComponentConverter : JsonConverter<StructureDefinition.SnapshotComponent>
{
    public override StructureDefinition.SnapshotComponent ReadJson(JsonReader reader, Type objectType, StructureDefinition.SnapshotComponent existingValue, bool hasExistingValue, JsonSerializer serializer)
    {
        var jsonObj = JObject.Load(reader);
        var snapshot = new StructureDefinition.SnapshotComponent();
        
        // 手动反序列化element列表并添加到SnapshotComponent
        var elements = jsonObj["element"].ToObject<List<ElementDefinition>>(serializer);
        foreach (var elem in elements)
        {
            snapshot.Element.Add(elem);
        }
        
        return snapshot;
    }

    public override void WriteJson(JsonWriter writer, StructureDefinition.SnapshotComponent value, JsonSerializer serializer)
    {
        serializer.Serialize(writer, value);
    }
}

// 使用方式
var settings = new JsonSerializerSettings();
settings.Converters.Add(new SnapshotComponentConverter());
var snapshot = JsonConvert.DeserializeObject<StructureDefinition.Snapshot
相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 00:30:53