Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-120

ToJson() serialization of Abstract classes

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 0.9
    • Affects Version/s: 0.9
    • Component/s: None
    • None

      If I do this:
      using System;

      using MongoDB.Bson;

      namespace BsonTest
      {
      internal sealed class Program
      {
      private static void Main(string[] args)
      {
      Payload payload = new ChildOfPayLoad

      { name="Justin", email="justin@domain.com" }

      ;

      payload.ToJson();

      Console.Write("Press any key to continue . . . ");
      Console.ReadKey(true);
      }
      }

      public abstract class Payload
      {
      private static readonly string _myId = "something";
      public string myId
      {
      get

      { return _myId; }

      set { }
      }
      public abstract string someType

      { get; set; }
      }

      public class ChildOfPayLoad : Payload
      {

      public string name { get; set; }

      public string email

      { get; set; }

      public override string someType
      {
      get

      { return "Something"; }

      set { }
      }
      }

      }
      I get:

      System.ArgumentException: An item with the same key has already been added.
      at static void System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
      at void System.Collections.Generic.Dictionary`2[System.String,MongoDB.Bson.DefaultSerializer.BsonMemberMap].Insert(String key, BsonMemberMap value, Boolean add)
      at void System.Collections.Generic.Dictionary`2[System.__Canon,System.__Canon].Add(__Canon key, __Canon value)
      at void MongoDB.Bson.DefaultSerializer.BsonClassMap.Freeze() in d:\justin's projects\mongo-csharp\Bson\DefaultSerializer\BsonClassMap.cs:line 303
      at static BsonClassMap MongoDB.Bson.DefaultSerializer.BsonClassMap.LookupClassMap(Type classType) in d:\justin's projects\mongo-csharp\Bson\DefaultSerializer\BsonClassMap.cs:line 186
      at void MongoDB.Bson.DefaultSerializer.BsonClassMapSerializer.Serialize(BsonWriter bsonWriter, Type nominalType, Object value, Boolean serializeIdFirst) in d:\justin's projects\mongo-csharp\Bson\DefaultSerializer\BsonClassMapSerializer.cs:line 160
      at static void MongoDB.Bson.Serialization.BsonSerializer.Serialize(BsonWriter bsonWriter, Type nominalType, Object value, Boolean serializeIdFirst) in d:\justin's projects\mongo-csharp\Bson\Serialization\BsonSerializer.cs:line 257
      at static void MongoDB.Bson.Serialization.BsonSerializer.Serialize(BsonWriter bsonWriter, Object value, Boolean serializeIdFirst) in d:\justin's projects\mongo-csharp\Bson\Serialization\BsonSerializer.cs:line 232
      at static String MongoDB.Bson.BsonExtensionMethods.ToJson(Object obj, Boolean serializeIdFirst, BsonJsonWriterSettings settings) in d:\justin's projects\mongo-csharp\Bson\BsonExtensionMethods.cs:line 110
      at static String MongoDB.Bson.BsonExtensionMethods.ToJson(Object obj, BsonJsonWriterSettings settings) in d:\justin's projects\mongo-csharp\Bson\BsonExtensionMethods.cs:line 120
      at static String MongoDB.Bson.BsonExtensionMethods.ToJson(Object obj) in d:\justin's projects\mongo-csharp\Bson\BsonExtensionMethods.cs:line 93
      at static void BsonTest.Program.Main(String[] args) in c:\Users\DearingJ\Documents\SharpDevelop Projects\BsonTest\BsonTest\Program.cs:line 16

        There are no Sub-Tasks for this issue.

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            zippy1981 Justin Dearing
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: