C
C#7mo ago
Abdesol

On deserializing using Newtonsoft, I get default constructor required issue on mac but not on window

I am trying to use the ChatGPT nuget package <PackageReference Include="OpenAI" Version="1.11.0" /> which have to deserialize json for data processing. It works normally on windows, but on macos, I face the error below:
Newtonsoft.Json.JsonSerializationException: Unable to find a constructor to use for type OpenAl_API.EndpointBase+ApiErrorResponse. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute.
Newtonsoft.Json.JsonSerializationException: Unable to find a constructor to use for type OpenAl_API.EndpointBase+ApiErrorResponse. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute.
My target framework for windows is net8.0, and for macos, it is net8.0-macos. I have tried to switch to another ChatGPT package, but similar issue arises. Is there any fix for this issue? Thank you!
14 Replies
Abdesol
AbdesolOP7mo ago
I have also tried using some other Json libraries beside Newtonsoft, like Json.NET and Json.NET.Aot, but still same issue arises
Pobiega
Pobiega7mo ago
Have you tried the built in System.Text.Json? That said, there will need to be a constructor for the deserializer to use
Abdesol
AbdesolOP7mo ago
Switching to a library that uses that causes a new error on mac:
System.NotSupportedException: The deserialization constructor for type 'OpenAl.ObjectModels.RequestModels.ChatMessage' contains parameters with null names. This might happen because the parameter names have been trimmed by ILLink. Consider using the source generated serializer instead.
System.NotSupportedException: The deserialization constructor for type 'OpenAl.ObjectModels.RequestModels.ChatMessage' contains parameters with null names. This might happen because the parameter names have been trimmed by ILLink. Consider using the source generated serializer instead.
In my .csproj, I have <TrimMode>copyused</TrimMode> set. net8.0-macos needs it for some reason
Pobiega
Pobiega7mo ago
Hm. Yeah try the source generated serialized and provide a manual context then Don't know what else to suggest
Abdesol
AbdesolOP7mo ago
I am confused around here.. I cannot really fix the library without cloning and adding it as a refernece.. will play around with other tags like LinkMode, PublishTrimmed, ...
reflectronic
reflectronic7mo ago
"net8.0-macos needs it for some reason" according to who? to be clear, the trimming is 100% what is breaking this and there is no way for you to fix it. Newtonsoft does not work with trimming. these libraries need to be rewritten to work with trimming, you will keep running into random issues like this one that said. it is definitely not a requirement to turn it on
Abdesol
AbdesolOP7mo ago
Ah okay, thanks for the info Looks like partial trim mode is kinda working
reflectronic
reflectronic7mo ago
to be clear, you should set <PublishTrimmed>false<PublishTrimmed> instead of doing this
Abdesol
AbdesolOP7mo ago
when I set that it shows the error that mac apps cannot be built with publish trimmed set to false on dotnet run
reflectronic
reflectronic7mo ago
ok, i see. it does say to set <LinkMode>None</<LinkMode> in your project, though. does that not fix the problem?
Abdesol
AbdesolOP7mo ago
the program crashes on a run and when I see the error, it says it was not able to load interop, etc.. dll stuffs trim mode partial seems to work. But it is causing some weird issue.
Abdesol
AbdesolOP7mo ago
@reflectronic I was trying to root some of the libraries to avoid trimming but still it says to use the source generated serializer
No description
Abdesol
AbdesolOP7mo ago
the library causing the issue is the openai one nvm, I got it fixed ye! Just adding OpenAI instead of the Betalgo fixed it how do I close this thread
Pobiega
Pobiega7mo ago
with /close
Want results from more Discord servers?
Add your server