Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InvalidCastException occurs when a subclass obtains the Formatter of the base class. #201

Open
Liangjia0411 opened this issue Nov 21, 2023 · 3 comments
Labels
enhancement New feature or request

Comments

@Liangjia0411
Copy link

When the base class defines a Formatter through JsonCustomSerializerAttribute, the subclass may obtain the Formatter when it obtains the Formatter; but when the code runs to ResolverBase.cs Line147, an InvalidCastException will occur.
In order to solve this problem, when performing BuildFormatter, "type. GetCustomAttribute()" should not consider inheriting?

@Tornhoof
Copy link
Owner

I'm not sure I follow, can you please provide a repro for this?

@Liangjia0411
Copy link
Author

This is my test code. When I try to serialize an object of type ClassB, the following error will appear: "InvalidCastException: Unable to cast object of type 'MyTest.ClassAFormatter' to type 'SpanJson.IJsonFormatter2[MyTest.ClassB" ,System.Char]`'.
"

using SpanJson;

namespace MyTest
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var b = new ClassB { A = 1 };

            var jsonB = JsonSerializer.Generic.Utf16.Serialize(b);
        }
    }

    [JsonCustomSerializer(typeof(ClassAFormatter))]
    public class ClassA
    {
        public int A { get; set; }
    }

    public class ClassB : ClassA
    {
    }

    public class ClassAFormatter : ICustomJsonFormatter<ClassA>
    {
        public object Arguments { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }

        public static ClassAFormatter Default = new ClassAFormatter();

        public void Serialize(ref JsonWriter<char> writer, ClassA value)
        {
            throw new NotImplementedException();
        }

        public void Serialize(ref JsonWriter<byte> writer, ClassA value)
        {
            throw new NotImplementedException();
        }

        public ClassA Deserialize(ref JsonReader<byte> reader)
        {
            throw new NotImplementedException();
        }

        public ClassA Deserialize(ref JsonReader<char> reader)
        {
            throw new NotImplementedException();
        }

    }

}

@Tornhoof
Copy link
Owner

Yeah, I don't think that inheritance works with custom serializers, the concept is not really designed for that.

@Tornhoof Tornhoof added the enhancement New feature or request label Dec 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants