title: Json.NET deserialize or serialize json string and map properties to different property names defined at runtime
body: <p>I have the following JSON string:</p>
  
  <pre><code>{
      "values": {
          "details": {
              "property1": "94",
              "property2": "47",
              "property3": "32",
              "property4": 1
          },
          count: 4
      }
  }     
  </code></pre>
  
  <p>I am going to map this to the following model:</p>
  
  <pre><code>public class Details
  {
      public string property1 { get; set; }
      public string property2 { get; set; }
      public string property3 { get; set; }
      public int property4 { get; set; }
  }
  
  public class Values
  {
      public Details details { get; set; }
      public int count { get; set; }
  }
  
  public class RootObject
  {
      public Values values { get; set; }
  }
  </code></pre>
  
  <p>I want to be able to map the these property names to different names at runtime when deserializing this JSON string like this:</p>
  
  <pre><code>JsonConvert.DeserializeObject&lt;RootObject&gt;(jsonString);
  </code></pre>
  
  <p>For example, in the deserialization process, I want the deserialize the name of "property1" to "differen_property_name1" or "differen_property_name2" or "differen_property_name3".
  Because <strong>I am choosing the new name at runtime</strong> (the new name to which I will change the "property1" name to), I can't use the solution using JsonPropertyAttribute, as suggested here:</p>
  
  <p><a href="https://stackoverflow.com/questions/15915503/net-newtonsoft-json-deserialize-map-to-a-different-property-name">.NET NewtonSoft JSON deserialize map to a different property name</a></p>
  
  <p>One of the answers of the above question (Jack's answer) uses inheritance of DefaultContractResolver but it doesn't seem to work in that case. </p>
  
  <p><strong>Update</strong></p>
  
  <p>Later on, I needed to serialize the object I got from the deserialization and map the properties to different property names, defined at runtime.
  I used the same method as Brian proposed to do the serialization:</p>
  
  <p>I used the dictionary to map my new property names:</p>
  
  <pre><code>var map = new Dictionary&lt;Type, Dictionary&lt;string, string&gt;&gt;
  {
      { 
          typeof(Details), 
          new Dictionary&lt;string, string&gt;
          {
              {"property1", "myNewPropertyName1"},
              {"property2", "myNewPropertyName2"},
              {"property3", "myNewPropertyName3"},
              {"property4", "myNewPropertyName4"}
          }
      }
  };    
  </code></pre>
  
  <p>and then I used Brian's DynamicMappingResolver to serialize the object like this:</p>
  
  <pre><code>var settings = new JsonSerializerSettings
  {
      ContractResolver = new DynamicMappingResolver(map)
  };
  
  var root = JsonConvert.SerializeObject(myObjectInstance, settings);            
  </code></pre>