1

I have a custom Collection to control the changes made to it, and revert changes if I need to, similar to the implementation of the IEditableObject

public class CollectionBO<TEntity> : Collection<TEntity> where TEntity : BOBase
{
    public List<TEntity> AddedEntities { get; set; }
    public List<TEntity> RemovedEntities { get; set; }

   
    public CollectionBO()
    {
        AddedEntities = new List<TEntity>();
        RemovedEntities = new List<TEntity>();
    }

}

I want to use that list also in the DTO of a rest api, to access the information of the records to be removed or added easily, but the problem I have is that it does not serialize the internal lists (AddedEntities, RemovedEntities), when they arrive to the server, those lists are always empty, the question is it possible to serialize a list and even its IList properties

 await (serverUrl).AppendPathSegment(endPoit)
                            .WithOAuthBearerToken(token)
                            .PutJsonAsync(CollectionBO);
David L
  • 32,885
  • 8
  • 62
  • 93
  • What internal lists? Those are public properties. What serializer are you using? How are you building the data? it is absolutely possible, but there's no way to know where the hangup is occurring based on this example. – David L Jan 11 '21 at 20:47
  • Hi thanks for replying, the internal lists are the AddedEntities and RemovedEntities properties. if you want to test you should create a CollectionBO instance, and then add any objects to the AddedEntities and RemovedEntities lists. To serialize I use JsonConvert.SerializeObject (CollectionBO instance), you will see that it doesn't serialize the objects of the AddedEntities and RemovedEntities lists – Fabian Wesling Jan 11 '21 at 23:21
  • Possible duplicate of [How do I get json.net to serialize members of a class deriving from List?](https://stackoverflow.com/q/21265629/10263) – Brian Rogers Jan 12 '21 at 16:35

1 Answers1

1

The issue is caused by a mismatch between your inheritance structure and your desired output structure.

By inheriting from Collection<T>, Newtonsoft invokes the JsonArrayContract due to your type implementing ICollection<>. As a result, when it attempts to serialize, it outputs an array: "[]".

That said, your object structure is an object with containing arrays. In order to force the serializer to treat your CollectionBO<TEntity> as an object, you need to decorate it with the [JsonObject] attribute.

[JsonObject]
public class CollectionBO<TEntity> : System.Collections.ObjectModel.Collection<TEntity> 
    where TEntity : BOBase
{
      public List<TEntity> AddedEntities { get; set; }
      public List<TEntity> RemovedEntities { get; set; }


      public CollectionBO()
      {
            AddedEntities = new List<TEntity>();
            RemovedEntities = new List<TEntity>();
      }
}

This allows the serializer to treat it correctly:

{"AddedEntities":[{"Id":1},{"Id":2}],"RemovedEntities":[{"Id":3},{"Id":4}],"Count":0}

As a word of caution, extending collection types is rarely advisable since you can create all sorts of odd behaviors, such as the behavior with the Newtonsoft serializer. In addition, note that the Count property is serialized to 0, even though you have underlying objects. While you can certainly make this work, be advised that you will continue to run into unexpected behavior when extending a collection type but not treating it as a collection.

David L
  • 32,885
  • 8
  • 62
  • 93