private static async Task <Address> CreateAddressAsync(JsonTextReader reader) { Address a = new Address(); while (await reader.ReadAsync()) { if (reader.TokenType == JsonToken.PropertyName) { switch ((string)reader.Value) { case "Street": a.Street = await reader.ReadAsStringAsync(); break; case "Phone": a.Phone = await reader.ReadAsStringAsync(); break; case "Entered": a.Entered = (await reader.ReadAsDateTimeAsync()).GetValueOrDefault(); break; } } else { break; } } return(a); }
public async Task ReadStringValue_InvalidEndArrayAsync() { JsonTextReader reader = new JsonTextReader(new StringReader("]")); await ExceptionAssert.ThrowsAsync <JsonReaderException>(async() => { await reader.ReadAsDateTimeAsync(); }, "Unexpected character encountered while parsing value: ]. Path '', line 1, position 1."); }
public async Task Test_Async() { JsonTextReader reader = new JsonTextReader(new StringReader(@"{""enddate"":-1}")); reader.Read(); reader.Read(); await ExceptionAssert.ThrowsAsync <JsonReaderException>( () => reader.ReadAsDateTimeAsync(), "Cannot read number value as type. Path 'enddate', line 1, position 13."); }
public async Task ReadAsDateTime_MissingCommaAsync() { string json = "['2017-02-04T00:00:00Z' '2018-02-04T00:00:00Z' '2019-02-04T00:00:00Z']"; JsonTextReader reader = new JsonTextReader(new StringReader(json)); Assert.IsTrue(await reader.ReadAsync()); Assert.AreEqual(new DateTime(2017, 2, 4, 0, 0, 0, DateTimeKind.Utc), (DateTime)await reader.ReadAsDateTimeAsync()); await ExceptionAssert.ThrowsAsync <JsonReaderException>( () => reader.ReadAsDateTimeAsync(), "After parsing a value an unexpected character was encountered: '. Path '[0]', line 1, position 24."); }
public async Task ReadStringValue_Numbers_NotStringAsync() { JsonTextReader reader = new JsonTextReader(new StringReader("[56,56]")); await reader.ReadAsync(); await ExceptionAssert.ThrowsAsync <JsonReaderException>(async() => { await reader.ReadAsDateTimeAsync(); }, "Unexpected character encountered while parsing value: 5. Path '', line 1, position 2."); await ExceptionAssert.ThrowsAsync <JsonReaderException>(async() => { await reader.ReadAsDateTimeAsync(); }, "Unexpected character encountered while parsing value: 6. Path '', line 1, position 3."); await ExceptionAssert.ThrowsAsync <JsonReaderException>(async() => { await reader.ReadAsDateTimeAsync(); }, "Unexpected character encountered while parsing value: ,. Path '[0]', line 1, position 4."); Assert.AreEqual(56, await reader.ReadAsInt32Async()); Assert.IsTrue(await reader.ReadAsync()); }
public async Task ReadAsDateTime_BooleanAsync() { string json = @"true"; JsonTextReader reader = new JsonTextReader(new StringReader(json)); await ExceptionAssert.ThrowsAsync <JsonReaderException>(async() => { await reader.ReadAsDateTimeAsync(); }, "Unexpected character encountered while parsing value: t. Path '', line 1, position 1."); }
public async Task DateParseHandlingAsync() { string json = @"[""1970-01-01T00:00:00Z"",""\/Date(0)\/""]"; JsonTextReader reader = new JsonTextReader(new StringReader(json)); reader.DateParseHandling = DateParseHandling.DateTime; Assert.IsTrue(await reader.ReadAsync()); Assert.IsTrue(await reader.ReadAsync()); Assert.AreEqual( new DateTime(DateTimeUtils.InitialJavaScriptDateTicks, DateTimeKind.Utc), reader.Value ); Assert.AreEqual(typeof(DateTime), reader.ValueType); Assert.IsTrue(await reader.ReadAsync()); Assert.AreEqual( new DateTime(DateTimeUtils.InitialJavaScriptDateTicks, DateTimeKind.Utc), reader.Value ); Assert.AreEqual(typeof(DateTime), reader.ValueType); Assert.IsTrue(await reader.ReadAsync()); reader = new JsonTextReader(new StringReader(json)); reader.DateParseHandling = DateParseHandling.DateTimeOffset; Assert.IsTrue(await reader.ReadAsync()); Assert.IsTrue(await reader.ReadAsync()); Assert.AreEqual( new DateTimeOffset(DateTimeUtils.InitialJavaScriptDateTicks, TimeSpan.Zero), reader.Value ); Assert.AreEqual(typeof(DateTimeOffset), reader.ValueType); Assert.IsTrue(await reader.ReadAsync()); Assert.AreEqual( new DateTimeOffset(DateTimeUtils.InitialJavaScriptDateTicks, TimeSpan.Zero), reader.Value ); Assert.AreEqual(typeof(DateTimeOffset), reader.ValueType); Assert.IsTrue(await reader.ReadAsync()); reader = new JsonTextReader(new StringReader(json)); reader.DateParseHandling = DateParseHandling.None; Assert.IsTrue(await reader.ReadAsync()); Assert.IsTrue(await reader.ReadAsync()); Assert.AreEqual(@"1970-01-01T00:00:00Z", reader.Value); Assert.AreEqual(typeof(string), reader.ValueType); Assert.IsTrue(await reader.ReadAsync()); Assert.AreEqual(@"/Date(0)/", reader.Value); Assert.AreEqual(typeof(string), reader.ValueType); Assert.IsTrue(await reader.ReadAsync()); reader = new JsonTextReader(new StringReader(json)); reader.DateParseHandling = DateParseHandling.DateTime; Assert.IsTrue(await reader.ReadAsync()); await reader.ReadAsDateTimeOffsetAsync(); Assert.AreEqual( new DateTimeOffset(DateTimeUtils.InitialJavaScriptDateTicks, TimeSpan.Zero), reader.Value ); Assert.AreEqual(typeof(DateTimeOffset), reader.ValueType); await reader.ReadAsDateTimeOffsetAsync(); Assert.AreEqual( new DateTimeOffset(DateTimeUtils.InitialJavaScriptDateTicks, TimeSpan.Zero), reader.Value ); Assert.AreEqual(typeof(DateTimeOffset), reader.ValueType); Assert.IsTrue(await reader.ReadAsync()); reader = new JsonTextReader(new StringReader(json)); reader.DateParseHandling = DateParseHandling.DateTimeOffset; Assert.IsTrue(await reader.ReadAsync()); await reader.ReadAsDateTimeAsync(); Assert.AreEqual( new DateTime(DateTimeUtils.InitialJavaScriptDateTicks, DateTimeKind.Utc), reader.Value ); Assert.AreEqual(typeof(DateTime), reader.ValueType); await reader.ReadAsDateTimeAsync(); Assert.AreEqual( new DateTime(DateTimeUtils.InitialJavaScriptDateTicks, DateTimeKind.Utc), reader.Value ); Assert.AreEqual(typeof(DateTime), reader.ValueType); Assert.IsTrue(await reader.ReadAsync()); }
private async Task <TestClass> DeserializeJsonNetManualAsync(string json) { TestClass c = new TestClass(); JsonTextReader reader = new JsonTextReader(new StringReader(json)); await reader.ReadAsync(); while (await reader.ReadAsync()) { if (reader.TokenType == JsonToken.PropertyName) { string propertyName = (string)reader.Value; switch (propertyName) { case "strings": await reader.ReadAsync(); while (await reader.ReadAsync() && reader.TokenType != JsonToken.EndArray) { c.strings.Add((string)reader.Value); } break; case "dictionary": await reader.ReadAsync(); while (await reader.ReadAsync() && reader.TokenType != JsonToken.EndObject) { string key = (string)reader.Value; c.dictionary.Add(key, (await reader.ReadAsInt32Async()).GetValueOrDefault()); } break; case "Name": c.Name = await reader.ReadAsStringAsync(); break; case "Now": c.Now = (await reader.ReadAsDateTimeAsync()).GetValueOrDefault(); break; case "BigNumber": c.BigNumber = (await reader.ReadAsDecimalAsync()).GetValueOrDefault(); break; case "Address1": await reader.ReadAsync(); c.Address1 = await CreateAddressAsync(reader); break; case "Addresses": await reader.ReadAsync(); while (await reader.ReadAsync() && reader.TokenType != JsonToken.EndArray) { var address = await CreateAddressAsync(reader); c.Addresses.Add(address); } break; } } else { break; } } return(c); }