MongoDB(C#)BsonSerializer没有正确反序列化

时间:2016-07-17 19:58:00

标签: c# mongodb serialization deserialization

序列化前后的项目比较(JSON):http://pastebin.com/zCWvAQ9G

免责声明:由于可读性,很多Json被排除在外。

问题出现在MongoDB BsonSerializer的工作之下。 当项目被反序列化时,它会设置" id"为0,而不是保持2或原始值。此外,id字段将被放置在extraElement字段内,当Mongo无法正确地将json值映射到相应的类字段时,会发生这种情况。类映射是AutoMapping,我没有做出明确的更改。

旁注:我不能使用Newtonsoft的JsonConvert,因为它无法转换Mongo ObjectId

    public static RootObject DeserializeUpdateItem(DataPuller dp,string index, RootObject itemtoupdate)
     {
        Thread.Sleep(5000);
        string jsonGraphData = dp.UrlToJson(@"http://services.runescape.com/m=itemdb_rs/api/graph/" + index + ".json");
        if (jsonGraphData == "Null")
        {
            return null;
        }
        string finalJsonItem = JsonMerger.MergeJson(JsonConvert.SerializeObject(itemtoupdate), jsonGraphData);          
        //Where the Magic happens (Aka the error)
        RootObject item = BsonSerializer.Deserialize<RootObject>(finalJsonItem);
        return item;
    }

相关课程

  public class RootObject
  {
    public Item item { get; set; }
    public Dictionary<string, int> daily;
    public Dictionary<string, int> average;
    public ObjectId _id { get; set; }

}

public class Item
{
    public string icon { get; set; }
    public string icon_large { get; set; }
    public int id { get; set; }
    public string type { get; set; }
    public string typeIcon { get; set; }
    public string name { get; set; }
    public string description { get; set; }
    public Current current = new Current();
    public Today today { get; set; }
    public string members { get; set; }
    public Prices daily = new Prices();
    public Prices average = new Prices();
    public Day day30 { get; set; }
    public Day day90 { get; set; }
    public Day day180 { get; set; }
    [BsonExtraElements]
    public BsonDocument extraElement { get; set; }
}

public class Prices
{
    public string[] dates;
    public int[] prices;
}

public class Current
{
    public string trend { get; set; }
    public string price { get; set; }
}

public class Today
{
    public string trend { get; set; }
    public string price { get; set; }
}

public class Day
{
    public string trend { get; set; }
    public string change { get; set; }
    public int[] averagePrices;
    public int[] currentPrices;
    public string[] dates;
}

通过添加这两行代码,我可以添加一个临时修复程序。我仍然想知道为什么会出现这种错误以及如何解决它。

     public static RootObject DeserializeUpdateItem(DataPuller dp,string index, RootObject itemtoupdate)
 {
    Thread.Sleep(5000);
    string jsonGraphData = dp.UrlToJson(@"http://services.runescape.com/m=itemdb_rs/api/graph/" + index + ".json");
    if (jsonGraphData == "Null")
    {
        return null;
    }
    string finalJsonItem = JsonMerger.MergeJson(JsonConvert.SerializeObject(itemtoupdate), jsonGraphData);          
    //Where the Magic happens (Aka the error)
    RootObject item = BsonSerializer.Deserialize<RootObject>(finalJsonItem);
    //Temp Fix
    item.item.id = int.Parse(index);
    item.item.extraElement = null;
    return item;
}

0 个答案:

没有答案