I have a problem where I would like to be able to convert a json path into an object path based on the model used by Newtonsoft.Json to map between json and an my c# object model.
Below I have an example of some complex types that are serialised into json. Given I have the json path of a child property my_child.uppercase
, I would like to convert that into the object property path Child.Uppercase
.
Is it possible to query the model used by Newtonsoft.Json to map between the two worlds? Or do i have to build it from scratch using reflection?
public class ParentModel
{
// serialised into another name!
[JsonProperty(PropertyName = "my_child")]
public ChildModel Child {get; set;}
}
//serialised into another case!
[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
public class ChildModel
{
public string Uppercase {get; set;}
}
void Main()
{
var item = new ParentModel() {
Child = new ChildModel() {
Uppercase = "Joe"
}
};
// outputs {"my_child":{"uppercase":"Joe"}}
JsonConvert.SerializeObject(item).Dump();
var expected = "Child.Uppercase";
var actual = ConvertJsonPropertiesToObjectProperties("my_child.uppercase");
Debug.Assert(expected == actual);
}
string ConvertJsonPropertiesToObjectProperties(string jsonPropertyNames){
return "implement me!!";
}