I have a requirement to create a dynamic JSON response based on master model class in C#.
GIVEN
public class Person
{
public string Firstname {get; set;}
public string Lastname {get; set;}
public string Gender {get; set;}
}
var personObj = new Person {Firstname = "Foo", Lastname = "Bar", Gender = "M"};
var responseJsonTemplate1 = "{\"Firstname\":\"\",\"Gender\":\"\"}";
var responseJsonTemplate2 = "{\"Firstname\":\"\",\"Lastname\":\"\"}";
EXPECTATION
var responseJson1 = "{\"Firstname\":\"Foo\",\"Gender\":\"Male\"}";
var responseJson2 = "{\"Firstname\":\"Foo\",\"Lastname\":\"Bar\"}";
I want to generate JSON response from the personObj based on the template JSON string.
Is there any library to do such type of mapping?
Note:
- The template JSON will always follow the structure of Person class
- Since the response JSON will be dynamic and with any combinations of properties, we can't create a concrete class for each response JSON. It has to be dynamically generated.