title: How to deserialize an untyped object using JSON.NET or DataContractJsonSerializer
body: <p>I'm trying to recreate an object when deserializing it. By reimplementing the Serialize and Deserialize methods so that I'll be able to use them independently. Since I'll be storing the serialized object in the database, I won't be able to access the object's type (class).
     The problem is: is there a way to deserialize and object without having the object's type, only it's JSON string? Any way to get it's type from the JSON string?</p>
  
  <p>Here are the methods:</p>
  
  <h3>DataContractJsonSerializer</h3>
  
  <pre><code>public string Serialize(object aoObject)
      {
        MemoryStream stream = new MemoryStream();
        DataContractJsonSerializer serializer = new     DataContractJsonSerializer(aoObject.GetType());
  
        serializer.WriteObject(stream, aoObject);
  
        return Encoding.Default.GetString(stream.ToArray());
      }
  
      public object Deserialize(string asObject)
      {
        MemoryStream stream = new MemoryStream(Encoding.Default.GetBytes(asObject));
  
        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(["the type of the object"]));
  
        return serializer.ReadObject(stream);
      }
  </code></pre>
  
  <h3>JSON.NET</h3>
  
  <pre><code>public string Serialize(object aoObject)
  {
    DefaultContractResolver dcr = new DefaultContractResolver();
    dcr.DefaultMembersSearchFlags |= System.Reflection.BindingFlags.NonPublic;
    JsonSerializerSettings jss = new JsonSerializerSettings();
    jss.TypeNameHandling = TypeNameHandling.All;
    jss.ContractResolver = dcr;
  
    string asObject = JsonConvert.SerializeObject(loObject, jss);
  
  }
  
  public object Deserialize(string asObject)
  {
    ["type of the object"] fake2 = JsonConvert.DeserializeObject(asObject);
  }
  </code></pre>