You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions?) overload is documented to only throw NotSupportedException, however it throws an ArgumentException if the serialized object has a string property that is too long.
Reproduction Steps
private sealed class Foo
{
public string? Bar { get; set; }
}
[TestMethod]
public void Test()
{
var foo = new Foo() { Bar = new string(' ', 0x3FFFFFDF) };
var serialized = JsonSerializer.Serialize(foo);
}
Expected behavior
I believe there are several possible expected behaviors:
Simply document the ArgumentException that is throw here
Given the amount of "work" that the Serialize method does prior to this exception, I think it's a bit disingenuous to consider this an ArgumentException. I think that an InvalidOperationException is likely more appropriate.
Ultimately, I don't think that the JSON standard has a maximum length on string properties. Given that .NET does have a maximum string length, and this overload returns a string, there will be some limit hit at some point, but the limit in the Serialize method is significantly smaller than the .NET limit. As a result, I believe that this is an artificial limit. Ideally, I would say that the best behavior would be:
Serialize method should only throw if the resulting string exceeds the .NET string length thing
Serialize should simply preserve the existing string OutOfMemoryException
This OutOfMemoryException should be documented as a possible exception thrown by the Serialize method
Actual behavior
An ArgumentException is thrown:
The JSON value of length 1073741791 is too large and not supported.
It is not feasible for the JsonSerializer methods to document every possible exception that they might throw. This is because the methods are generic and plug into arbitrary converters which might be user defined. In this case, the error you are experiencing is due to #67337 (comment)
Description
The
System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions?)
overload is documented to only throwNotSupportedException
, however it throws anArgumentException
if the serialized object has a string property that is too long.Reproduction Steps
Expected behavior
I believe there are several possible expected behaviors:
ArgumentException
that is throw hereSerialize
method does prior to this exception, I think it's a bit disingenuous to consider this anArgumentException
. I think that anInvalidOperationException
is likely more appropriate.Serialize
method is significantly smaller than the .NET limit. As a result, I believe that this is an artificial limit. Ideally, I would say that the best behavior would be:Serialize
method should only throw if the resultingstring
exceeds the .NET string length thingSerialize
should simply preserve the existing stringOutOfMemoryException
OutOfMemoryException
should be documented as a possible exception thrown by theSerialize
methodActual behavior
An
ArgumentException
is thrown:Regression?
No response
Known Workarounds
No response
Configuration
No response
Other information
No response
The text was updated successfully, but these errors were encountered: