public static void ParseNullStringToStructShouldThrowJsonException() { string nullString = "null"; Utf8JsonReader reader = new Utf8JsonReader(Encoding.UTF8.GetBytes(nullString)); JsonTestHelper.AssertThrows <JsonException>(reader, (reader) => JsonSerializer.Deserialize <SimpleStruct>(ref reader)); Assert.Throws <JsonException>(() => JsonSerializer.Deserialize <SimpleStruct>(Encoding.UTF8.GetBytes(nullString))); Assert.Throws <JsonException>(() => JsonSerializer.Deserialize <SimpleStruct>(nullString)); }
public static void ParseNullStringToStructShouldThrowJsonException() { string nullString = "null"; byte[] nullStringAsBytes = Encoding.UTF8.GetBytes(nullString); Utf8JsonReader reader = new Utf8JsonReader(nullStringAsBytes); JsonTestHelper.AssertThrows <JsonException>(ref reader, (ref Utf8JsonReader reader) => JsonSerializer.Deserialize <SimpleStruct>(ref reader)); Assert.Throws <JsonException>(() => JsonSerializer.Deserialize <SimpleStruct>(nullStringAsBytes)); Assert.Throws <JsonException>(() => JsonSerializer.Deserialize <SimpleStruct>(nullString)); // null can be assigned to nullable structs. Assert.Null(JsonSerializer.Deserialize <SimpleStruct?>(nullStringAsBytes)); Assert.Null(JsonSerializer.Deserialize <SimpleStruct?>(nullString)); }