Feature request: conditional serialization support in DataMember and DataContractSerializer RRS feed

  • Question

  • I would like to pass the same information through several application layers. The data is being serialized on each layer boundary. Using the same serialization mechanism, I would like to specify which subset of the information (which properties) should be serialized. The options I can think of:

    • using different transport classes (data contracts) for each serialization and writing the transformation between them
    • extending the serialization mechanism to recognize custom metadata (e.g. custom attributes) in order to decide whether a certain property should be serialized or not (the same transport object can be used)
    • passing a context to the serialization mechanism and specifying conditions on the serialization attributes (the same transport object can be used), that would look like the following:
    public class MyTransportClass
    [DataMember<TransportContextEnum>(Condition = context => context == TransportContextEnum.InternalServiceToWebAPI)]
    public string SomeProperty { get; set; }
    /// ... later in serialization code
    var serializer = new DataContractJsonSerializer<TransportContextEnum>(typeof(MyTransportClass));
    serializer.Serialize(new MyTransportClass { SomeProperty = "Some value" }, TransportContextEnum.InternalServiceToWebAPI);

    Tuesday, July 9, 2013 3:26 PM