public void Read_TokenTypeNone_ShouldThrowJsonException() { var converter = new ObjectConverter(); Assert.Throws <JsonException>(() => { var reader = new Utf8JsonReader(Encoding.UTF8.GetBytes("1").AsSpan()); converter.Read(ref reader, typeof(object), new JsonSerializerOptions()); }); }
public void Read_SimpleValue_ShouldMatchExpected(string json, object?expected) { var reader = new Utf8JsonReader(Encoding.UTF8.GetBytes(json).AsSpan()); reader.Read(); var converter = new ObjectConverter(FloatParseHandling.Double, DateParseHandling.None); var actual = converter.Read(ref reader, typeof(object), new JsonSerializerOptions()); Assert.Equal(expected, actual); }
public void Read_NumericValue_FloatParseHandlingDecimal_ShouldReturnDecimal() { var json = "2.0"; var reader = new Utf8JsonReader(Encoding.UTF8.GetBytes(json).AsSpan()); reader.Read(); var converter = new ObjectConverter(floatParseHandling: FloatParseHandling.Decimal, dateParseHandling: DateParseHandling.None); var actual = converter.Read(ref reader, typeof(object), new JsonSerializerOptions()); Assert.Equal(2m, actual); }
public void Read_DateTimeValueNotUtc_DateParseHandlingOffset_ShouldReturnDateTimeOffset() { var json = "\"2002-02-22T13:14:15-08:00\""; var reader = new Utf8JsonReader(Encoding.UTF8.GetBytes(json).AsSpan()); reader.Read(); var converter = new ObjectConverter(FloatParseHandling.Double, DateParseHandling.DateTimeOffset); var actual = converter.Read(ref reader, typeof(object), new JsonSerializerOptions()); var expected = new DateTimeOffset(2002, 2, 22, 13, 14, 15, TimeSpan.FromHours(-8)); Assert.Equal(expected, actual); }
public void Read_DateValue_DateParseHandlingOffset_ShouldReturnDateTimeOffset() { var json = "\"2002-02-22\""; var reader = new Utf8JsonReader(Encoding.UTF8.GetBytes(json).AsSpan()); reader.Read(); var converter = new ObjectConverter(FloatParseHandling.Double, DateParseHandling.DateTimeOffset); var actual = converter.Read(ref reader, typeof(object), new JsonSerializerOptions()); var expectedDt = new DateTime(2002, 2, 22, 0, 0, 0); Assert.Equal(expectedDt, ((DateTimeOffset)actual !).DateTime); }
public void Read_DateTimeValue_ShouldReturnDateTime() { var json = "\"2002-02-22T13:14:15\""; var reader = new Utf8JsonReader(Encoding.UTF8.GetBytes(json).AsSpan()); reader.Read(); var converter = new ObjectConverter(); var actual = converter.Read(ref reader, typeof(object), new JsonSerializerOptions()); var expected = new DateTime(2002, 2, 22, 13, 14, 15, DateTimeKind.Unspecified); Assert.Equal(expected, actual); Assert.Equal(expected.Kind, ((DateTime)actual !).Kind); }
public void Read_CommentOnly_ShouldThrowJsonException() { const string json = @"// Comment"; var converter = new ObjectConverter(); var jsonReaderOptions = new JsonReaderOptions { CommentHandling = JsonCommentHandling.Allow }; Assert.Throws <JsonException>(() => { var reader = new Utf8JsonReader(Encoding.UTF8.GetBytes(json).AsSpan(), jsonReaderOptions); reader.Read(); converter.Read(ref reader, typeof(object), new JsonSerializerOptions()); }); }
public void Read_DateTimeValueNotUtc_ShouldReturnDateTime() { var json = "\"2002-02-22T13:14:15-08:00\""; var reader = new Utf8JsonReader(Encoding.UTF8.GetBytes(json).AsSpan()); reader.Read(); var converter = new ObjectConverter(); var actual = converter.Read(ref reader, typeof(object), new JsonSerializerOptions()); var actualDt = (DateTime)actual !; var expected = new DateTime(2002, 2, 22, 21, 14, 15, DateTimeKind.Utc); Assert.Equal(expected, TimeZoneInfo.ConvertTimeToUtc(actualDt)); Assert.Equal(DateTimeKind.Local, actualDt.Kind); }
public void Read_ObjectWithComments_ShouldMatch(JsonCommentHandling commentHandling) { const string json = @"/* Comment */{ /* Comment */ ""a"":1, // Comment ""b"": // Comment 2, /* Comment */ ""c"": /* Comment */ 3, ""d"": [ // Comment 4/* Comment */,/* Comment */ 5 /* Comment */ ]/* Comment */ /* Comment */}"; var jsonReaderOptions = new JsonReaderOptions { CommentHandling = commentHandling, }; var reader = new Utf8JsonReader(Encoding.UTF8.GetBytes(json).AsSpan(), jsonReaderOptions); reader.Read(); var converter = new ObjectConverter(); var actual = converter.Read(ref reader, typeof(object), new JsonSerializerOptions()); var expected = new Dictionary <string, object> { ["a"] = 1L, ["b"] = 2L, ["c"] = 3L, ["d"] = new List <object> { 4L, 5L }, }; JsonDeepEqualAssert.Equal(expected, actual); }