Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In my project, there is such a piece of code, as shown below:
var serializeOperations = new JsonSerializerOptions();
serializeOperations.Converters.Add(new Int32Converter());  // Get error
Root lrsSeg = JsonSerializer.Deserialize<Root>(result, serializeOperations);

But I get
Error CS1503: cannot convert from 'System.ComponentModel.Int32Converter' to 'System.Text.Json.Serialization.JsonConverter'

How to resolve this problem? Thanks if you can help.
----------
After I posted this thread, I retried the code from https://stackoverflow.com/questions/59097784/system-text-json-deserialize-json-with-automatic-casting[^] , the
AutoNumberToStringConverter
, but still get failure
"The JSON value could not be converted to System.Int32.


What I have tried:

Error CS1503: cannot convert from 'System.ComponentModel.Int32Converter' to 'System.Text.Json.Serialization.JsonConverter'
Posted
Updated 15-May-23 4:06am
v2
Comments
Graeme_Grant 12-May-23 22:09pm    
and what does the json look like?

1 solution

Well, that's because the Int32Converter class is not for Json. You have to use a class that derives from JsonConverter<t>. The Int32Converter class does not do that. I mean, look at the namespace the class sits in: System.ComponentModel. Does that sound like it has anything to do with Json?

You didn't say why you're trying to do this, so about the best I can suggest is reading up on How to write custom converters for JSON serialization - .NET | Microsoft Learn[^]
 
Share this answer
 
Comments
s yu 15-May-23 7:57am    
Dave: Thanks for your response. The piece of code I used is sourced from
https://www.thecodebuzz.com/system-text-json-the-json-value-could-not-be-converted-to-system-int32/
Solution 1, which is a custom conversion.
s yu 15-May-23 10:51am    
I re-checked the classes built through VS tool "Paste Special", and found one field in a class is incorrect. After correcting it. The error is gone. Thank Dave's solution also.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900