icubilla / jsonexserializer

Automatically exported from code.google.com/p/jsonexserializer
0 stars 0 forks source link

Add CollectionHandler attribute for Collection Classes #17

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Make it easier to register your own collection handlers with a
CollectionHandler attribute.  The attribute should be allowed on the class,
and on a property if feasible.

When the attribute appears on a class the TypeHandler should use that
CollectionHandler for the collection test.  It should first search the list
for an instance of the type by exact match, then by inheritance if no exact
match found.  Finally if no instance of the class is available in the
collectionhandlers then it should try and create it with a noarg constructor.

Original issue reported on code.google.com by elliott....@gmail.com on 10 Jul 2008 at 4:08

GoogleCodeExporter commented 9 years ago

Original comment by elliott....@gmail.com on 10 Jul 2008 at 4:59

GoogleCodeExporter commented 9 years ago

Original comment by elliott....@gmail.com on 10 Jul 2008 at 5:00

GoogleCodeExporter commented 9 years ago
Wanted to rethink this a little.  There may be a use case for just setting the 
item type.

[JsonExCollection(ItemType=typeof(string))]
public class MyStringCollection : CollectionBase {

   public void Add(string Item) {
      this.InnerList.Add(Item);
   }
}

In this situation the handler would first find an existing Handler to handle the
particular class.  Then it would create a wrapper around the handler.

public class CollectionHandlerWrapper : CollectionHandler {

   private CollectionHandler _inner;
   private Type _collectionType;
   private Type _itemType;

   public CollectionHandlerWrapper(CollectionHandler Inner, Type CollectionType, Type
ItemType) {
     _inner = Inner;
     _collectionType = CollectionType;
     _itemType = ItemType;
   }

   public override bool IsCollection(Type CollectionType) {
     return _collectionType.IsAssignableFrom(CollectionType);
   }

   public override Type GetItemType(Type CollectionType) {
     return _itemType;
   }

   // All other methods delegate to the _inner instance
}

   }

Original comment by elliott....@gmail.com on 10 Jul 2008 at 1:41

GoogleCodeExporter commented 9 years ago
Should probably add some type of support for this to the Config and maybe the 
Context
although you could do it in the context without any extra support it would just 
make
it easier.

Original comment by elliott....@gmail.com on 10 Jul 2008 at 1:44

GoogleCodeExporter commented 9 years ago

Original comment by elliott....@gmail.com on 19 Jul 2008 at 7:04