-1

This is my Custom JsonConverter

public class TransactionJsonConverter : JsonConverter
{
    private Dictionary<string, object> _objectReferenceHolder = new Dictionary<string, object>();
    private TransactionCore _core;

    public TransactionJsonConverter(TransactionCore core)
    {
        _core = core;
    }

    public override bool CanConvert(Type objectType)
    {
        if (objectType == typeof(Transaction)) return true;

        return false;
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        JObject jObject = JObject.Load(reader);

        if (objectType == typeof(Transaction))
        {
            // process id must not be set --> will be set during population
            Transaction transaction = new Transaction(_core, null);

            _objectReferenceHolder.Add("Transaction", transaction);
            serializer.Populate(jObject.CreateReader(), transaction);
            transaction.Properties.ProcessConfiguration = _core.ConfigProvider.GetDocProcess(transaction.ProcessId);

            return transaction;
        }
        throw new NotImplementedException();
    }

    public override bool CanWrite => false;

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

The Object Transaction has a field Rows (Row[]) and Row has 2 Properties like that:

private int? _baseLoyaltyPoints = null;
private int? _loyaltyPoints = null;

[JsonIgnore]
public override int BaseLoyaltyPoints
{
    get
    {
        if (_baseLoyaltyPoints.HasValue)
            return _baseLoyaltyPoints.Value;

        return calcBasePoints();
    }
    internal set
    {
        if (value != 0)
            _baseLoyaltyPoints = value;
    }
}

[JsonIgnore]
public override int LoyaltyPoints
{
    get
    {
        if (_loyaltyPoints.HasValue)
            return _loyaltyPoints.Value;
        else return BaseLoyaltyPoints;
    }
    internal set
    {
        if (value != 0)
            _loyaltyPoints = value;
    }
}

As you can see I already tried to add the JsonIgnore attribute, but Custom JsonConverters do not have accsess to attributes. I am currently struggling to implement a solution so the JsonConverter would dezerialize the private variables but not the Properties

The end goal is that the private Variable does not get set when dezerialising the Property but does get set if it already had a value

Etheraex
  • 184
  • 1
  • 12
0brine
  • 425
  • 2
  • 15
  • Does this answer your question? [Ignore property with attribute in custom JSON converter](https://stackoverflow.com/questions/63469897/ignore-property-with-attribute-in-custom-json-converter) – Etheraex Jun 03 '22 at 09:35
  • No because I am using `serializer.Populate()` which has no settings parameter so I can not provide an Contract Resolver, if there is a different way to populate than `serializer.Populate()` which allows me to specify an Contract Resolver it would help me. – 0brine Jun 03 '22 at 09:45
  • It would be much better if you post a json you are trying to deserialize and error, instead of your code – Serge Jun 03 '22 at 13:57
  • I would expect that `JsonSerializer.Populate()` would not populate properties marked with `[JsonIgnore]`, are you saying this is not true? Might you please [edit] your question to share a [mcve]? It's not really clear what your problem is. From [ask]: *if your problem is with code you've written, you should include some... Include just enough code to allow others to reproduce the problem.* The code you show doesn't compile, and no JSON is included, so we can't reproduce the problem. – dbc Jun 03 '22 at 14:36

0 Answers0