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