-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
System.Text.Json object cycle error message could be better #38786
Comments
cc @jozkee |
@meziantou can you try looking at |
Indeed,
Maybe it should be part of the exception message, so it would be displayed in
I use a custom JsonConverter for CultureInfo. It uses
I don't think this is a good workaround here. Even if that work for serialization, I don't think it will correctly instantiate the CultureInfo when deserializing. |
I think including the full JSON path here could be too cumbersome, I was thinking that we could append the last property name before throwing but that would not work well for reference loops in arrays (e.g:
@meziantou that's a nice workaround. |
Adding the full path could be useful. In this case the Another idea is to change the message to indicate that the |
That implies saving a reference of each object in the current object branch and then removing them when we are done with it, that's probably very expensive. The fact about that error message is that we are not detecting a cycle, we are just validating that the runtime/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterOfT.cs Lines 241 to 244 in 682141e
However I think maybe we can do something to make the message clearer as you suggests. |
Other execeptions in System.Text.Json already provide the |
May I ask why I think the object-cycle exception message is fine for general use-cases, just that a |
I converted a code from Newtonsoft.Json to S.T.Json. Using Newtonsoft.Json, |
@layomia may I ask what is the reason why |
The path has been added to the exception text in the meantime. |
Description
When migrating to S.T.Json from Newtonsoft.Json I got an error when serializing a
CultureInfo
.This is ok that the json serializer doesn't handle every possible type. In this case I wrote a custom JsonConverter and it works well.
However, it tooks me a few minutes to find the problematic property in my model as the message/call stack doesn't include any useful data.
I think this could be useful to add the name of the property and its type when this exception is thrown. Maybe it's even possible to get the full property path. In my case, knowing that the problematic property is "Parent" of type "CultureInfo" would have helped.
BTW, S.T.Json could also support serializing CultureInfo out of the box but this is another issue.
Configuration
The text was updated successfully, but these errors were encountered: