序列化 Mongo ObjectId 时出现 JSON.NET 转换错误 [英] JSON.NET cast error when serializing Mongo ObjectId

查看:22
本文介绍了序列化 Mongo ObjectId 时出现 JSON.NET 转换错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在玩 MongoDB 并且有一个带有 mongodb ObjectId 的对象.当我用 .NET Json() 方法序列化它时,一切都很好(但日期太可怕了!)

I am playing around with MongoDB and have an object with a mongodb ObjectId on it. When I serialise this with the .NET Json() method, all is good (but the dates are horrible!)

如果我使用 JSON.NET 序列化程序尝试此操作,它会在尝试序列化 ObjectID 时给我一个 InvalidCastException

If I try this with the JSON.NET serialiser it gives me an InvalidCastException when trying to serialise the ObjectID

有什么想法发生了什么以及我如何解决这个问题?

any ideas whats happening and how I can fix this?

using MongoDB.Driver;
using MongoDB.Bson;
using Newtonsoft.Json;

//this is a route on a controller
   public string NiceJsonPlease()
    {

        var q = new TestClass();
        q.id = new ObjectId();
        q.test = "just updating this";

        return JsonConvert.SerializeObject(q);
    }

    //simple test class
    class TestClass
    {
        public ObjectId id; //MongoDB ObjectID
        public string test = "hi there";
    }


Exception Details: System.InvalidCastException: Specified cast is not valid.

如果您将控制器方法更改为使用 .NET 附带的序列化程序,它就可以正常工作(但是,这个给出了丑陋的日期,blugh)

If you change the controller method to use the serializer that ships with .NET, it works ok (but, this one gives ugly dates, blugh)

public JsonResult NiceJsonPlease()
    {

        var q = new TestClass();
        q.id = new ObjectId();
        q.test = "just updating this";

        return Json(q, JsonRequestBehavior.AllowGet);
    }

推荐答案

我有一个来自 MongoDB 用户组的指针.https://groups.google.com/forum/?fromgroups=#!topic/mongodb-csharp/A_DXHuPscnQ

I had a pointer from the MongoDB user group. https://groups.google.com/forum/?fromgroups=#!topic/mongodb-csharp/A_DXHuPscnQ

回复是

这似乎是一个 Json.NET 问题,但实际上并非如此.有一个习俗在这里输入它根本不知道.你需要告诉 Json.NET 如何序列化一个 ObjectId.

This seems to be a Json.NET issue, but not really. There is a custom type here it simply doesn't know about. You need to tell Json.NET how to serialize an ObjectId.

所以,我实施了以下解决方案

So, I implemented the following solution

我用

[JsonConverter(typeof(ObjectIdConverter))]

然后写了一个自定义转换器,它只是吐出ObjectId的Guid部分

Then wrote a custom converter that just spits out the Guid portion of the ObjectId

 class ObjectIdConverter : JsonConverter
{

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    { 
        serializer.Serialize(writer, value.ToString());
       
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }

    public override bool CanConvert(Type objectType)
    {
        return typeof(ObjectId).IsAssignableFrom(objectType);
        //return true;
    }


}

这篇关于序列化 Mongo ObjectId 时出现 JSON.NET 转换错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆