MongoDB objecd转换错误在c#使用Newtonsoft反序列化



我一直在努力将MongoDB BSON文档转换为c#中的列表对象。在转换时,我得到下面的错误

"{"Unexpected character encountered while parsing value: O. Path '_id', line 1, position 10."}"

在stackoverflow中搜索类似问题后,我发现下面的链接

JSON。序列化Mongo ObjectId时,NET强制转换错误

和我一样。

我的代码:

实体/模型示例

public class BTMObj
{
[JsonConverter(typeof(MongoDataSerializer))]
public ObjectId _id { get; set; }
public string requestFormat { get; set; }
}
public class MongoDataSerializer : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(ObjectId);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType != JsonToken.String)
{
throw new Exception(
String.Format("Unexpected token parsing ObjectId. Expected String, got {0}.",
reader.TokenType));
}
var value = (string)reader.Value;
return String.IsNullOrEmpty(value) ? ObjectId.Empty : new ObjectId(value);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (value is ObjectId)
{
var objectId = (ObjectId)value;
writer.WriteValue(objectId != ObjectId.Empty ? objectId.ToString() : String.Empty);
}
else
{
throw new Exception("Expected ObjectId value.");
}
}
}
public List<T> GetMongoCollection<T>(string collectionName)
{
try
{
List<T> list = new List<T>();
var client = new MongoClient(Convert.ToString(ConfigurationManager.AppSettings["MONGO_CONNECTION"]));
var database = client.GetDatabase(Convert.ToString(ConfigurationManager.AppSettings["MONGO_DB"]));
var collection = database.GetCollection<BsonDocument>(collectionName);
var documents = collection.Find(new BsonDocument()).ToList();
foreach (var document in documents)
{
try
{
list.Add(JsonConvert.DeserializeObject<T>(document.ToJson()));
}
catch (Exception ex)
{
}
}
return list;
}
catch (Exception ex)
{
throw;
}
}

调用方法

list = mongoDBOperations.GetMongoCollection<BTMObj>(Collection);

MongoDataSerializer类覆盖的方法应该得到调用,但事实并非如此。我们需要的是在Model中获得ObjectId作为字符串。

请帮忙解决这个问题。

示例document.toJson()值

{
"_id": ObjectId("611cf42e1e4c89336b6fe2f0"),
"requestFormat": "json"
}

你只用了一半的相关代码。

如果你写这个JSON:

"_id": ObjectId("611cf42e1e4c89336b6fe2f0"),
"requestFormat": "json"
}

BsonObject.ToJson()所做的,那么这不是JSON。ObjectId(...)方言,就像Date(),NumberLong(),NumberInt()NumberDecimal()是使MongoDB吐出无效JSON的结构,因为它是其内部BSON存储格式的表示

所以如果你想把它当作JSON,写有效的JSON。代码就在链接中:您需要自己序列化对象。参见如何将BsonDocument对象反序列化回类。

首先确保Mongo c#驱动程序将BSON反序列化到POCO中:

// Prefer using statically-typed extension methods such as 
// _collection.FindAs<MyType>()
var deserialized = BsonSerializer.Deserialize<BTMobj>(document);

然后使用您的转换器将该对象序列化为JSON:

var json = JsonConvert.SerializeObject(deserialized);

您的输出将变得非常可解析:

"_id": "611cf42e1e4c89336b6fe2f0",
"requestFormat": "json"
}

类型的元数据(属性)将告诉解析器解析"611cf42e1e4c89336b6fe2f0"当您再次将其反序列化为BTMobj时,将其作为BsonObjectId

最新更新