icubilla / jsonexserializer

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

Support Collection properties with no Setter #11

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
It is often desirable to expose a collection from an object via only a
getter property and force clients to use the modifier methods on the
collection's interface rather than replacing the whole collection.  This is
especially important if the exposed property is an interface or abstract
type, but the class internally depends on a specific implementation.

Example:
public class Customer {
  private List<Order> _orders = new List<Order>();

  public ICollection<Order> Orders {
     get { return _orders; }
  }
}

Currently the serializer only supports properties with both a public getter
and setter.  Thus, a class has to provide a setter for serialization to
work.  Support should be added to allow either a collection, dictionary or
object type to work with only getter.  

1.  An attribute will be created to indicate that the property should be
serialized as it normally wouldn't be in this case and that should still be
the default behavior.
2. Upon deserialization instead of creating an instance of the class to be
held for the property, the value of the property will be retrieved from the
parent instance.  In this case the type used to expose metadata should be
the declared type of the property and not the actual object type.  
3. The desired type can be overriden in the attribute declaration if desired.
4. The attribute should be ignored on primitive properties
5. The converter attribute will be ignored and any converters defined for
the property type will be ignored

Attribute example:
[JsonExSerialize]  // use declared type on the property
public IOrder Order { ...

[JsonExSerialize(typeof(Acme.OrderImpl))]
public IOrder Order { ...

Usage:
public class Customer {
  private List<Order> _orders = new List<Order>();

  [JsonExSerialize]
  public ICollection<Order> Orders {
     get { return _orders; }
  }
}

Original issue reported on code.google.com by elliott....@gmail.com on 21 Jan 2008 at 2:41

GoogleCodeExporter commented 9 years ago

Original comment by elliott....@gmail.com on 10 Feb 2008 at 4:34

GoogleCodeExporter commented 9 years ago

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