当前位置: 首页>>代码示例>>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;未经允许,请勿转载。