title: Read JSON using DataContractJsonSerializer
body: <p>I’m working on an application which accesses the TMDb.
  I use the DataContractJsonSerializer class to read the data returned by the TMDb API.</p>
  
  <p>There is one method of the API which returns a JSON which can look like:</p>
  
  <pre><code>{"id":550,"favorite":false,"rated":false,"watchlist":false}
  </code></pre>
  
  <p>or like:</p>
  
  <pre><code>{"id":49521,"favorite":false,"rated":{"value":5.5},"watchlist":false}
  </code></pre>
  
  <p>As you can see, the rated field in the first JSON is a Boolean and in the second one it isn’t.<br>
  Is there a good way to deal with this JSONs?</p>
  
  <p>Thanks for any Help</p>
  
  <p><strong>Edit:</strong>
  Reading the JSON:</p>
  
  <pre><code>[DataContract]
  public class JsonModelBase&lt;T&gt; where T : class, new()
  {
      public T DesterilizeJson(String jsonContent)
      {
          var bytes = Encoding.UTF8.GetBytes(jsonContent);
          using (var stream = new MemoryStream(bytes))
              return DesterilizeJson(stream);
      }
      public T DesterilizeJson(Stream jsonStream)
      {
          var serializer = new DataContractJsonSerializer(typeof(T));
          return (T)serializer.ReadObject(jsonStream);
      }
  }
  
  [DataContract]
  public class TmdbMovieAccountStates : JsonModelBase&lt;TmdbMovieAccountStates&gt;
  {
      [DataMember(Name = "id")]
      public Int32 Id { get; set; }
      [DataMember(Name = "favorite")]
      public Boolean Favorite { get; set; }
      [DataMember(Name = "rated")]
      public Boolean Rated { get; set; }
      [DataMember(Name = "watchlist")]
      public Boolean Watchlist { get; set; }
  }
  </code></pre>
  
  <p>Sample (if you want to try something out)</p>
  
  <pre><code>var json = "{\"id\":49521,\"favorite\":false,\"rated\":{\"value\":5.5},\"watchlist\":false}";
  var bytes = Encoding.UTF8.GetBytes(json);
  using ( var stream = new MemoryStream( bytes ) )
  {
      var serializer = new DataContractJsonSerializer(typeof(TmdbMovieAccountStates));
      var data = (TmdbMovieAccountStates)serializer.ReadObject(stream);
  }
  </code></pre>