本文整理汇总了C#中Newtonsoft.Json.JsonTextReader.ReadAsDateTimeOffset方法的典型用法代码示例。如果您正苦于以下问题:C# JsonTextReader.ReadAsDateTimeOffset方法的具体用法?C# JsonTextReader.ReadAsDateTimeOffset怎么用?C# JsonTextReader.ReadAsDateTimeOffset使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Newtonsoft.Json.JsonTextReader
的用法示例。
在下文中一共展示了JsonTextReader.ReadAsDateTimeOffset方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ParseDateTimeOffset
public static DateTimeOffset? ParseDateTimeOffset(JToken value)
{
var jValue = value as JValue;
if (jValue != null)
{
if (jValue.Value == null)
return null;
if (jValue.Value is DateTimeOffset)
return jValue.Value<DateTimeOffset>();
}
var rawValue = value.AsString();
if (string.IsNullOrWhiteSpace(rawValue))
return null;
rawValue = rawValue.Replace("NeoDate", "Date");
if (!DateRegex.IsMatch(rawValue))
{
DateTimeOffset parsed;
if (!DateTimeOffset.TryParse(rawValue, out parsed))
return null;
}
var text = string.Format("{{\"a\":\"{0}\"}}", rawValue);
var reader = new JsonTextReader(new StringReader(text)) {DateParseHandling = DateParseHandling.DateTimeOffset};
reader.Read(); // JsonToken.StartObject
reader.Read(); // JsonToken.PropertyName
return reader.ReadAsDateTimeOffset();
}
示例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: ReadAsDateTimeOffsetNewZealandDate
public void ReadAsDateTimeOffsetNewZealandDate()
{
string json = @"{""Offset"":""30/1/2011""}";
JsonTextReader reader = new JsonTextReader(new StringReader(json));
reader.Culture = new CultureInfo("en-NZ");
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
reader.ReadAsDateTimeOffset();
Assert.AreEqual(JsonToken.Date, reader.TokenType);
Assert.AreEqual(typeof(DateTimeOffset), reader.ValueType);
DateTimeOffset dt = (DateTimeOffset)reader.Value;
Assert.AreEqual(new DateTime(2011, 1, 30, 0, 0, 0, DateTimeKind.Unspecified), dt.DateTime);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
}
示例4: ReadAsDateTimeOffsetIsoDate
public void ReadAsDateTimeOffsetIsoDate()
{
string json = @"{""Offset"":""2011-08-01T21:25Z""}";
JsonTextReader reader = new JsonTextReader(new StringReader(json));
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
reader.ReadAsDateTimeOffset();
Assert.AreEqual(JsonToken.Date, reader.TokenType);
Assert.AreEqual(typeof(DateTimeOffset), reader.ValueType);
Assert.AreEqual(new DateTimeOffset(new DateTime(2011, 8, 1, 21, 25, 0, DateTimeKind.Utc), TimeSpan.Zero), reader.Value);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
}
示例5: ReadAsDateTimeOffsetWithMinutes
public void ReadAsDateTimeOffsetWithMinutes()
{
string json = @"{""Offset"":""\/Date(946708260000-0631)\/""}";
JsonTextReader reader = new JsonTextReader(new StringReader(json));
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
reader.ReadAsDateTimeOffset();
Assert.AreEqual(JsonToken.Date, reader.TokenType);
Assert.AreEqual(typeof(DateTimeOffset), reader.ValueType);
Assert.AreEqual(new DateTimeOffset(new DateTime(2000, 1, 1), TimeSpan.FromHours(-6).Add(TimeSpan.FromMinutes(-31))), reader.Value);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
}
示例6: ReadAsDateTimeOffsetHoursOnly
public void ReadAsDateTimeOffsetHoursOnly()
{
string json = "{\"Offset\":\"\\/Date(946663200000+06)\\/\"}";
JsonTextReader reader = new JsonTextReader(new StringReader(json));
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
reader.ReadAsDateTimeOffset();
Assert.AreEqual(JsonToken.Date, reader.TokenType);
Assert.AreEqual(typeof(DateTimeOffset), reader.ValueType);
Assert.AreEqual(new DateTimeOffset(new DateTime(2000, 1, 1), TimeSpan.FromHours(6)), reader.Value);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
}
示例7: ReadAsDateTimeOffsetBadString
public void ReadAsDateTimeOffsetBadString()
{
string json = @"{""Offset"":""blablahbla""}";
JsonTextReader reader = new JsonTextReader(new StringReader(json));
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
ExceptionAssert.Throws<JsonReaderException>(
"Could not convert string to DateTimeOffset: blablahbla. Path 'Offset', line 1, position 22.",
() =>
{
reader.ReadAsDateTimeOffset();
});
}
示例8: ReadAsDateTimeOffsetBadContent
public void ReadAsDateTimeOffsetBadContent()
{
JsonTextReader reader = new JsonTextReader(new StringReader(@"new Date()"));
ExceptionAssert.Throws<JsonReaderException>(
"Error reading date. Unexpected token: StartConstructor. Path '', line 1, position 9.",
() =>
{
reader.ReadAsDateTimeOffset();
});
}
示例9: ReadAsDateTimeOffsetNoContent
public void ReadAsDateTimeOffsetNoContent()
{
JsonTextReader reader = new JsonTextReader(new StringReader(@""));
Assert.IsNull(reader.ReadAsDateTimeOffset());
Assert.AreEqual(JsonToken.None, reader.TokenType);
}
示例10: ReadAsDateTimeOffsetBadString
public void ReadAsDateTimeOffsetBadString()
{
string json = @"{""Offset"":""blablahbla""}";
JsonTextReader reader = new JsonTextReader(new StringReader(json));
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
reader.ReadAsDateTimeOffset();
}
示例11: ReadAsDateTimeOffsetBadContent
public void ReadAsDateTimeOffsetBadContent()
{
JsonTextReader reader = new JsonTextReader(new StringReader(@"new Date()"));
reader.ReadAsDateTimeOffset();
}
示例12: ReadAsDateTimeOffsetBadContent
public void ReadAsDateTimeOffsetBadContent()
{
JsonTextReader reader = new JsonTextReader(new StringReader(@"new Date()"));
ExceptionAssert.Throws<JsonReaderException>(() => { reader.ReadAsDateTimeOffset(); }, "Unexpected character encountered while parsing value: e. Path '', line 1, position 2.");
}