Mar*_*ins 10 c# serialization json json.net mongodb
范围:
我正在编写一组工具来帮助人们在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> ();
}
}
Run Code Online (Sandbox Code Playgroud)
问题:
此代码适用于我到目前为止测试的所有类型,但"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>();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)