Is there an attribute to set a Json serialization name and then a deserialization name that is different. I have a JObject
that has names like Customer.Age
and I want it to deserialize to the Age
property and then stay Age
if serialized again. Anyone know if this is possible with something like a simple attribute or two?
Asked
Active
Viewed 86 times
0

David Carek
- 1,103
- 1
- 12
- 26
-
http://stackoverflow.com/questions/8796618/how-can-i-change-property-names-when-serializing-with-json-net – MethodMan Sep 16 '15 at 20:14
-
1The `JsonProperty` attribute has an overloaded constructor that takes in a name which is used during serialization instead of the property name itself. – Arian Motamedi Sep 16 '15 at 20:15
1 Answers
0
JsonProperty
did work I just had to set up the class a little differently. I made a constructor with params that had JsonProperty
on them and then in the body of the constructor I set the property name equal to the param name.
public class Customer
{
public Customer(
[JsonProperty("Customer.Name")] string name,
[JsonProperty("Customer.Email")] string email,
[JsonProperty("Customer.Country")] string country,
[JsonProperty("Customer.State")] string state,
[JsonProperty("Customer.City")] string city,
[JsonProperty("Customer.Phone")] string phone,
[JsonProperty("Customer.Company")] string company,
[JsonProperty("Customer.Region")] string region,
[JsonProperty("Customer.Fax")] string fax,
[JsonProperty("Customer.Age")] string age,
[JsonProperty("Customer.Territory")] string territory,
[JsonProperty("Customer.Occupation")] string occupation
)
{
Name = name;
Email = email;
Country = country;
State = state;
City = city;
PhoneNumber = phone;
Company = company;
Region = region;
FaxNumber = FaxNumber;
Age = age;
Territory = territory;
Occupation = occupation;
}
public string Name { get; set; }
public string Email { get; set; }
public string Country { get; set; }
public string State { get; set; }
public string City { get; set; }
public string PhoneNumber { get; set; }
public string Company { get; set; }
public string Region { get; set; }
public string FaxNumber { get; set; }
public string Age { get; set; }
public string Territory { get; set; }
public string Occupation { get; set; }
}
This lets me use customer.ToObject<Customer>()
on the JObject customer.

David Carek
- 1,103
- 1
- 12
- 26