Exception when serializing an abstract generic property

Aug 14, 2008 at 12:58 PM
Hi guys,

I just ran into a little problem with the json serializer. I have an abstract base class that sits on top of my domain model. This class is generic so I can pass the Type of the database identifier to it.
When I serialize such an object I get the following exception:

Newtonsoft.Json.JsonSerializationException: A member with the name 'Id' already exists on NewtonSoftBug.Tests+AbstractGenericBase`1[System.Int32]. Use the JsonPropertyAttribute to specify another name.

Here is a test case that shows this behavior.

    [TestFixture]
    public class Tests
    {
        private abstract class AbstractGenericBase<TKey>
        {
            public abstract TKey Id { get; set; }
        }

        private class GenericImpl : AbstractGenericBase<int>
        {
            public override int Id { get; set; }
        }

        [Test]
        public void GenericAbstractProperty()
        {
            var serializer = new JsonSerializer();

            var writer = new StringWriter();
            serializer.Serialize(writer, new GenericImpl());
        }
    }

The suggested workaround (specify a JsonPropertyAttribute) work fine but I still believe this is a bug.

Thoughts on this?

cheers and thanks for all the great work!

Ernst.
Coordinator
Aug 16, 2008 at 7:38 AM
Thanks for submitting such a detailed report of the bug!

I've fixed this issue and checked the change into CodePlex. Try building from the latest source and see how you go.

If you're interested the issue is caused by weirdness in the .NET GetProperties method around abstract generic types - http://forums.msdn.microsoft.com/en-US/netfxbcl/thread/b5abbfee-e292-4a64-8907-4e3f0fb90cd9/

~ James
Aug 18, 2008 at 11:31 AM
Hi James,

thanks for the fix and link detailing the origin of this issue :)
I'll give it a spin when I'm at work tomorrow.

cheers,

Ernst.

On Sat, Aug 16, 2008 at 10:54 AM, JamesNK <notifications@codeplex.com> wrote:
> From: JamesNK
>
> Thanks for submitting such a detailed report of the bug!
>
> I've fixed this issue and checked the change into CodePlex. Try building
> from the latest source and see how you go.
>
> If you're interested the issue is caused by weirdness in the .NET
> GetProperties method around abstract generic types -
> http://forums.msdn.microsoft.com/en-US/netfxbcl/thread/b5abbfee-e292-4a64-8907-4e3f0fb90cd9/
>
> ~ James
>
> Read the full discussion online.
>
> To add a post to this discussion, reply to this email
> ([email removed])
>
> To start a new discussion for this project, email
> [email removed]
>
> You are receiving this email because you subscribed to this discussion on
> CodePlex. You can unsubscribe on codePlex.com.
>
> Please note: Images and attachments will be removed from emails. Any posts
> to this discussion will also be available online at codeplex.com