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

时间:2015-04-14 15:02:41

标签: c# json mongodb serialization json.net

范围:

我正在写一个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> ();
                }
        }

问题:

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

我试图将其反序列化为&#34; DateTime&#34;和&#34;对象&#34;,但它们都无法工作。有没有正确的方法这样做?这基本上是我所缺少的所有这些&#34;动态出口商&#34;的工作原理。

提前致谢

1 个答案:

答案 0 :(得分:0)

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