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