✅ 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
Denis
Denis11mo ago
Consider the following hierarchy:
public interface IPet
{
string Name { get; }
}

public class Dog : IPet
{
public required string Name { get; init; }
public bool CanFetch { get; set; } = true;
}

public class Cat : IPet
{
public required string Name { get; init; }
public bool CanMeow { get; set; } = true;
}
public interface IPet
{
string Name { get; }
}

public class Dog : IPet
{
public required string Name { get; init; }
public bool CanFetch { get; set; } = true;
}

public class Cat : IPet
{
public required string Name { get; init; }
public bool CanMeow { get; set; } = true;
}
And the following collection:
var pets = new List<IPet>
{
new Dog { Name = "Snoopy", CanFetch = false },
new Cat { Name = "Garfield", CanMeow = true }
};
var pets = new List<IPet>
{
new Dog { Name = "Snoopy", CanFetch = false },
new Cat { Name = "Garfield", CanMeow = true }
};
Serializing the pets as is will produce this JSON:
[
{
"Name": "Snoopy"
},
{
"Name": "Garfield"
}
]
[
{
"Name": "Snoopy"
},
{
"Name": "Garfield"
}
]
Decorating the IPet interface with:
[JsonPolymorphic]
[JsonDerivedType(typeof(Dog), nameof(Dog))]
[JsonDerivedType(typeof(Cat), nameof(Cat))]
[JsonPolymorphic]
[JsonDerivedType(typeof(Dog), nameof(Dog))]
[JsonDerivedType(typeof(Cat), nameof(Cat))]
....and serializing again shall produce the following result:
[
{
"$type": "Dog",
"Name": "Snoopy",
"CanFetch": false
},
{
"$type": "Cat",
"Name": "Garfield",
"CanMeow": true
}
]
[
{
"$type": "Dog",
"Name": "Snoopy",
"CanFetch": false
},
{
"$type": "Cat",
"Name": "Garfield",
"CanMeow": true
}
]
Herbs And Spices
Herbs And SpicesOP11mo ago
I keep getting this errorThe type or namespace name 'JsonDerivedTypeAttribute' could not be found (are you missing a using directive or an assembly reference?)
Denis
Denis11mo ago
Try importing the missing references? This attribute is for System.Text.Json What framework are you using for your .NET project?
Herbs And Spices
Herbs And SpicesOP11mo ago
I already have system.text.json how do i see the framework?
Herbs And Spices
Herbs And SpicesOP11mo ago
No description
No description
Denis
Denis11mo ago
View the contents of .csproj
Herbs And Spices
Herbs And SpicesOP11mo ago
<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>
Denis
Denis11mo ago
And there you'll see an XML file with an element called TargetFramework .net 6... Ok, let me think
Herbs And Spices
Herbs And SpicesOP11mo ago
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
Denis
Denis11mo ago
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...
Denis
Denis11mo ago
It might be a Godot specific issue Could you try loading system.text.json from nuget? Maybe that'll help?
Herbs And Spices
Herbs And SpicesOP11mo ago
in the documentation it says it works with .net7 and up, so it looks like im out of luck
Denis
Denis11mo ago
Then you have newtonsoft json
Herbs And Spices
Herbs And SpicesOP11mo ago
thanks for your help, ill see if I can find a workaround
reflectronic
reflectronic11mo ago
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
Herbs And Spices
Herbs And SpicesOP11mo ago
how can i do that?
reflectronic
reflectronic11mo ago
dotnet add package System.Text.Json
Herbs And Spices
Herbs And SpicesOP11mo ago
yes wait what do i do with that?
reflectronic
reflectronic11mo ago
you run it in a teminal window you can open one inside your vs code
Herbs And Spices
Herbs And SpicesOP11mo ago
its still giving me the same error
Denis
Denis11mo ago
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?
Herbs And Spices
Herbs And SpicesOP11mo ago
<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
reflectronic
reflectronic11mo ago
that error is there even after you rebuild?
Herbs And Spices
Herbs And SpicesOP11mo ago
it works now after i added using System.Text.Json.Serialization;! thanks to both of you
Denis
Denis11mo ago
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]
Herbs And Spices
Herbs And SpicesOP11mo ago
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
Denis
Denis11mo ago
Perfect! $close
MODiX
MODiX11mo ago
Use the /close command to mark a forum thread as answered
Want results from more Discord servers?
Add your server