草庐IT

c# - Mongo C# 驱动 toJson() DateTime

coder 2023-10-31 原文

我在 mongo 中有这样的数据:

"trd" : ISODate("2003-12-08T00:00:00Z")

现在,我正在像这样从 Mongo 获取数据作为 BsonDocument:

 var builder = Builders<BsonDocument>.Filter;
            var filter = builder.Eq("wsid", id);
            var mongoListBatch = _client.GetManyByFilter(filter, "directory");
            JsonWriterSettings settings = new JsonWriterSettings();
            settings.OutputMode = JsonOutputMode.Strict;

            var lists = mongoListBatch.ToJson(settings);

问题是我希望在 json 中得到像这样的 DateTime 对象:

 "transactiedatum": "23-02-1993"

但是,我得到:

 "transactiedatum": {
          "$date": 1070841600000
        }

首先,它的unix时间格式,其次,json结构有变化。我该如何处理?

最佳答案

我们遇到了类似的问题。这听起来很复杂,但没什么大不了的。

所以,首先,这是 github 上 Mongo C# 驱动器的链接: MongoC#Driver

对你来说,这里有两个重要的链接:

  1. Bson Extension Methods
  2. JsonWriter
  3. JsonWriterContext

我们想要实现的是我们不希望 Json 字符串中的表示是 unix 时间戳格式,所以我们将这些类以不同的名称复制到我们的项目中,所以,这里是所有的类:

扩展类:

using System;
using System.IO;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
using MongoDB.Bson;

    namespace Fishing.MongoDB.Serializers
    {
        public static class MyJsonWriterSettings
        {

            /// <summary>
            /// Serializes an object to a BSON byte array.
            /// </summary>
            /// <typeparam name="TNominalType">The nominal type of the object.</typeparam>
            /// <param name="obj">The object.</param>
            /// <param name="serializer">The serializer.</param>
            /// <param name="writerSettings">The writer settings.</param>
            /// <param name="configurator">The serialization context configurator.</param>
            /// <param name="args">The serialization args.</param>
            /// <returns>A BSON byte array.</returns>
            public static byte[] ToBson<TNominalType>(
                this TNominalType obj,
                IBsonSerializer<TNominalType> serializer = null,
                BsonBinaryWriterSettings writerSettings = null,
                Action<BsonSerializationContext.Builder> configurator = null,
                BsonSerializationArgs args = default(BsonSerializationArgs)
                )
            {
                return ToBson(obj, typeof(TNominalType), writerSettings, serializer, configurator, args);
            }

            /// <summary>
            /// Serializes an object to a BSON byte array.
            /// </summary>
            /// <param name="obj">The object.</param>
            /// <param name="nominalType">The nominal type of the object..</param>
            /// <param name="writerSettings">The writer settings.</param>
            /// <param name="serializer">The serializer.</param>
            /// <param name="configurator">The serialization context configurator.</param>
            /// <param name="args">The serialization args.</param>
            /// <returns>A BSON byte array.</returns>
            /// <exception cref="System.ArgumentNullException">nominalType</exception>
            /// <exception cref="System.ArgumentException">serializer</exception>
            public static byte[] ToBson(
                this object obj,
                Type nominalType,
                BsonBinaryWriterSettings writerSettings = null,
                IBsonSerializer serializer = null,
                Action<BsonSerializationContext.Builder> configurator = null,
                BsonSerializationArgs args = default(BsonSerializationArgs))
            {
                if (nominalType == null)
                {
                    throw new ArgumentNullException("nominalType");
                }

                if (serializer == null)
                {
                    serializer = BsonSerializer.LookupSerializer(nominalType);
                }
                if (serializer.ValueType != nominalType)
                {
                    var message = string.Format("Serializer type {0} value type does not match document types {1}.", serializer.GetType().FullName, nominalType.FullName);
                    throw new ArgumentException(message, "serializer");
                }

                using (var memoryStream = new MemoryStream())
                {
                    using (var bsonWriter = new BsonBinaryWriter(memoryStream, writerSettings ?? BsonBinaryWriterSettings.Defaults))
                    {
                        var context = BsonSerializationContext.CreateRoot(bsonWriter, configurator);
                        args.NominalType = nominalType;
                        serializer.Serialize(context, args, obj);
                    }
                    return memoryStream.ToArray();
                }
            }

            /// <summary>
            /// Serializes an object to a BsonDocument.
            /// </summary>
            /// <typeparam name="TNominalType">The nominal type of the object.</typeparam>
            /// <param name="obj">The object.</param>
            /// <param name="serializer">The serializer.</param>
            /// <param name="configurator">The serialization context configurator.</param>
            /// <param name="args">The serialization args.</param>
            /// <returns>A BsonDocument.</returns>
            public static BsonDocument ToBsonDocument<TNominalType>(
                this TNominalType obj,
                IBsonSerializer<TNominalType> serializer = null,
                Action<BsonSerializationContext.Builder> configurator = null,
                BsonSerializationArgs args = default(BsonSerializationArgs))
            {
                return ToBsonDocument(obj, typeof(TNominalType), serializer, configurator, args);
            }

            /// <summary>
            /// Serializes an object to a BsonDocument.
            /// </summary>
            /// <param name="obj">The object.</param>
            /// <param name="nominalType">The nominal type of the object.</param>
            /// <param name="serializer">The serializer.</param>
            /// <param name="configurator">The serialization context configurator.</param>
            /// <param name="args">The serialization args.</param>
            /// <returns>A BsonDocument.</returns>
            /// <exception cref="System.ArgumentNullException">nominalType</exception>
            /// <exception cref="System.ArgumentException">serializer</exception>
            public static BsonDocument ToBsonDocument(
                this object obj,
                Type nominalType,
                IBsonSerializer serializer = null,
                Action<BsonSerializationContext.Builder> configurator = null,
                BsonSerializationArgs args = default(BsonSerializationArgs))
            {
                if (nominalType == null)
                {
                    throw new ArgumentNullException("nominalType");
                }

                if (obj == null)
                {
                    return null;
                }

                if (serializer == null)
                {
                    var bsonDocument = obj as BsonDocument;
                    if (bsonDocument != null)
                    {
                        return bsonDocument; // it's already a BsonDocument
                    }

                    var convertibleToBsonDocument = obj as IConvertibleToBsonDocument;
                    if (convertibleToBsonDocument != null)
                    {
                        return convertibleToBsonDocument.ToBsonDocument(); // use the provided ToBsonDocument method
                    }

                    serializer = BsonSerializer.LookupSerializer(nominalType);
                }
                if (serializer.ValueType != nominalType)
                {
                    var message = string.Format("Serializer type {0} value type does not match document types {1}.", serializer.GetType().FullName, nominalType.FullName);
                    throw new ArgumentException(message, "serializer");
                }

                // otherwise serialize into a new BsonDocument
                var document = new BsonDocument();
                using (var bsonWriter = new BsonDocumentWriter(document))
                {
                    var context = BsonSerializationContext.CreateRoot(bsonWriter, configurator);
                    args.NominalType = nominalType;
                    serializer.Serialize(context, args, obj);
                }
                return document;
            }

            /// <summary>
            /// Serializes an object to a JSON string.
            /// </summary>
            /// <typeparam name="TNominalType">The nominal type of the object.</typeparam>
            /// <param name="obj">The object.</param>
            /// <param name="writerSettings">The JsonWriter settings.</param>
            /// <param name="serializer">The serializer.</param>
            /// <param name="configurator">The serializastion context configurator.</param>
            /// <param name="args">The serialization args.</param>
            /// <returns>
            /// A JSON string.
            /// </returns>
            public static string ToMyJson<TNominalType>(
                this TNominalType obj,
                JsonWriterSettings writerSettings = null,
                IBsonSerializer<TNominalType> serializer = null,
                Action<BsonSerializationContext.Builder> configurator = null,
                BsonSerializationArgs args = default(BsonSerializationArgs))
            {
                return ToMyJson(obj, typeof(TNominalType), writerSettings, serializer, configurator, args);
            }

            /// <summary>
            /// Serializes an object to a JSON string.
            /// </summary>
            /// <param name="obj">The object.</param>
            /// <param name="nominalType">The nominal type of the objectt.</param>
            /// <param name="writerSettings">The JsonWriter settings.</param>
            /// <param name="serializer">The serializer.</param>
            /// <param name="configurator">The serialization context configurator.</param>
            /// <param name="args">The serialization args.</param>
            /// <returns>
            /// A JSON string.
            /// </returns>
            /// <exception cref="System.ArgumentNullException">nominalType</exception>
            /// <exception cref="System.ArgumentException">serializer</exception>
            public static string ToMyJson(
                this object obj,
                Type nominalType,
                JsonWriterSettings writerSettings = null,
                IBsonSerializer serializer = null,
                Action<BsonSerializationContext.Builder> configurator = null,
                BsonSerializationArgs args = default(BsonSerializationArgs))
            {
                if (nominalType == null)
                {
                    throw new ArgumentNullException("nominalType");
                }

                if (serializer == null)
                {
                    serializer = BsonSerializer.LookupSerializer(nominalType);
                }
                if (serializer.ValueType != nominalType)
                {
                    var message = string.Format("Serializer type {0} value type does not match document types {1}.", serializer.GetType().FullName, nominalType.FullName);
                    throw new ArgumentException(message, "serializer");
                }

                using (var stringWriter = new StringWriter())
                {
                    using (var bsonWriter = new JsonWriterMine(stringWriter, writerSettings ?? JsonWriterSettings.Defaults))
                    {
                        var context = BsonSerializationContext.CreateRoot(bsonWriter, configurator);
                        args.NominalType = nominalType;
                        serializer.Serialize(context, args, obj);
                    }
                    return stringWriter.ToString();
                }
            }
        }
    }

所以基本上,将整个类从 github 复制到您自己的类中,并将 2 个方法的名称:ToJson() 更改为您的方法之一。在这里你可以看到我的是 ToJsonMine()。

现在,到第二节课你需要:

JsonWriter

using MongoDB.Bson.IO;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using MongoDB.Bson;

namespace Fishing.MongoDB.Serializers
{
    public class JsonWriterMine : BsonWriter
    {

        // private fields
        private TextWriter _textWriter;
        private JsonWriterSettings _jsonWriterSettings; // same value as in base class just declared as derived class
        private InternalJsonWriterContext _context;

        // constructors
        /// <summary>
        /// Initializes a new instance of the JsonWriter class.
        /// </summary>
        /// <param name="writer">A TextWriter.</param>
        public JsonWriterMine(TextWriter writer)
            : this(writer, JsonWriterSettings.Defaults)
        {
        }

        /// <summary>
        /// Initializes a new instance of the JsonWriter class.
        /// </summary>
        /// <param name="writer">A TextWriter.</param>
        /// <param name="settings">Optional JsonWriter settings.</param>
        public JsonWriterMine(TextWriter writer, JsonWriterSettings settings)
            : base(settings)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            _textWriter = writer;
            _jsonWriterSettings = settings; // already frozen by base class
            _context = new InternalJsonWriterContext(null, ContextType.TopLevel, "");
            State = BsonWriterState.Initial;
        }

 /// <summary>
    /// Writes a BSON DateTime to the writer.
    /// </summary>
    /// <param name="value">The number of milliseconds since the Unix epoch.</param>
    public override void WriteDateTime(long value)
    {
        if (Disposed) { throw new ObjectDisposedException("JsonWriter"); }
        if (State != BsonWriterState.Value && State != BsonWriterState.Initial)
        {
            ThrowInvalidState("WriteDateTime", BsonWriterState.Value, BsonWriterState.Initial);
        }

        WriteNameHelper(Name);
        switch (_jsonWriterSettings.OutputMode)
        {
            case JsonOutputMode.Strict:
                var utcDateTimeFirst = BsonUtils.ToDateTimeFromMillisecondsSinceEpoch(value);
                _textWriter.Write($"\"{utcDateTimeFirst.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ")}\"");
                break;

            case JsonOutputMode.Shell:
            default:
                // use ISODate for values that fall within .NET's DateTime range, and "new Date" for all others
                if (value >= BsonConstants.DateTimeMinValueMillisecondsSinceEpoch &&
                    value <= BsonConstants.DateTimeMaxValueMillisecondsSinceEpoch)
                {
                    var utcDateTime = BsonUtils.ToDateTimeFromMillisecondsSinceEpoch(value);
                    _textWriter.Write("ISODate(\"{0}\")", utcDateTime.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ"));
                }
                else
                {
                    _textWriter.Write("new Date({0})", value);
                }
                break;
        }

        State = GetNextState();
    }

    }
}

这就是魔法发生的地方。将整个类 JsonWriter 从 GitHub 复制到你自己的并给它一个新名称(扩展 BsonWriter nad 实现所有方法)。现在,您可以在这里操作您希望如何序列化日期。相应地更改 WriteDateTime(long value)。如您所见,在 case JsonOutputMode.Strict: 中,我将其更改为返回一个按我需要的方式格式化的 DateTime 对象。

最后,由于 MongoSerializer 具有名为 JsonWriterContext 的内部类,您需要创建自己的类并在 JsonWriter 中使用它(第 2 步)。

这是我的样子(你可以完整复制它):

using MongoDB.Bson.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Fishing.MongoDB.Serializers
{
    public class InternalJsonWriterContext
    {
        // private fields
        private InternalJsonWriterContext _parentContext;
        private ContextType _contextType;
        private string _indentation;
        private bool _hasElements = false;

        // constructors
        internal InternalJsonWriterContext(InternalJsonWriterContext parentContext, ContextType contextType, string indentChars)
        {
            _parentContext = parentContext;
            _contextType = contextType;
            _indentation = (parentContext == null) ? indentChars : parentContext.Indentation + indentChars;
        }

        // internal properties
        internal InternalJsonWriterContext ParentContext
        {
            get { return _parentContext; }
        }

        internal ContextType ContextType
        {
            get { return _contextType; }
        }

        internal string Indentation
        {
            get { return _indentation; }
        }

        internal bool HasElements
        {
            get { return _hasElements; }
            set { _hasElements = value; }
        }
    }
}

在你拥有一切之后,你将在 MongoCursorJsonConverter(第一步)中看到,在第二个 ToJsonMine() 方法中,我有这一行:

 using (var bsonWriter = new JsonWriterMine(stringWriter, writerSettings ?? JsonWriterSettings.Defaults))

您只需将其替换为您在第 2 步中创建的自己的自定义类,它就会非常有效。

最后,您只需调用:

var lists = mongoListBatch.ToJson(settings);

它会序列化你在 WriteDate(long value)

中添加的日期

关于c# - Mongo C# 驱动 toJson() DateTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46847042/

有关c# - Mongo C# 驱动 toJson() DateTime的更多相关文章

  1. ruby-on-rails - Rails 3 I18 : translation missing: da. datetime.distance_in_words.about_x_hours - 2

    我看到这个错误:translationmissing:da.datetime.distance_in_words.about_x_hours我的语言环境文件:http://pastie.org/2944890我的看法:我已将其添加到我的application.rb中:config.i18n.load_path+=Dir[Rails.root.join('my','locales','*.{rb,yml}').to_s]config.i18n.default_locale=:da如果我删除I18配置,帮助程序会处理英语。更新:我在config/enviorments/devolpment

  2. c# - 如何在 ruby​​ 中调用 C# dll? - 2

    如何在ruby​​中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL

  3. C# 到 Ruby sha1 base64 编码 - 2

    我正在尝试在Ruby中复制Convert.ToBase64String()行为。这是我的C#代码:varsha1=newSHA1CryptoServiceProvider();varpasswordBytes=Encoding.UTF8.GetBytes("password");varpasswordHash=sha1.ComputeHash(passwordBytes);returnConvert.ToBase64String(passwordHash);//returns"W6ph5Mm5Pz8GgiULbPgzG37mj9g="当我在Ruby中尝试同样的事情时,我得到了相同sha

  4. ruby-on-rails - 如何在 Ruby on Rails 中实现由 JSF 2.0 (Primefaces) 驱动的 UI 魔法 - 2

    按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭10年前。问题1)我想知道ruby​​onrails是否有功能类似于primefaces的gem。我问的原因是如果您使用primefaces(http://www.primefaces.org/showcase-labs/ui/home.jsf),开发人员无需担心javascript或jquery的东西。据我所知,JSF是一个规范,基于规范的各种可用实现,prim

  5. FOHEART H1数据手套驱动Optitrack光学动捕双手运动(Unity3D) - 2

    本教程将在Unity3D中混合Optitrack与数据手套的数据流,在人体运动的基础上,添加双手手指部分的运动。双手手背的角度仍由Optitrack提供,数据手套提供双手手指的角度。 01  客户端软件分别安装MotiveBody与MotionVenus并校准人体与数据手套。MotiveBodyMotionVenus数据手套使用、校准流程参照:https://gitee.com/foheart_1/foheart-h1-data-summary.git02  数据转发打开MotiveBody软件的Streaming,开始向Unity3D广播数据;MotionVenus中设置->选项选择Unit

  6. 基于C#实现简易绘图工具【100010177】 - 2

    C#实现简易绘图工具一.引言实验目的:通过制作窗体应用程序(C#画图软件),熟悉基本的窗体设计过程以及控件设计,事件处理等,熟悉使用C#的winform窗体进行绘图的基本步骤,对于面向对象编程有更加深刻的体会.Tutorial任务设计一个具有基本功能的画图软件**·包括简单的新建文件,保存,重新绘图等功能**·实现一些基本图形的绘制,包括铅笔和基本形状等,学习橡皮工具的创建**·设计一个合理舒适的UI界面**注明:你可能需要先了解一些关于winform窗体应用程序绘图的基本知识,以及关于GDI+类和结构的知识二.实验环境Windows系统下的visualstudio2017C#窗体应用程序三.

  7. c# - C# 中的 Flatten Ruby 方法 - 2

    我如何做Ruby方法"Flatten"RubyMethod在C#中。此方法将锯齿状数组展平为一维数组。例如:s=[1,2,3]#=>[1,2,3]t=[4,5,6,[7,8]]#=>[4,5,6,[7,8]]a=[s,t,9,10]#=>[[1,2,3],[4,5,6,[7,8]],9,10]a.flatten#=>[1,2,3,4,5,6,7,8,9,10 最佳答案 递归解决方案:IEnumerableFlatten(IEnumerablearray){foreach(variteminarray){if(itemisIEnume

  8. ruby - 可以像在 C# 中使用#region 一样在 Ruby 中使用 begin/end 吗? - 2

    我最近从C#转向了Ruby,我发现自己无法制作可折叠的标记代码区域。我只是想到做这种事情应该没问题:classExamplebegin#agroupofmethodsdefmethod1..enddefmethod2..endenddefmethod3..endend...但是这样做真的可以吗?method1和method2最终与method3是同一种东西吗?还是有一些我还没有见过的用于执行此操作的Ruby惯用语? 最佳答案 正如其他人所说,这不会改变方法定义。但是,如果要标记方法组,为什么不使用Ruby语义来标记它们呢?您可以使用

  9. c# - Ruby 等效于 C# Linq 聚合方法 - 2

    什么是Linq聚合方法的ruby​​等价物。它的工作原理是这样的varfactorial=new[]{1,2,3,4,5}.Aggregate((acc,i)=>acc*i);每次将数组序列中的值传递给lambda时,变量acc都会累积。 最佳答案 这在数学以及几乎所有编程语言中通常称为折叠。它是更普遍的变形概念的一个实例。Ruby从Smalltalk中继承了这个特性的名称,它被称为inject:into:(像aCollectioninject:aStartValueinto:aBlock一样使用。)所以,在Ruby中,它称为inj

  10. c# - 先学什么? - 2

    关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭8年前。Improvethisquestion几年前我去学校学习编程,毕业后我找到了一份系统管理方面的工作,这就是我职业生涯的方向。我想重新开始某种开发,并且一直在“玩”C#和ASP.NET,但我已经听到很多关于其他"new"语言的讨论(新的意思是它们是新的)我)喜欢Ruby和F#。我想我想知道我是否在浪费时间学习主要的MS语言,而不是成为一名通才。很长一段时间没有离开开发社区(如果我曾经离开过的话)让我在潮流中挣扎,我不想落在时代的

随机推荐