JSON.NET序列蒙戈的ObjectId时施放的错误 [英] JSON.NET cast error when serializing Mongo ObjectId

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

问题描述

我玩弄的MongoDB,并有一个MongoDB的对象的ObjectId就可以了。
当我这个连载的.NET JSON()方法,一切都很好(但日期是太可怕了!)

如果我尝试这与JSON.NET串行器它给了我尝试的连载时的ObjectID一个InvalidCastException

任何想法发生了什么,我怎么能解决这个问题?

 使用MongoDB.Driver;
使用MongoDB.Bson;
使用Newtonsoft.Json;//这是一个控制器上的路线
   公共字符串NiceJsonPlease()
    {        变种Q =新识别TestClass();
        q.id =新的ObjectId();
        q.test =只更新此;        返回JsonConvert.SerializeObject(Q);
    }    //简单的测试类
    类识别TestClass
    {
        公众的ObjectId ID; // MongoDB的对象ID
        公共字符串测试=你好;
    }
异常详细信息:System.InvalidCastException:指定的转换无效。

如果您更改控制器方法使用附带的.NET,它的工作原理串行OK(不过,这一次给了丑陋的日期,blugh)

 公共JsonResult NiceJsonPlease()
    {        变种Q =新识别TestClass();
        q.id =新的ObjectId();
        q.test =只更新此;        返回JSON(Q,JsonRequestBehavior.AllowGet);
    }


解决方案

我从MongoDB的用户组的指针。
<一href=\"https://groups.google.com/forum/?fromgroups=#!topic/mongodb-csharp/A_DXHuPscnQ\">https://groups.google.com/forum/?fromgroups=#!topic/mongodb-csharp/A_DXHuPscnQ

的反应是
这似乎是一个Json.NET的问题,但不是真的,有一个自定义类型在这里是根本不知道的。你需要告诉Json.NET如何序列化的ObjectId。

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

我饰我的ObjectId以

  [JsonConverter(typeof运算(ObjectIdConverter))]

然后写道,刚刚吐出的ObjectId的GUID部分自定义转换

 类ObjectIdConverter:JsonConverter
{    公共覆盖无效WriteJson(JsonWriter作家,对象的值,JsonSerializer串行)
    {
        serializer.Serialize(作家,value.ToString());    }    公众覆盖对象ReadJson(JsonReader读者,类型的objectType,对象existingValue,JsonSerializer串行)
    {
        抛出新NotImplementedException();
    }    公众覆盖布尔CanConvert(类型的objectType)
    {
        返回typeof运算(的ObjectId).IsAssignableFrom(的objectType);
        //返回true;
    }
}

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!)

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.

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);
    }

解决方案

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

The response was "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

I decorated my ObjectId with

[JsonConverter(typeof(ObjectIdConverter))]

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;
    }


}

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

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