Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions?) throws an undocument ArgumentException #104168

Closed
ravudet opened this issue Jun 28, 2024 · 2 comments

Comments

@ravudet
Copy link

ravudet commented Jun 28, 2024

Description

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:

  1. Simply document the ArgumentException that is throw here
  2. 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.
  3. 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:
    1. Serialize method should only throw if the resulting string exceeds the .NET string length thing
    2. Serialize should simply preserve the existing string OutOfMemoryException
    3. 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.

Stack Trace: 
ThrowHelper.ThrowArgumentException_ValueTooLarge(Int32 tokenLength)
Utf8JsonWriter.WriteStringValue(ReadOnlySpan1 value) JsonConverter1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
JsonPropertyInfo1.GetMemberAndWriteJson(Object obj, WriteStack& state, Utf8JsonWriter writer) ObjectDefaultConverter1.OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, WriteStack& state)
JsonConverter1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state) JsonConverter1.WriteCore(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
JsonSerializer.WriteCore[TValue](Utf8JsonWriter writer, TValue& value, JsonTypeInfo1 jsonTypeInfo) JsonSerializer.WriteString[TValue](TValue& value, JsonTypeInfo1 jsonTypeInfo)
JsonSerializer.Serialize[TValue](TValue value, JsonSerializerOptions options)

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Jun 28, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
See info in area-owners.md if you want to be subscribed.

@eiriktsarpalis
Copy link
Member

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)

@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Jun 28, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Jul 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants