Deserialization of a Json response causes FATAL ERROR

Deserialization of a Json response causes FATAL ERROR

bsaidiDG51
Enthusiast Enthusiast
1,201 Views
2 Replies
Message 1 of 3

Deserialization of a Json response causes FATAL ERROR

bsaidiDG51
Enthusiast
Enthusiast

I'm currently coding a C# .NET macro for AutoCAD that needs to make a [GET] Api call. Retrieving the content string of the API call works fine, but when I try to deserialize the response string using Flurl or Newton, the AutoCAD crashes with a FATAL ERROR.

And when I try to deserialize the response using: 

using JsonSerializer = System.Text.Json.JsonSerializer;

for this code:

using HttpClient httpClient = new HttpClient();
HttpResponseMessage result1 = await httpClient.GetAsync(apiUrl);
string result2 = await result1.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<List<SubAssemblyTypeDto>>(result2, _jsonSerializerOptions);

 I get this error: Could not load file or assembly 'System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. Le fichier spécifié est introuvable.
at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options)

Then when I install the demanded version of System.Memory, I get this error: Could not load file or assembly 'System.Buffers, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. Le fichier spécifié est introuvable.
at System.Text.Json.JsonSerializer.Deserialize(String json, Type returnType, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options)

 

Why is it so complex to deserialize Json strings within the AutoCAD .NET environment? Is there any simpler way?

 

Thank you!

0 Likes
Accepted solutions (1)
1,202 Views
2 Replies
Replies (2)
Message 2 of 3

norman.yuan
Mentor
Mentor
Accepted solution

Which version of AutoCAD are you working against? do you add reference to System.Text.Json from Nuget package? Have you looked at your project's output folder (".....\bin\Debug\") to make sure the System.Text.Json.dll and ALL of the dependency assemblies (quite a few of them!) are there? If you do AutoCAD 2025 with .NET 8, you need to edit the *.csproj file to make sure all the DLLs in the Nuget package get outputted when the code is compiled.

 

I use NewtonSoft for handling https request/response, from AutoCAD 2015 to 2024, without issue. Using System.Text.Json from Nuget package adds ridiculously 9 dependency DLLs to the AutoCAD plugin, while with NewtonSoft, only 1 dll (NewtonSoft.Json.dll) goes with the Plugin DLL, or you do not event need to deploy NewtonSoft.Json.dll at all (set Copy Local to false), because AutoCAD comes with NewtonSoft.Json.dll (in the C:\Program Files\Autodesk\AutoCAD20xx\ folder).

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 3

bsaidiDG51
Enthusiast
Enthusiast

Weird, I'm pretty sure I had used the Newtonsoft deserializer and it made my AutoCAD crash, but now it seems to work just fine.

Thank you!

 

return Newtonsoft.Json.JsonConvert.DeserializeObject<List<SubAssemblyTypeDto>>(responseString);

 

0 Likes