The short answer NOT possible, IMO what you are asking for is NOT possible and has not thing to do with api.
IMO you have 3 options either use
- dynamic type
- custom object type
- Dictionay type
As this answer mention,
Whatever is the reason you want to do that - the reason is simple:
JObject implements IDictionary and this case is treated in a special
way by Json.NET. If your class implements IDictionary - Json.NET will
not look at properties of your class but instead will look for keys
and values in the dictionary. So to fix your case you can do this
Let's get some details:
To prove my first point, try just to create a simple console app with newton.json with your input as string:
var input = "{\"Bye\": \"string\"}";
With dynamic it will works fine:
var result = JsonConvert.DeserializeObject<dynamic>(input);
Console.WriteLine(JsonConvert.SerializeObject(result));
Now with customize object like:
public class TestMe
{
public string Bye { get; set; }
}
and
var result = JsonConvert.DeserializeObject<TestMe>(input);
Console.WriteLine(JsonConvert.SerializeObject(result));
Works fine as well.
Now lets take your approach:
public class TestMe : JObject
{
}
Testing it with following, it will break:
var result = JsonConvert.DeserializeObject<TestMe>(input);
Console.WriteLine(JsonConvert.SerializeObject(result));
Now lets try with Dictionary:
public class TestMe
{
[JsonExtensionData]
public Dictionary<string, JToken> MyProperties { get; set; } = new Dictionary<string, JToken>();
}
And test it
var result = JsonConvert.DeserializeObject<TestMe>(input);
Console.WriteLine(JsonConvert.SerializeObject(result));
Will also works fine.
Now I have presented 3 options.