本文整理汇总了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();
}
示例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
}
示例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.");
}
示例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());
}
示例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.");
}