Skip to content

Cannot serialize abstract class on Android #1114

Closed

Description

@JamesNK

Copied from JamesNK/Newtonsoft.Json#1514 @skarllot


I getting in trouble to serialize abstract class that implements binary serialization on Android (NETStandard 1.3), but on desktop works as expected.

A minimum test project was created at: https://github.com/skarllot/JsonNetAbstractClass

Source/destination types

    [Serializable]
    public abstract class BaseClass : ISerializable
    {
        protected BaseClass()
        {
        }
        
        protected BaseClass(SerializationInfo info, StreamingContext context)
        {
        }
        
        void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
        {
            if (info == null)
                throw new ArgumentNullException(nameof(info));

            InternalGetObjectData(info, context);
        }
        
        protected abstract void InternalGetObjectData(SerializationInfo info, StreamingContext context);
    }

    [Serializable]
    public sealed class Collection : BaseClass
    {
        private readonly List<BaseClass> children;

        public Collection()
        {
            children = new List<BaseClass>();
        }

        private Collection(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
            // omitted
        }

        public IEnumerable<BaseClass> Children => children;

        public int Count => children.Count;

        // omitted
    }

    [Serializable]
    public class Termination : BaseClass
    {
        public int Value { get; set; }
        // omitted
    }

Source/destination JSON

On desktop gets:

{
   "Children":{
      "$type":"JsonNetAbstractClass.BaseClass[], DesktopJsonNetAbstractClass",
      "$values":[
         {
            "$type":"JsonNetAbstractClass.Termination, DesktopJsonNetAbstractClass",
            "Value":10
         },
         {
            "$type":"JsonNetAbstractClass.Termination, DesktopJsonNetAbstractClass",
            "Value":20
         }
      ]
   }
}

Expected behavior

Serialize same type on both platforms.

Actual behavior

Throws InvalidOperationException: Can't compile a NewExpression with a constructor declared on an abstract class.

Trace writer output:

2017-11-21T21:33:50.088 Info Started serializing JsonNetAbstractClass.Collection. Path ''.
2017-11-21T21:33:50.128 Info Started serializing JsonNetAbstractClass.BaseClass[]. Path 'Children'.
2017-11-21T21:33:50.133 Verbose Writing type name 'JsonNetAbstractClass.BaseClass[], PortableJsonNetAbstractClass' for JsonNetAbstractClass.BaseClass[]. Path 'Children'.
2017-11-21T21:33:50.136 Error Error serializing JsonNetAbstractClass.Collection. Can't compile a NewExpression with a constructor declared on an abstract class. Path 'Children.$values'.
2017-11-21T21:35:33.760 Error Error trying to serialize object

Steps to reproduce

var stringWriter = new StringWriter();
var collection = new Collection();
collection.Add(new Termination(10));
collection.Add(new Termination(20));

var traceWriter = new MemoryTraceWriter();
var serializer = JsonSerializer.CreateDefault();
serializer.TypeNameHandling = TypeNameHandling.Auto;
serializer.TraceWriter = traceWriter;
serializer.Serialize(stringWriter, collection);

Activity

  1. ghost locked as resolved and limited conversation to collaborators on Jun 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions