I am deserialize a BSON string to a C# class, and met a error.
Here is the string
{
"_typeName" : "FATPOT::Mapping::Rendering::GisSourceFile",
"compressionType" : 2,
"fileType" : 3,
"id" : "9f5f53d7-dc38-421e-8e2d-f419717540d1",
"localPath" : "C:/Resources/Shape/GU_Reserve.dbf",
"name" : "GU_Reserve.dbf",
"stored" : true
}
Here is the C# model class: GisSourceFileModel
public class GisSourceFileModel
{
[BsonElement("_typeName")]
public string TypeName { get; set; }
[BsonElement("compressionType")]
public int CompressionType { get; set; }
[BsonElement("fileType")]
public int FileType { get; set; }
[BsonElement("id")]
public string Id { get; set; }
[BsonElement("localPath")]
public string LocalPath { get; set; }
[BsonElement("name")]
public string Name { get; set; }
[BsonElement("stored")]
public bool Stored { get; set; }
}
Deserialization code
[TestMethod]
public void BsonDeserialize_GisSourceFileModel_Should_Work()
{
var srcStr = "{ \"_typeName\" : \"FATPOT::Mapping::Rendering::GisSourceFile\", \"compressionType\" : 2, \"fileType\" : 3, \"id\" : \"9f5f53d7-dc38-421e-8e2d-f419717540d1\", \"localPath\" : \"C:/Resources/Shape/GU_Reserve.dbf\", \"name\" : \"GU_Reserve.dbf\", \"stored\" : true }";
var bsonDocument = BsonDocument.Parse(srcStr);
var actualJsonDeserializedModel = BsonSerializer.Deserialize<GisSourceFileModel>(bsonDocument);
Assert.IsNotNull(actualJsonDeserializedModel);
}
Here is the error message
Message:
Test method Mapping.Plugin.Tests.GisSourceTests.BsonDeserialize_GisSourceFileModel_Should_Work threw exception:
System.FormatException: Element 'id' does not match any field or property of class Mapping.Plugin.BLL.Models.GisSourceFileModel.
From the error message, the "id" is the cause of the error. Can I have any hint or input to fix the error? Thanks
Found the Answer Change GisSourceFileModel 's "Id" to any other name to make it work.
e.g.
Feel "Id" is a kind of reserved keyword, or maybe it's a bug for BsonSerializer