title: How to have a WCF DataContract with a json dynamic member
body: <p>at the project I am working on, we have a requirement to have a DataContract that can contain some undefined JSON.</p>
  
  <p>The DataMember is some JSON that makes sense to the client only. We want to allow the client to send us json we do not know about.</p>
  
  <p>Example:</p>
  
  <pre><code>public class Contract
  {
      [DataMember]
      public int clientId;
      [DataMember]
      public string json;
  }
  </code></pre>
  
  <p>Obviously, having a contract defined like that would require the client to escape the json like this:</p>
  
  <pre><code>{
      "clientId":1,
      "json": "{\"test\":\"json\"}"
  }
  </code></pre>
  
  <p>Obviously, this is not what we need. The json the client should sent us should look like this:</p>
  
  <pre><code>{
      "clientId":1,
      "json": {"test":"json"}
  }
  </code></pre>
  
  <p>Possible solutions we investigated:</p>
  
  <ol>
  <li>use Stream as contract parameters for the request's body. Works but puts the work on our side instead of using the framework.</li>
  <li>Defining "json" as a DynamicObject. Does not work. Was not able to get the property written properly.</li>
  <li>Using Newtonsoft library, change the default contract serializer in the WCF endpoint to serialize all inputs to a JObject. We mst also handle serialization on the request, and it causes problems in our application. We'd prefer to avoid this way.</li>
  </ol>
  
  <p>Does anyone have a possible solution to this problem?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>The service offers rest json resources. It defines a single endpoint with webHttpBinding.
  The operation is defined like this (stripped down for simplicity):</p>
  
  <pre><code>[WebInvoke(Method = "POST", UriTemplate = "...", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
  [OperationContract]
  Stream Create(Contract c);
  </code></pre>
  
  <p>Also, the service is decorated with following attribute:</p>
  
  <pre><code>[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
  </code></pre>
  
  <p>Thank you.
  JF</p>