✅ Trying to use a polymorphic type discriminator to deserialize child classes
[JsonDerivedType(typeof(Effect), typeDiscriminator: "base")] above the class gives me JsonDerivedType could not be found error. The documentation doesn't seem to mention how to define it.
28 Replies
Consider the following hierarchy:
And the following collection:
Serializing the
pets
as is will produce this JSON:
Decorating the IPet
interface with:
....and serializing again shall produce the following result:
I keep getting this errorThe type or namespace name 'JsonDerivedTypeAttribute' could not be found (are you missing a using directive or an assembly reference?)
Try importing the missing references?
This attribute is for System.Text.Json
What framework are you using for your .NET project?
I already have system.text.json
how do i see the framework?
View the contents of .csproj
<Project Sdk="Godot.NET.Sdk/4.2.1">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework Condition=" '$(GodotTargetPlatform)' == 'android' ">net7.0</TargetFramework>
<TargetFramework Condition=" '$(GodotTargetPlatform)' == 'ios' ">net8.0</TargetFramework>
<EnableDynamicLoading>true</EnableDynamicLoading>
</PropertyGroup>
</Project>
And there you'll see an XML file with an element called TargetFramework
.net 6... Ok, let me think
crap
dotnet 6 doesn't support that
I'm using c# with the godot engine, so I'm not sure if i cant upgrade to 7 or 8
GitHub
[3.2.3RC3 Regression] System.Text.Json package not found when build...
Godot version: 3.2.3RC3 OS/device including version: Windows 10 64bit (Visual Studio 2019 with Godot extension) Issue description: I am using NuGet to include the System.Text.Json package in my God...
It might be a Godot specific issue
Could you try loading system.text.json from nuget?
Maybe that'll help?
in the documentation it says it works with .net7 and up, so it looks like im out of luck
Then you have newtonsoft json
thanks for your help, ill see if I can find a workaround
you can reference System.Text.Json 7.0.0 or 8.0.0 even though you are using .NET 6
it is allowed and works fine and correctly
how can i do that?
dotnet add package System.Text.Json
yes
wait what do i do with that?
you run it in a teminal window
you can open one inside your vs code
its still giving me the same error
It is generally recommend to use Visual Studio instead of VSCode. VSCode is very limited in it's functionality even after the significant updates to the C# extension
Just to double check, when you now look at the csproj again, you can see an item group with a package reference to system.text.json, right?
<Project Sdk="Godot.NET.Sdk/4.2.1">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework Condition=" '$(GodotTargetPlatform)' == 'android' ">net7.0</TargetFramework>
<TargetFramework Condition=" '$(GodotTargetPlatform)' == 'ios' ">net8.0</TargetFramework>
<EnableDynamicLoading>true</EnableDynamicLoading>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Text.Json" Version="8.0.0" />
</ItemGroup>
</Project>
looks like it
that error is there even after you rebuild?
it works now after i added using System.Text.Json.Serialization;!
thanks to both of you
Welcome!
Remember, you put the attribute on the base type to state what classes inherit it (derive from it)
And, I don't remember whether this is necessary or not, you might also need to put [JsonPolymorphic]
Its working great! json polymorphic doesn't seem to be needed. I can't wait to be able to save effects in my game now
Perfect!
$close
Use the /close command to mark a forum thread as answered