bson 中 .Net 类型的原因是字典中缺少类型。Bson 序列化程序试图获得足够的状态来恢复字典中项目的原始对象。从上下文(字典)来看,它们属于“对象”类型,因此在反序列化时插入 .Net 类型以了解足够多的信息。
以下解决方案回答了您的问题,但丢失了反序列化的类型信息。
解决方案1:将字典类型更改为<string, decimal>
var dict = new Dictionary<string, decimal> {
{ "decimal", 3.503m },
{ "int", 45 }
};
var serializedString = dict.ToJson();
结果是:{ "decimal" : "3.503", "int" : "45" }
通过覆盖十进制序列化程序,您将获得预期的结果。
{ "decimal" : 3.503, "int" : 45 }
解决方案 2:将字典类型更改为<string, double>
var dict = new Dictionary<string, double> {
{ "decimal", (double)3.503m },
{ "int", 45 }
};
var serializedString = dict.ToJson();
结果是预期的结果:{ "decimal" : 3.503, "int" : 45 }
解决方案 3:使用自定义序列化程序
public class MyDictionarySerializer : SerializerBase<Dictionary<string, object>>
{
public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, Dictionary<string, object> dictionary)
{
context.Writer.WriteStartArray();
foreach (var item in dictionary)
{
context.Writer.WriteStartDocument();
context.Writer.WriteString(item.Key);
// TODO your converstions from object to double
var value = (double)item.Value;
context.Writer.WriteDouble(value);
context.Writer.WriteEndDocument();
}
context.Writer.WriteEndArray();
}
public override Dictionary<string, object> Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
{
context.Reader.ReadStartArray();
var result = new Dictionary<string, object>();
while (true)
{
try
{
//this catch block only need to identify the end of the Array
context.Reader.ReadStartDocument();
}
catch (Exception exp)
{
context.Reader.ReadEndArray();
break;
}
var key = context.Reader.ReadString();
double value = context.Reader.ReadDouble();
result.Add(key, value);
context.Reader.ReadEndDocument();
}
return result;
}
}