提问人:YHapticY 提问时间:11/16/2023 最后编辑:Mark RotteveelYHapticY 更新时间:11/19/2023 访问量:75
高效地将大型 JSON 数据封送到具有自定义类型的结构中
Efficiently marshaling large JSON data into structs with custom types
问:
我正在开发一个 Go 应用程序,我需要有效地将大型 JSON 数据集编组到结构中。数据集包含复杂的嵌套结构和自定义类型。我在处理特别大的 JSON 文件时遇到了性能问题。
JSON结构如下(为简洁起见进行了简化):
{
"data": [
{
"id": "123",
"attributes": {
"name": "Example",
"details": {
"type": "customType",
"info": "Some info"
}
}
}
// More items...
]
}
我已经定义了相应的 Go 结构,包括用于处理特定字段的自定义类型:
type DataSet struct {
Data []DataItem `json:"data"`
}
type DataItem struct {
ID string `json:"id"`
Attributes Attributes `json:"attributes"`
}
type Attributes struct {
Name string `json:"name"`
Details Detail `json:"details"`
}
type Detail struct {
Type CustomType `json:"type"`
Info string `json:"info"`
}
// CustomType with custom unmarshaling logic
type CustomType string
func (ct *CustomType) UnmarshalJSON(b []byte) error {
// Custom unmarshaling logic
}
使用 json 封送大型数据集时会出现此问题。解封功能,导致内存使用率高,性能下降。
Go 中是否有更有效的技术来处理具有自定义解组逻辑的大型 JSON 数据集?
是否可以优化结构设计以提高 JSON 解组的性能?
Go 版本:1.18。JSON 文件的大小范围从 100 MB 到 1 GB 不等。我考虑过使用流式 JSON 解析器,但我不确定如何与自定义类型集成。
我尝试了以下方法。
- 标准 json。使用提供的结构设置取消编组。
- 尝试减少嵌套结构的深度。
答: 暂无答案
评论