Hi,
consider the following class and struct
public class Entity {
public IdType Id {get;set;}
public string Data {get;set;}
}
[TypeConverter(IdTypeConverter))]
public struct IdType {
... any data ...
}
The IdTypeConverter can convert the IdType struct from and to string.
Now, what I want is this class to be serializable for AJAX, WCF and ViewState. The senario could be a WCF web service which provides as array of Entity[] to a DataSource. And a custom control which binds to the datasource, saves this class to it's ViewState and sends the data to client side code.
This could be achieved easily by simply adding a [Serializable] attribute to all serialized types. But I don't want the IdType to be serialized, but to be converted to a string. Thus the JSON representation should be
{ 'Id'=>'StringRepresentationOfId', 'Data'=>'foo' }
This would analogously be the optimal serialization for WCF and ViewState.
Another solution would be to write another class
public class JsonEntity {
public JsonEntity(Entity from) {
Id = from.Id;
Data = from.Data;
}
public string Id {get;set;}
public string Data {get;set;}
}
and use this for JsonSerialization. But I don't like this, because this would imply that the control which is sending the data to the client knows about the Entity type.
The actual question is: Is it possible to customize JsonSerialization with attributes without breaking WCF and ViewState serialization?
EDIT: An answer like "That' impossible" would satify me, since I'd stop trying.