當前位置: 首頁>>代碼示例>>C#>>正文


C# JsonTextReader.ReadAsDateTime方法代碼示例

本文整理匯總了C#中Newtonsoft.Json.JsonTextReader.ReadAsDateTime方法的典型用法代碼示例。如果您正苦於以下問題:C# JsonTextReader.ReadAsDateTime方法的具體用法?C# JsonTextReader.ReadAsDateTime怎麽用?C# JsonTextReader.ReadAsDateTime使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Newtonsoft.Json.JsonTextReader的用法示例。


在下文中一共展示了JsonTextReader.ReadAsDateTime方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: ParseDateTime

        public static DateTime? ParseDateTime(JToken value)
        {
            var rawValue = value.AsString();

            if (string.IsNullOrWhiteSpace(rawValue))
                return null;

            rawValue = rawValue.Replace("NeoDate", "Date");

            if (!DateRegex.IsMatch(rawValue))
            {
                DateTime parsed;
                if (!DateTime.TryParse(rawValue, out parsed))
                    return null;

                return rawValue.EndsWith("Z", StringComparison.InvariantCultureIgnoreCase) ? parsed.ToUniversalTime() : parsed;
            }

            var text = string.Format("{{\"a\":\"{0}\"}}", rawValue);
            var reader = new JsonTextReader(new StringReader(text));
            reader.Read(); // JsonToken.StartObject
            reader.Read(); // JsonToken.PropertyName
            return reader.ReadAsDateTime();
        }
開發者ID:albumprinter,項目名稱:Neo4jClient,代碼行數:24,代碼來源:CommonDeserializerMethods.cs

示例2: DateParseHandling

    public void DateParseHandling()
    {
      string json = @"[""1970-01-01T00:00:00Z"",""\/Date(0)\/""]";

      JsonTextReader reader = new JsonTextReader(new StringReader(json));
      reader.DateParseHandling = Json.DateParseHandling.DateTime;

      Assert.IsTrue(reader.Read());
      Assert.IsTrue(reader.Read());
      Assert.AreEqual(new DateTime(DateTimeUtils.InitialJavaScriptDateTicks, DateTimeKind.Utc), reader.Value);
      Assert.AreEqual(typeof(DateTime), reader.ValueType);
      Assert.IsTrue(reader.Read());
      Assert.AreEqual(new DateTime(DateTimeUtils.InitialJavaScriptDateTicks, DateTimeKind.Utc), reader.Value);
      Assert.AreEqual(typeof(DateTime), reader.ValueType);
      Assert.IsTrue(reader.Read());

#if !NET20
      reader = new JsonTextReader(new StringReader(json));
      reader.DateParseHandling = Json.DateParseHandling.DateTimeOffset;

      Assert.IsTrue(reader.Read());
      Assert.IsTrue(reader.Read());
      Assert.AreEqual(new DateTimeOffset(DateTimeUtils.InitialJavaScriptDateTicks, TimeSpan.Zero), reader.Value);
      Assert.AreEqual(typeof(DateTimeOffset), reader.ValueType);
      Assert.IsTrue(reader.Read());
      Assert.AreEqual(new DateTimeOffset(DateTimeUtils.InitialJavaScriptDateTicks, TimeSpan.Zero), reader.Value);
      Assert.AreEqual(typeof(DateTimeOffset), reader.ValueType);
      Assert.IsTrue(reader.Read());
#endif

      reader = new JsonTextReader(new StringReader(json));
      reader.DateParseHandling = Json.DateParseHandling.None;

      Assert.IsTrue(reader.Read());
      Assert.IsTrue(reader.Read());
      Assert.AreEqual(@"1970-01-01T00:00:00Z", reader.Value);
      Assert.AreEqual(typeof(string), reader.ValueType);
      Assert.IsTrue(reader.Read());
      Assert.AreEqual(@"/Date(0)/", reader.Value);
      Assert.AreEqual(typeof(string), reader.ValueType);
      Assert.IsTrue(reader.Read());

#if !NET20
      reader = new JsonTextReader(new StringReader(json));
      reader.DateParseHandling = Json.DateParseHandling.DateTime;

      Assert.IsTrue(reader.Read());
      reader.ReadAsDateTimeOffset();
      Assert.AreEqual(new DateTimeOffset(DateTimeUtils.InitialJavaScriptDateTicks, TimeSpan.Zero), reader.Value);
      Assert.AreEqual(typeof(DateTimeOffset), reader.ValueType);
      reader.ReadAsDateTimeOffset();
      Assert.AreEqual(new DateTimeOffset(DateTimeUtils.InitialJavaScriptDateTicks, TimeSpan.Zero), reader.Value);
      Assert.AreEqual(typeof(DateTimeOffset), reader.ValueType);
      Assert.IsTrue(reader.Read());


      reader = new JsonTextReader(new StringReader(json));
      reader.DateParseHandling = Json.DateParseHandling.DateTimeOffset;

      Assert.IsTrue(reader.Read());
      reader.ReadAsDateTime();
      Assert.AreEqual(new DateTime(DateTimeUtils.InitialJavaScriptDateTicks, DateTimeKind.Utc), reader.Value);
      Assert.AreEqual(typeof(DateTime), reader.ValueType);
      reader.ReadAsDateTime();
      Assert.AreEqual(new DateTime(DateTimeUtils.InitialJavaScriptDateTicks, DateTimeKind.Utc), reader.Value);
      Assert.AreEqual(typeof(DateTime), reader.ValueType);
      Assert.IsTrue(reader.Read());
#endif
    }
開發者ID:robertherber,項目名稱:PersonalTasks,代碼行數:69,代碼來源:JsonTextReaderTest.cs

示例3: ReadStringValue_InvalidEndArray

        public void ReadStringValue_InvalidEndArray()
        {
            JsonTextReader reader = new JsonTextReader(new StringReader("]"));

            ExceptionAssert.Throws<JsonReaderException>(() =>
            {
                reader.ReadAsDateTime();
            }, "Unexpected character encountered while parsing value: ]. Path '', line 1, position 1.");
        }
開發者ID:JamesNK,項目名稱:Newtonsoft.Json,代碼行數:9,代碼來源:ExceptionHandlingTests.cs

示例4: ReadStringValue_Numbers_NotString

        public void ReadStringValue_Numbers_NotString()
        {
            JsonTextReader reader = new JsonTextReader(new StringReader("[56,56]"));
            reader.Read();

            ExceptionAssert.Throws<JsonReaderException>(() =>
            {
                reader.ReadAsDateTime();
            }, "Unexpected character encountered while parsing value: 5. Path '', line 1, position 2.");

            ExceptionAssert.Throws<JsonReaderException>(() =>
            {
                reader.ReadAsDateTime();
            }, "Unexpected character encountered while parsing value: 6. Path '', line 1, position 3.");

            ExceptionAssert.Throws<JsonReaderException>(() =>
            {
                reader.ReadAsDateTime();
            }, "Unexpected character encountered while parsing value: ,. Path '[0]', line 1, position 4.");

            Assert.AreEqual(56, reader.ReadAsInt32());
            Assert.IsTrue(reader.Read());
        }
開發者ID:JamesNK,項目名稱:Newtonsoft.Json,代碼行數:23,代碼來源:ExceptionHandlingTests.cs

示例5: ReadAsDateTime_Boolean

        public void ReadAsDateTime_Boolean()
        {
            string json = @"true";

            JsonTextReader reader = new JsonTextReader(new StringReader(json));

            ExceptionAssert.Throws<JsonReaderException>(() => { reader.ReadAsDateTime(); }, "Unexpected character encountered while parsing value: t. Path '', line 1, position 1.");
        }
開發者ID:JamesNK,項目名稱:Newtonsoft.Json,代碼行數:8,代碼來源:ExceptionHandlingTests.cs


注:本文中的Newtonsoft.Json.JsonTextReader.ReadAsDateTime方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。