gpt4 book ai didi

c# - 是否可以将 MongoDB 的 "ISODate"字段反序列化为 JToken (C#)?

转载 作者:可可西里 更新时间:2023-11-01 09:16:09 26 4
gpt4 key购买 nike

范围:

我正在写一个 set of tools帮助人们在他们的 MongoDB 数据库上运行常见操作,“导出”数据就是其中之一。

目前我支持完整的 JSON 导出和“CSV”,但后者更棘手。

导出工具允许使用“ConfigFile”指定哪些字段将被反序列化(来自 BsonDocument ),而不关心它们的类型。目前大多数类型都可以使用,但“ISO”日期仍然让我头疼。

动态反序列化

目前我依赖JObjects处理“Json”文档的解析,就像这样:

        // Json Writer Settings - To avoid problems with 10Gen types
var jsonSettings = new JsonWriterSettings () { OutputMode = JsonOutputMode.Strict };

// Mapping string to a dynamic json object
JObject mappedJson = JObject.Parse (jsonObject.ToJson (jsonSettings));

// Trying to extract property values out of the object
foreach (Field field in _configuration.Fields)
{
// Checking for JToken Type
JTokenType objType = fieldData.Type;

// Sanity Check for NULL Values of properties that do exist
if (objType == JTokenType.Null)
{
fieldValue = String.Empty;
}
else if (objType == JTokenType.Array) // Checking for Arrays (that need to be serialized differently)
{
String[] valuesArray = fieldData.Select (t => t.Value<String> ().Replace (_configuration.ListDelimiter, String.Empty)
.Replace (_configuration.Delimiter, String.Empty)).ToArray ();

fieldValue = String.Join (_configuration.ListDelimiter, valuesArray);
}
else if (objType == JTokenType.Object && field.Name.Equals ("_id")) // Checking for specific MongoDB "_id" situation
{
fieldValue = fieldData.ToObject<String> (); // Value<ObjectId> ().ToString ();
}
else
{
// Reaching Attribute Value as "String" (if nothing else worked)
fieldValue = fieldData.Value<String> ();
}
}

问题:

此代码适用于我目前测试过的所有类型,但“DateTime”除外。 MongoDB 存储的方式如下:"PublicationDate": ISODate("2014-08-10T00:00:00.000Z"),这完全打破了我的反序列化。

我曾尝试将其反序列化为“DateTime”和“Object”,但它们都无法正常工作。有什么正确的方法可以做到这一点吗?这基本上是我使这个“动态导出器”工作所缺少的全部。

提前致谢

最佳答案

try catch 可能是捕获 iso datetime 的糟糕方法的解决方案?像 JTokenType.Date

using System.Globalization;

public static void ParseMongoDBISODate()
{
// Json Writer Settings - To avoid problems with 10Gen types
var jsonSettings = new JsonWriterSettings() { OutputMode = JsonOutputMode.Strict };

// Mapping string to a dynamic json object
JObject mappedJson = JObject.Parse(jsonObject.ToJson(jsonSettings));

// Trying to extract property values out of the object
foreach (Field field in _configuration.Fields)
{
// Checking for JToken Type
JTokenType objType = fieldData.Type;

// Sanity Check for NULL Values of properties that do exist
if (objType == JTokenType.Null)
{
fieldValue = String.Empty;
}
// Checking for Arrays (that need to be serialized differently)
else if (objType == JTokenType.Array)
{
String[] valuesArray = fieldData.Select(t => t.Value<String>().Replace(_configuration.ListDelimiter, String.Empty).Replace(_configuration.Delimiter, String.Empty)).ToArray();

fieldValue = String.Join(_configuration.ListDelimiter, valuesArray);
}
// Checking for specific MongoDB "_id" situation
else if (objType == JTokenType.Object && field.Name.Equals("_id"))
{
fieldValue = fieldData.ToObject<String>(); // Value<ObjectId> ().ToString ();
}
else
{
try // it's a bad way but you can set fieldValue as a DateTime
{
//JTokenType.Date
DateTime mongoDBISODate = DateTime.Parse(fieldData.Value<String>(), null, DateTimeStyles.RoundtripKind);
fieldValue = mongoDBISODate;
}
catch (Exception)
{
// Reaching Attribute Value as "String" (if nothing else worked)
fieldValue = fieldData.Value<String>();
}
}
}
}

关于c# - 是否可以将 MongoDB 的 "ISODate"字段反序列化为 JToken (C#)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29630816/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com