Workaround for providing support for System.Exception with System.Text.Json #82299
Replies: 3 comments 11 replies
-
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsWe are migrating our code base from Newtonsoft to System.Text.Json. While testing this migration effort, we have encountered errors while working with System.Exception objects. SerializedData Text: We see the following data in the serialized data created using System.Text.Json { Issue: Can you please share a workaround for providing support for System.Exception type with System.Text.Json?
|
Beta Was this translation helpful? Give feedback.
-
You will need to write JsonConverter and factory for exceptions because Message doesn't have a setter (unless you're fine with setting that by reflection then you can also workaround it with contract model and providing Set for Message property). If exception is your own implementation then you can use JsonConstructor attribute and put it on the constructor which takes message |
Beta Was this translation helpful? Give feedback.
-
@snjosephms could you share the expected serialization format? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
We are migrating our code base from Newtonsoft to System.Text.Json. While testing this migration effort, we have encountered errors while working with System.Exception objects.
SerializedData Text: We see the following data in the serialized data created using System.Text.Json
{
"Status": 0,
"Exception": {
"Message": "TestExceptionMessag",
"Data": {},
"HResult": -2146233088
},
"SourceService": 1,
"RequestId": "97f534a6-cfad-4d5f-934a-7298d27288a4",
"Type": "ProvisionOC_Response",
"RetryCount": 0,
"IsRetryable": false,
"ResubmitRetryCount": 0
}
Issue:
The de-serialized data does not contain the Message text correctly. Message from deserialized object is "Exception of type 'System.Exception' was thrown."
Can you please share a workaround for providing support for System.Exception type with System.Text.Json?
Beta Was this translation helpful? Give feedback.
All reactions