簡體   English   中英

C#/ MongoDB對字典數組的數組進行反序列化

[英]C# / MongoDB Deserialization of Array of Array of Dictionaries

我正在嘗試使用MongoDB在ASP.NET Core中創建字典數組的數組。 最后,數據應保留此模型以用於所有用途:

模型

{
    "ContextName": "Base rates",
    "Indexes": [
        {
            "IndexName": "S&P",
            "IndexValues" : [
                {
                    "Date": "2019-01-01",
                    "Value": "2600.98"
                },
                {
                    "Date": "2019-01-02",
                    "Value": "2605.98"
                }              
            ]
        }
    ]
}

當前,我在下面定義了第一層,包括ContextNameIndexes

Context.cs

public class Context
    {
        [BsonId]
        [BsonRepresentation(MongoDB.Bson.BsonType.ObjectId)]
        public string Id { get; set; }
        [BsonElement("ContextName")]
        public string ContextName { get; set; }
        [BsonElement("Indexes")]
        public ICollection<Index> Indexes { get; set; }
    }

Index具有以下結構:

Index.cs

public class Index
    {
        [BsonElement("IndexName")]
        public string IndexName { get; set; }
        [BsonElement("IndexValues")]
        [BsonDictionaryOptions(DictionaryRepresentation.ArrayOfDocuments)]
        public Dictionary<DateTime, double> IndexValues { get; set; }
    }

運行此代碼,遇到格式異常說明

'反序列化類Database.Index的IndexValues屬性時發生錯誤:無效元素:'Date'。

我的第一個想法是這是一個問題,該問題的關鍵是Date類型而不是數據庫本身內的string ,但是我在這里已經讀到底部 ,官方驅動程序實際上支持此問題。

我進一步嘗試嘗試設置一些自定義{ get; set; } { get; set; } { get; set; }以字符串形式輸入密鑰,但最后卻遇到相同的錯誤。

public Dictionary<DateTime, double> IndexValues
        {
            get
            {
                Dictionary<DateTime, double> _indexValues = new Dictionary<DateTime, double>();
                foreach (var indexValue in IndexValues)
                {
                    _indexValues.Add(indexValue.Key, indexValue.Value);
                }
                return _indexValues;
            }
            set { IndexValues = IndexValues; } // Unsure about here as well
        }

我不確定在這種情況下要看什么,並且正在尋找一些提示來解決這個問題。 提前致謝。

更新資料

使用外部方法格式化字典可以使用正確的格式,但是由於它用於收集所有必需的信息,因此還會插入一個額外的字段。

public Dictionary<DateTime, double> IndexValuesDictionary
        {
            get { return SetDictionary(); }
            set { SetDictionary(); }
        }

        private Dictionary<DateTime, double> SetDictionary()
        {
            if (_indexValues == null)
            {
                _indexValues = new Dictionary<DateTime, double>();
                foreach (var indexValue in IndexValues)
                {
                    _indexValues.Add(indexValue.Date, indexValue.Value);
                }
            }
            return _indexValues;
        }
{
        "id": "5c93c1123369220b685719b3",
        "contextName": "Base rates",
        "indexes": [
            {
                "indexName": "S&P",
                "indexValues": [
                    {
                        "date": "2019-01-01T00:00:00Z",
                        "value": 2600.98
                    },
                    {
                        "date": "2019-01-02T00:00:00Z",
                        "value": 2605.98
                    }
                ],
                "indexValuesDictionary": { // This should not be included
                    "2019-01-01T00:00:00Z": 2600.98,
                    "2019-01-02T00:00:00Z": 2605.98
                }
            }
        ]
    }

這是此輸出的唯一可行選擇嗎?

在退后一步並查看了數據結構之后,我可以通過刪除數組嵌套並僅使用文檔嵌套來實現此功能(不幸的是,但簡化了架構)。 更新后的數據庫結構為:

[
    {
        "ContextName": "Base rates",
        "Description": "Base rates description",
        "Indexes": {
            "SPX": {
                "2019-01-01T00:00:00Z": 2600.98
            },
            "NDX": {
                "2019-01-01T00:00:00Z": 6600.98
            }
        }
    }
]

將類結構更新為:

Context.cs

public class Context
    {
        [BsonId]
        [BsonRepresentation(BsonType.ObjectId)]
        public string Id { get; set; }
        [BsonElement("ContextName")]
        public string ContextName { get; set; }
        [BsonElement("Description")]
        public string Description { get; set; }
        [BsonElement("Indexes")]
        [BsonDictionaryOptions(DictionaryRepresentation.Document)]
        public Index Indexes { get; set; }
    }

Index.cs

public class Index : Dictionary<string, Dictionary<DateTime, double>> { }

我還更改了序列化程序以將DateTime視為string ,因此將其包含在程序啟動中:

BsonSerializer.RegisterSerializer(new DateTimeSerializer(DateTimeKind.Local, BsonType.String));

這些更改允許正確格式化結構。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM