You should declare one more model for PropertyStats. Otherwise, it will be kind of reverse engineering to modify the already created JSON string.
Like this:
public class ApiRequest
{
[JsonProperty(PropertyName = "name")]
public string Name { get; set; }
[JsonProperty(PropertyName = "description")]
public string Description { get; set; }
[JsonProperty(PropertyName = "privacy")]
public PropertyStats PrivacyStats { get; set; }
}
public class PropertyStats
{
[JsonProperty(PropertyName = "view")]
public string View { get; set; }
}
Then You can serialize it like this.
static void Main(string[] args)
{
ApiRequest objApiRequest = new ApiRequest()
{
Name = "Test1",
Description = "Testing Only",
PrivacyStats = new PropertyStats
{
View = "unlisted"
}
};
var serializedString = JsonConvert.SerializeObject(objApiRequest, Formatting.Indented);
Console.WriteLine(serializedString);
Console.ReadLine();
}
Output:
{
"name": "Test1",
"description": "Testing Only",
"privacy": {
"view": "unlisted"
}
}
In the future, if you want to add additional property to the privacy model then it will be easy to serialize it.
Like below:
public class PropertyStats
{
[JsonProperty(PropertyName = "view")]
public string View { get; set; }
[JsonProperty(PropertyName = "status")]
public int StatusCode { get; set; }
}
Serialize like it below:
static void Main(string[] args)
{
ApiRequest objApiRequest = new ApiRequest()
{
Name = "Test1",
Description = "Testing Only",
PrivacyStats = new PropertyStats
{
View = "unlisted",
StatusCode = 1
}
};
var serializedString = JsonConvert.SerializeObject(objApiRequest, Formatting.Indented);
Console.WriteLine(serializedString);
Console.ReadLine();
}
Output:
{
"name": "Test1",
"description": "Testing Only",
"privacy": {
"view": "unlisted",
"status": 1
}
}
privacywith one field, than implement custom converter and maintain it.