title: Object serializable for AJAX, WCF and ViewState
body: <p>consider the following class and struct</p>
  
  <pre><code>public class Entity {
      public IdType Id {get;set;}
      public string Data {get;set;}
  }
  
  [TypeConverter(IdTypeConverter))]
  public struct IdType {
      ... any data ...
  }
  </code></pre>
  
  <p>The IdTypeConverter can convert the IdType struct from and to string.</p>
  
  <p>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.</p>
  
  <p>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 </p>
  
  <pre><code>{ 'Id'=&gt;'StringRepresentationOfId', 'Data'=&gt;'foo' }
  </code></pre>
  
  <p>This would analogously be the optimal serialization for WCF and ViewState. </p>
  
  <p>Another solution would be to write another class</p>
  
  <pre><code>public class JsonEntity {
      public JsonEntity(Entity from) {
          Id = from.Id;
          Data = from.Data;
      }
      public string Id {get;set;}
      public string Data {get;set;}
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>The actual question is: Is it possible to customize JsonSerialization with attributes without breaking WCF and ViewState serialization? </p>
  
  <p>EDIT:
  An answer like "That' impossible" would satify me, since I'd stop trying.</p>