Given a variant style class in 3rd party library coded like this:
enum VariantType
{
Type1,
Type2,
... etc
TypeN
}
class Variant
{
[SerializeField]
private VariantType _variantType;
public VariantType VariantType => _variantType;
[SerializeField] // Unity Serialization attribute
private Type1 _type1;
public Type1 Type1 => _type1;
... etc
[SerializeField] // Unity Serialization attribute
private TypeN _typeN;
public TypeN TypeN => _typeN;
}
Is there a way using JSON.Net to only serialize the single property identified by the VariantType when serializing Variant?
So we get
{
"_variantType": "Type1",
"_type1": {...}
}
and not (given that none of the properties are null)
{
"_variantType": "Type1",
"_type1": {...}
...
"_typeN": {...}
}
I've investigated using a custom ContractResolver and CreateProperty, but there doesn't seem to be a way of accessing the parent object.