Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Base Class Properties not being Deserialized #159

Closed
frankvHoof93 opened this issue Dec 5, 2022 · 2 comments
Closed

Base Class Properties not being Deserialized #159

frankvHoof93 opened this issue Dec 5, 2022 · 2 comments

Comments

@frankvHoof93
Copy link

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?

@frankvHoof93
Copy link
Author

frankvHoof93 commented Dec 5, 2022

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?

@manuc66
Copy link
Owner

manuc66 commented Nov 27, 2023

Hello @frankvHoof93,

Could you please provide details about the JSON payload, specifically regarding AWeaponData and Gun?

Additionally, if possible, could you share a complete sample that can be independently executed?

@manuc66 manuc66 closed this as completed Nov 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants