情况: 语言:C# 使用 C# 驱动程序 我有一个包含 List 作为属性的模型。该列表可以包含全部继承 BaseModelClass 的 3 个不同模型之一。为了帮助序列化这种情况,Mongo 添加了 _t 来识别实际使用的是哪个模型。对我们来说,这是一个问题,因为 _t 占用的空间量很大。我是一个低级开发人员,我要求更多的空间和 ram,他们告诉我在没有额外空间的情况下解决它。所以我坐下来编写一个自定义序列化程序来处理不同的类型,而无需将 _t 写入 BSONDocument。在我开始对序列化进行单元测试之前,我认为一切都很好。我开始收到“ReadEndArray 只能在 ContextType 为 Array 时调用,而不能在 ContextType 为 Document 时调用。”
非常感谢任何意见或建议。
这是我到目前为止的代码...
<--------集合模型-------------------->
[BsonCollectionName("calls")]
[BsonIgnoreExtraElements]
public class Call
{
[BsonId]
public CallId _id { get; set; }
[BsonElement("responses")]
[BsonIgnoreIfNull]
public IList<DataRecord> Responses { get; set; }
}
<------------基本数据记录------------------>
[BsonSerializer(typeof(DataRecordSerializer))]
public abstract class DataRecord
{
[BsonElement("key")]
public string Key { get; set; }
}
<------------实际数据记录示例---------------->
[BsonSerializer(typeof(DataRecordSerializer))]
public class DataRecordInt : DataRecord
{
[BsonElement("value")]
public int Value { get; set; }
}
[BsonSerializer(typeof(DataRecordSerializer))]
public class DataRecordDateTime : DataRecord
{
[BsonElement("value")]
public DateTime? Value { get; set; }
}
<----------------触发解串器的单元测试---------------->
//Arrange
var bsonDocument = TestResources.SampleCallJson;
//Act
var result = BsonSerializer.Deserialize<Call>(bsonDocument);
//Assert
Assert.IsTrue(true);
<----------------序列化器---------------->
public class DataRecordSerializer : IBsonSerializer
{
public object Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options)
{
//Entrance Criteria
if(nominalType != typeof(DataRecord)) throw new BsonSerializationException("Must be of base type DataRecord.");
if(bsonReader.GetCurrentBsonType() != BsonType.Document) throw new BsonSerializationException("Must be of type Document.");
bsonReader.ReadStartDocument();
var key = bsonReader.ReadString("key");
bsonReader.FindElement("value");
var bsonType = bsonReader.CurrentBsonType;
if (bsonType == BsonType.DateTime)
{
return DeserializeDataRecordDateTime(bsonReader, key);
}
return bsonType == BsonType.Int32 ? DeserializeDataRecordInt(bsonReader, key) : DeserializeDataRecordString(bsonReader, key);
}
public object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
{
//Entrance Criteria
if (nominalType != typeof (DataRecord)) throw new BsonSerializationException("Must be of base type DataRecord.");
if (bsonReader.GetCurrentBsonType() != BsonType.Document) throw new BsonSerializationException("Must be of type Document.");
bsonReader.ReadStartDocument(); // Starts Reading and is able to pull data fine through this and the next few lines of code.
var key = bsonReader.ReadString("key");
if (actualType == typeof(DataRecordDateTime))
{
return DeserializeDataRecordDateTime(bsonReader, key);
}
return actualType == typeof(DataRecordInt) ? DeserializeDataRecordInt(bsonReader, key) : DeserializeDataRecordString(bsonReader, key); // Once it tries to return I am getting the following Error: ReadEndArray can only be called when ContextType is Array, not when ContextType is Document.
}
public IBsonSerializationOptions GetDefaultSerializationOptions()
{
return new DocumentSerializationOptions
{
AllowDuplicateNames = false,
SerializeIdFirst = false
};
}
public void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
{
var currentType = value.GetType();
if (currentType == typeof (DataRecordInt))
{
SerializeDataRecordInt(bsonWriter, value);
return;
}
if (currentType == typeof(DataRecordDateTime))
{
SerializeDataRecordDateTime(bsonWriter, value);
return;
}
if (currentType == typeof(DataRecordString))
{
SerializeDataRecordString(bsonWriter, value);
}
}
private static object DeserializeDataRecordString(BsonReader bsonReader, string key)
{
var stringValue = bsonReader.ReadString();
var isCommentValue = false;
if (bsonReader.FindElement("iscomment"))
{
isCommentValue = bsonReader.ReadBoolean();
}
return new DataRecordString
{
Key = key,
Value = stringValue,
IsComment = isCommentValue
};
}
private static object DeserializeDataRecordInt(BsonReader bsonReader, string key)
{
var intValue = bsonReader.ReadInt32();
return new DataRecordInt
{
Key = key,
Value = intValue
};
}
private static object DeserializeDataRecordDateTime(BsonReader bsonReader, string key)
{
var dtValue = bsonReader.ReadDateTime();
var dateTimeValue = new BsonDateTime(dtValue).ToUniversalTime();
return new DataRecordDateTime
{
Key = key,
Value = dateTimeValue
};
}
private static void SerializeDataRecordString(BsonWriter bsonWriter, object value)
{
var stringRecord = (DataRecordString) value;
bsonWriter.WriteStartDocument();
var keyValue = stringRecord.Key;
bsonWriter.WriteString("key", string.IsNullOrEmpty(keyValue) ? string.Empty : keyValue);
var valueValue = stringRecord.Value;
bsonWriter.WriteString("value", string.IsNullOrEmpty(valueValue) ? string.Empty : valueValue);
bsonWriter.WriteBoolean("iscomment", stringRecord.IsComment);
bsonWriter.WriteEndDocument();
}
private static void SerializeDataRecordDateTime(BsonWriter bsonWriter, object value)
{
var dateRecord = (DataRecordDateTime) value;
var millisecondsSinceEpoch = dateRecord.Value.HasValue
? BsonUtils.ToMillisecondsSinceEpoch(new DateTime(dateRecord.Value.Value.Ticks, DateTimeKind.Utc))
: 0;
bsonWriter.WriteStartDocument();
var keyValue = dateRecord.Key;
bsonWriter.WriteString("key", string.IsNullOrEmpty(keyValue) ? string.Empty : keyValue);
if (millisecondsSinceEpoch != 0)
{
bsonWriter.WriteDateTime("value", millisecondsSinceEpoch);
}
else
{
bsonWriter.WriteString("value", string.Empty);
}
bsonWriter.WriteEndDocument();
}
private static void SerializeDataRecordInt(BsonWriter bsonWriter, object value)
{
var intRecord = (DataRecordInt) value;
bsonWriter.WriteStartDocument();
var keyValue = intRecord.Key;
bsonWriter.WriteString("key", string.IsNullOrEmpty(keyValue) ? string.Empty : keyValue);
bsonWriter.WriteInt32("value", intRecord.Value);
bsonWriter.WriteEndDocument();
}
}
最佳答案
也在这里问:https://groups.google.com/forum/#!topic/mongodb-user/iOeEXbUYbo4
我认为在这种情况下您最好的选择是使用自定义鉴别器约定。您可以在此处查看示例:https://github.com/mongodb/mongo-csharp-driver/blob/v1.x/MongoDB.DriverUnitTests/Samples/MagicDiscriminatorTests.cs .虽然此示例基于文档中是否存在字段,但您可以轻松地基于字段的类型(BsonType.Int32、BsonType.Date 等)。
关于c# - MongoDB 自定义序列化程序以避免 _t 被添加到集合中,抛出 ReadEndArray 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28637135/