Skip to content
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.NotSupportedException: Serialization and deserialization of 'System.Type' instances are not supported. Path: $.ValueType. #91

Closed
kfathy-ys opened this issue Jun 12, 2022 · 7 comments · Fixed by #151
Assignees

Comments

@kfathy-ys
Copy link

When return result it get exception :
System.NotSupportedException: Serialization and deserialization of 'System.Type' instances are not supported. Path: $.ValueType.
---> System.NotSupportedException: Serialization and deserialization of 'System.Type' instances are not supported.
at System.Text.Json.Serialization.Converters.UnsupportedTypeConverter1.Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options) at System.Text.Json.Serialization.JsonConverter1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.Serialization.Metadata.JsonPropertyInfo1.GetMemberAndWriteJson(Object obj, WriteStack& state, Utf8JsonWriter writer) at System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.Serialization.JsonConverter1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state) at System.Text.Json.Serialization.JsonConverter1.WriteCore(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)


public Result AddUser(AddUserRequest addUserRequest)
{
try
{
return Result.Success("test");
}

@koralbit
Copy link

koralbit commented Feb 10, 2023

Maybe is too late but you need to add this nuget Microsoft.AspNetCore.Mvc.NewtonsoftJson and then add builder.Services.AddControllers().AddNewtonsoftJson(); in your Program.cs or Startup.cs file.

@KyleMcMaster
Copy link
Collaborator

Maybe is too late but you need to add this nuget Microsoft.AspNetCore.Mvc.NewtonsoftJson and then add builder.Services.AddControllers().AddNewtonsoftJson(); in your Program.cs or Startup.cs file.

The original comment is related to System.Text.Json and using the above configuration for MVC and Newtonsoft.Json won't fix this issue as far as I know.

@KyleMcMaster
Copy link
Collaborator

We should be able to fix this by either providing a customer serializer for System.Text.Json or possibly refactoring the Result class to make Type a calculated field using an expression. I'll try to work with Steve to get a fix in for this soon.

@KyleMcMaster KyleMcMaster self-assigned this Mar 2, 2023
@ardalis
Copy link
Owner

ardalis commented Mar 2, 2023

Do we need related serializer packages like we have for Ardalis.SmartEnum?

@VictorioBerra
Copy link

@ardalis I would personally like serialization support for a result/array of results.

@Rogal27
Copy link

Rogal27 commented Aug 17, 2023

I got this issue when I used [TranslateResultToActionResult] attribute but my controller didn't inherited ControllerBase class.
When I added the inheritance everything worked as expected.

@KyleMcMaster
Copy link
Collaborator

I believe this can be resolved by making the Type field use an expression rather than a property with an underlying field. I will try to put up a PR with the proposed changes here shortly.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants