0

Consider the following code (.NET Framework 4.8 c#)

using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;

namespace SerialisationTest
{
    class Program
    {
        class ErrorData     
        {
            public int ErrorCode { get; set; }

            public string ErrorDescription { get; set; }
        }

        [Serializable]
        class ErrorDataList : List<ErrorData>
        {
        }

        class DataResponse<TResponseType>
        {
            public TResponseType Data { get; set; }

            public List<ErrorData> ErrorList { get; set; }
        }

        class DataResponse2<TResponseType>
        {
            public TResponseType Data { get; set; }

            public ErrorDataList ErrorList { get; set; }
        }

        static void Main(string[] args)
        {
            JavaScriptSerializer serial = new JavaScriptSerializer();

            ErrorData error = new ErrorData
            {
                ErrorCode = 1,
                ErrorDescription = "ERRORS OCCURRED"
            };

            DataResponse<string> dataResponse = new DataResponse<string>
            {
                Data = "HELLO WORLD",
                ErrorList = new List<ErrorData>() { error }
            };

            string serialised = serial.Serialize(dataResponse);
            dataResponse = serial.Deserialize<DataResponse<string>>(serialised);

            DataResponse2<string> dataResponse2 = new DataResponse2<string>
            {
                Data = "HELLO WORLD",
                ErrorList = new ErrorDataList() { error }
            };

            string serialised2 = serial.Serialize(dataResponse);
            dataResponse2 = serial.Deserialize<DataResponse2<string>>(serialised2);
        }
    }
}

dataResponse serialises and deserialises fine, but dataResponse2 fails to deserialise with the error

The value "System.Collections.Generic.Dictionary`2[System.String,System.Object]" is not of type "SerialisationTest.Program+ErrorData" and cannot be used in this generic collection.

I marked the ErrorDataList class as Serializable in a vain attempt to rectify the issue (it is the same with or without the attribute). What is causing this error?

Symo
  • 466
  • 4
  • 16
  • You really shouldn't be inheriting from `List` anyway, there are [many reasons](https://stackoverflow.com/questions/21692193/why-not-inherit-from-listt) for that. – DavidG Oct 07 '22 at 13:40
  • 1
    What is the cause of the error though? In the 'real-world' use of the object there are methods in the DataErrorList class to e.g. add a new error to it, check if it has any entries. To refactor to change from the inherited class isn't really an option for us unfortunately and none of the options in that link (via the OP anyway) would be doable. If you aren't supposed to inherit from List why can you? – Symo Oct 07 '22 at 13:48
  • There's lots of things you *can do*, doesn't mean you should. As for why it is throwing an error, I'm not even going to guess, `JavaScriptSerializer` is also something that shouldn't really be used in modern development. – DavidG Oct 07 '22 at 13:53
  • Alas this application is 19 years old now... We do use the NewtonSoft JSON lib elsewhere so that may be an option. – Symo Oct 07 '22 at 13:57
  • Please note: there is a `Dictionary` in the error text. Not a `List`! – Alexander Petrov Oct 07 '22 at 14:07
  • Yes, I noticed that - JavaScriptSerializer falls back to Dictionary as a default (e.g. with DeserializeObject) so its doing that for some reason I think for my ErrorDataList. – Symo Oct 07 '22 at 16:10

1 Answers1

0

I ended up changing to use NewtonSoft.Json to deserialise and it works fine:

dataResponse2 = JsonConvert.DeserializeObject<DataResponse2<string>>(serialised2);
Symo
  • 466
  • 4
  • 16