I have class and interface written in c# like this:
public interface ITestClass
{
string Name { get; set; }
int Age { get; set; }
List<ITestClass> Tests { get; set; }
}
public class TestClass :ITestClass
{
public string Name { get; set; }
public int Age { get; set; }
public List<ITestClass> Tests { get; set; }
}
I want to serialize & deserialize the created objects using newtonsoft json like this:
var testObj = new TestClass() { Name = "Roberto", Age = 43 };
testObj.Tests = new List<ITestClass>();
testObj.Tests.Add(new TestClass() { Name = "Mario", Age = 84 });
var serializedJsonString= JsonConvert.SerializeObject(testObj);
var documentObject = JsonConvert.DeserializeObject<ITestClass>(serializedJsonString);
The deserailization is failing with error message:
Newtonsoft.Json.JsonSerializationException: 'Could not create an instance of type Model.ITestClass. Type is an interface or abstract class and cannot be instantiated. Path 'Name', line 1, position 8.'
Below is the serialized json string:
{
"Name": "Roberto",
"Age": 43,
"Tests": [
{
"Name": "Mario",
"Age": 84,
"Tests": null
}
]
}
Please help.