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



作用域:

我正在编写一套工具来帮助人们在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";并且作为";对象";,但这两种方法都无法奏效。有什么合适的方法吗?这基本上就是我所缺少的制作这个";Dynamic Exporter";作品

提前感谢

try catch可能是一个捕捉iso日期时间的糟糕解决方案?像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>();
}
}
}
}

相关内容

  • 没有找到相关文章

最新更新