I am working on .NET 5 API application xUnit Tests. I am receiving JSON object in the following format. In the JSON object, I have an nested object with the title 'data'. I need to deserialize data object into class. The data object is of type Dynamic. The whole object can deserialize to ResponseResult class which I don't want, I want to pick only object with title 'data' and deserialize to specific class
error
var actualResultData = JsonConvert.DeserializeObject<ResponseResult>(actualResult.Content).Data;
var dynamicDataObject = JsonConvert.DeserializeObject<HandHeldDeviceDataView>(actualResultData);
The best overloaded method match for 'Newtonsoft.Json.JsonConvert.DeserializeObject<HandHeld.Domain.DTOs.DataView.HandHeldDeviceDataView>(string)' has some invalid arguments
Test Method
[Fact]
public async Task GetHandHeldByIMEI_ShouldReturn_DataTypeOf_HandHeldWrapperDataView()
{
//Arrange
var fixture = new Fixture();
var imei = "imeiNo";
var handHeldWrapperMoq = fixture.Create<HandHeldWrapperDataView>();
var returnResultMoq = ResponseResultHelper.SuccessfulResult(handHeldWrapperMoq.HandHeld, handHeldWrapperMoq.ResultSummary);
var returnSerializeObjectMoq = JsonConvert.SerializeObject(returnResultMoq, Formatting.None);
mediatorMoq.Setup(x => x.Send(It.IsAny<GetHandHeldByIMEI>(), It.IsAny<CancellationToken>())).ReturnsAsync(handHeldWrapperMoq);
//Act
var actualResult = await sut.GetHandHeldByIMEI(imei) as ContentResult;
var actualResultData = JsonConvert.DeserializeObject<ResponseResult>(actualResult.Content).Data; // this is not working
//Assert
//Assert.Equal(returnSerializeObjectMoq, actualResult.Content);
// Assert.IsAssignableFrom<HandHeldDeviceDataView>(actualResultData);
}
JSON Object
{
"Ok":true,
"RecordCount":118,
"Error":null,
"InnerExceptionMessage":null,
"ExecutionMessage":"ExecutionMessage40f96da6-ae4d-41a4-b98d-5e49021959d7",
"Data":{
"HandHeldId":68,
"ParkingAttendantId":92,
"IMEI":"IMEI220d7976-f806-4ba7-ad06-e79bc6227f64",
"HandHeldAppId":"HandHeldAppId7cc85661-4d7d-4d78-a7d8-f183de88b627",
"SerialNumber":"SerialNumber063573ec-bcf6-408b-934f-0834d8824bca",
"PhoneNumber":"PhoneNumber639f894e-4dd7-4e24-899d-a28f031331d6",
"PrinterSerialNo":"PrinterSerialNoaf0d0082-5b21-476f-8cb8-c6c9f53d81a3",
"LastSeenDateTime":"2019-10-08T20:43:05.829758+01:00",
"LastPrintDateTime":"2022-11-01T05:39:58.3454711+00:00",
"IsActive":true,
"IsDeleted":false,
"CreatedBy":87,
"UpdatedBy":16,
"CreatedDateTimeUtc":"2022-05-30T04:22:53.2192713+01:00",
"UpdatedDateTimeUtc":"2019-12-16T03:06:14.3078736+00:00",
"testField":247
}
}
ResponseResult Class
in this class I have property 'data' dynamic which I want to deserialize to specific class
public class ResponseResult
{
public bool Ok { get; set; }
public int RecordCount { get; set; }
public string Error { get; set; }
public string InnerExceptionMessage { get; set; }
public string ExecutionMessage { get; set; }
public dynamic Data { get; set; }
}
and I have found the answer;
var actualResultData = JsonConvert.DeserializeObject<ResponseResult>(actualResult.Content).Data.ToObject<HandHeldDeviceDataView>();