1

I would like to store a list of transactions in a list as a json. (Register transaction, unregister transaction etc). Each transaction has an upload method, and the implementation of the upload method is specific to the transaction type (calls a different endpoint on a server).

For this I created an abstract class called "TransactionTypeBase", that contains an upload method that will be overriden and triggered when deserializing and enumerating the list of concrete transactions types.

The issue is when deserializng the List<TransactionTypeBase> I get this error:

Deserialization of reference types without parameterless constructor is not supported. Type MyProject.TransactionTypeBase' (also nto sure if the serialization is done properly in this case)

I do understand from research that I have to create a custom converter inheriting from JsonConverter but I am not sure how to actually implement that for my case, and that is where I need help please.

This is my code:

public abstract class TransactionTypeBase
    {
        public abstract TransactionType TransactionType { get; }

        public abstract Task UploadToServer(Data data);
    }

    public enum TransactionType
    {
        Register = 1,
        Unregister = 2
    }

public class RegisterTransactionType : TransactionTypeBase
    {
        public override TransactionType TransactionType => TransactionType.Register;

        public override async Task UploadToServer(Data data)
        {
            // Call Register API endpoint
        }
    }

public class UnregisterTransactionType : TransactionTypeBase
    {
        public override TransactionType TransactionType => TransactionType.Unregister;

        public override async Task UploadToServer(Data data)
        {
            // call Unregister API endpoint
        }
    }


    public class Transactions : List<TransactionTypeBase>
    {

        public void Add(TransactionTypeBase type)
        {
            if (type is RegisterTransactionType)
            {
                // do some optional additional work
            }

            base.Add(type);
        }
    }


   public class Data
   {
      public int Id {get;set;}
      public string Username {get;set;}
      ..........................
   }

In usage:

......

var transactions = new Transactions();

if (isRegister)
   transactions.Add(new RegisterTransactionType());
else
   transactions.Add(new UnregisterTransactionType());

.......

To store transactions in a json format:

var serializedTransactionList = JsonSerializer.Serialize<Transactions>();

To later deserialize and enumerate (and where error occurs):

var deserializedTransactionList = JsonDeserialize<Transactions>(serializedTransactionList);

The purpose was to achieve this point, to trigger the saved transactions later on, when there is internet connection of for whatever other reason:

foreach (var transaction in deserializedTransactionList)
 {
   transaction.Upload(data); // where the endpoint is called based on what type of transaction it is.
 }
dbc
  • 104,963
  • 20
  • 228
  • 340
Razvan Emil
  • 533
  • 9
  • 25
  • 1
    `System.Text.Json` does not support polymorphic deserialization out of the box. You need to write a custom `JsonConverter`. See [Is polymorphic deserialization possible in System.Text.Json?](https://stackoverflow.com/q/58074304/3744182) for how to do it. – dbc Dec 22 '20 at 20:16
  • @dbc yes that is the issue, I didnt figure out how to do it..and what I need help with – Razvan Emil Dec 22 '20 at 20:18
  • Well there are several examples at the linked question, so is that sufficient? – dbc Dec 22 '20 at 20:18
  • @dbc oh the link looks helpful thanks dbc, i will study that – Razvan Emil Dec 22 '20 at 20:18
  • @dbc looks promising, will try to implement it in my scenario. – Razvan Emil Dec 22 '20 at 20:20

0 Answers0