You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to use JsonSubTypes to Serialize & Deserialize som sub-classes based on an abstract base-class.
When serializing the data, all of the properties are present in the JSON.
However, when Deserializing the Subclass' Properties are Deserialized, but the Base-Class Properties are not, and are all set to their default values.
Current Implementation:
Base Class:
public interface IDataObject<T>
{
void SetData(T obj);
}
[JsonConverter(typeof(JsonSubtypes), "WeaponType")]
[Serializable]
public abstract class AWeaponData : IDataObject<AWeapon>
{
public string WeaponType => GetType().Name;
[field: SerializeField]
public float Damage { get; private set; }
[field: SerializeField]
public float CurrentCooldown { get; private set; }
public AWeaponData(AWeapon weapon)
{
SetData(weapon);
}
[JsonConstructor]
protected AWeaponData(AWeaponData data)
{
Debug.Log("AA");
}
public virtual void SetData(AWeapon obj)
{
Damage = obj.Damage;
CurrentCooldown = obj.CurrentCooldown;
}
}
Example of SubClass:
[Serializable]
public class GunData : AWeaponData
{
[JsonConstructor]
protected GunData(GunData data) : base(data) { }
public string Test;
public GunData(Gun gun) : base(gun)
{ Test = "Test"; }
public override void SetData(AWeapon obj)
{
if (obj as Gun == null)
throw new InvalidOperationException("Invalid Class-Type");
base.SetData(obj);
}
}
I've tried both with a JsonConstructor with & without a parameter. With the parameter the incoming value is NULL.
After Deserializing a GunData-object, the Test-string is set, but the Damage & Cooldown are not.
Is this a bug in my implementation? Or a bug in the JsonSubTypes-Package?
The text was updated successfully, but these errors were encountered:
Addition:
I was able to get it to properly deserialize by using a custom [JsonConstructor] that uses actual parameters for all of the properties that exist in the object. (i.e. GunData(string test, float damage, float currentCooldown) : base(damage, currentCooldown))
However, this seems like a lot of work for something that's normally implemented by default, even with an empty constructor.
Is there a way to do this using settings?
I'm trying to use JsonSubTypes to Serialize & Deserialize som sub-classes based on an abstract base-class.
When serializing the data, all of the properties are present in the JSON.
However, when Deserializing the Subclass' Properties are Deserialized, but the Base-Class Properties are not, and are all set to their default values.
Current Implementation:
Base Class:
Example of SubClass:
I've tried both with a JsonConstructor with & without a parameter. With the parameter the incoming value is NULL.
After Deserializing a GunData-object, the Test-string is set, but the Damage & Cooldown are not.
Is this a bug in my implementation? Or a bug in the JsonSubTypes-Package?
The text was updated successfully, but these errors were encountered: