public static void JsonEncodedTextStringsCustomAllowAll(string message, string expectedMessage) { // Allow all unicode values (except forbidden characters which we don't have in test data here) JavaScriptEncoder encoder = JavaScriptEncoder.Create(UnicodeRanges.All); var options = new JsonSerializerOptions(); options.Encoder = encoder; var obj = new TestClassForEncoding(); obj.MyString = message; string baselineJson = JsonSerializer.Serialize(obj); Assert.DoesNotContain(expectedMessage, baselineJson); string json = JsonSerializer.Serialize(obj, options); Assert.Contains(expectedMessage, json); obj = JsonSerializer.Deserialize <TestClassForEncoding>(json); Assert.Equal(obj.MyString, message); }
public static void CustomEncoderAllowLatin1Supplement(string message, string expectedMessage) { // Latin-1 Supplement block starts from U+0080 and ends at U+00FF JavaScriptEncoder encoder = JavaScriptEncoder.Create(UnicodeRanges.Latin1Supplement); var options = new JsonSerializerOptions(); options.Encoder = encoder; var obj = new TestClassForEncoding(); obj.MyString = message; string baselineJson = JsonSerializer.Serialize(obj); Assert.DoesNotContain(expectedMessage, baselineJson); string json = JsonSerializer.Serialize(obj, options); Assert.Contains(expectedMessage, json); obj = JsonSerializer.Deserialize <TestClassForEncoding>(json); Assert.Equal(obj.MyString, message); }