HeadEx
HeadEx

Reputation: 33

MongoDB convert dictionary<Enum,dictionary<int,double>> to bson

I have an attribute like I said in title dictionary<Enum,dictionary<int,double>>. I want to record it to mongodb but I have a problem with converting to bson.

 var jsonDoc = Newtonsoft.Json.JsonConvert.SerializeObject(value);
 var bsonDoc = BsonSerializer.Deserialize<BsonDocument>(jsonDoc);

I tried the above code, but this code did not convert the dictionary in the value part. I need to help about this problem.

Upvotes: 0

Views: 343

Answers (1)

幻岭环
幻岭环

Reputation: 11

public Dictionary<int, Dictionary<int, long>> MapHeroDict2 = new Dictionary<int, Dictionary<int, long>>();
BsonClassMap.RegisterClassMap<TFCombatMap>(cm =>
            {
                cm.AutoMap();
                var customDictionarySerializer =
                    new DictionaryInterfaceImplementerSerializer<Dictionary<int, Dictionary<int, long>>>(
                        dictionaryRepresentation: DictionaryRepresentation.ArrayOfArrays,
                        keySerializer: new Int32Serializer(BsonType.String),
                        valueSerializer: new DictionaryInterfaceImplementerSerializer<Dictionary<int, long>>(
                            dictionaryRepresentation: DictionaryRepresentation.ArrayOfArrays,
                            keySerializer: new Int32Serializer(BsonType.String),
                            valueSerializer: BsonSerializer.SerializerRegistry.GetSerializer<long>()));
                cm.GetMemberMap(c => c.MapHeroDict2).SetSerializer(customDictionarySerializer);
            });

Upvotes: 1

Related Questions