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


C# JsonTextReader.ReadAsDateTimeOffset方法代碼示例

本文整理匯總了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();
        }
開發者ID:albumprinter,項目名稱:Neo4jClient,代碼行數:32,代碼來源: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: 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);
    }
開發者ID:robertherber,項目名稱:PersonalTasks,代碼行數:23,代碼來源:JsonTextReaderTest.cs

示例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);
    }
開發者ID:robertherber,項目名稱:PersonalTasks,代碼行數:20,代碼來源:JsonTextReaderTest.cs

示例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);
    }
開發者ID:robertherber,項目名稱:PersonalTasks,代碼行數:20,代碼來源:JsonTextReaderTest.cs

示例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);
    }
開發者ID:robertherber,項目名稱:PersonalTasks,代碼行數:20,代碼來源:JsonTextReaderTest.cs

示例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();
        });
    }
開發者ID:robertherber,項目名稱:PersonalTasks,代碼行數:19,代碼來源:JsonTextReaderTest.cs

示例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();
        });
    }
開發者ID:robertherber,項目名稱:PersonalTasks,代碼行數:11,代碼來源:JsonTextReaderTest.cs

示例9: ReadAsDateTimeOffsetNoContent

    public void ReadAsDateTimeOffsetNoContent()
    {
      JsonTextReader reader = new JsonTextReader(new StringReader(@""));

      Assert.IsNull(reader.ReadAsDateTimeOffset());
      Assert.AreEqual(JsonToken.None, reader.TokenType);
    }
開發者ID:robertherber,項目名稱:PersonalTasks,代碼行數:7,代碼來源:JsonTextReaderTest.cs

示例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();
    }
開發者ID:plurby,項目名稱:Newtonsoft.Json,代碼行數:14,代碼來源:JsonTextReaderTest.cs

示例11: ReadAsDateTimeOffsetBadContent

    public void ReadAsDateTimeOffsetBadContent()
    {
      JsonTextReader reader = new JsonTextReader(new StringReader(@"new Date()"));

      reader.ReadAsDateTimeOffset();
    }
開發者ID:plurby,項目名稱:Newtonsoft.Json,代碼行數:6,代碼來源:JsonTextReaderTest.cs

示例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.");
        }
開發者ID:JamesNK,項目名稱:Newtonsoft.Json,代碼行數:6,代碼來源:ExceptionHandlingTests.cs


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