public void Write_Null_ShouldThrowJsonException() { var options = new JsonSerializerOptions(); var converter = new ObjectConverter(); MemoryStream memoryStream; using (memoryStream = new MemoryStream()) using (var writer = new Utf8JsonWriter(memoryStream)) { #pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type. Assert.Throws <JsonException>(() => converter.Write(writer, null, options)); #pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type. } }
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 Write_Object_ShouldReturnNewObject() { var options = new JsonSerializerOptions(); var converter = new ObjectConverter(); MemoryStream memoryStream; using (memoryStream = new MemoryStream()) using (var writer = new Utf8JsonWriter(memoryStream)) { converter.Write(writer, new object(), options); } var actual = Encoding.UTF8.GetString(memoryStream.ToArray()); Assert.Equal("{}", actual); }
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); }
public void CanConvert_Dictionary_ShouldReturnFalse() { var converter = new ObjectConverter(); Assert.False(converter.CanConvert(typeof(Dictionary <string, object>))); }
public void CanConvert_String_ShouldReturnFalse() { var converter = new ObjectConverter(); Assert.False(converter.CanConvert(typeof(string))); }
public void CanConvert_Object_ShouldReturnTrue() { var converter = new ObjectConverter(); Assert.True(converter.CanConvert(typeof(object))); }