I have a class library project in .net. I can't use 3rd party assemblies, only Microsoft assemblies allowed.
I have a return class from web API like below. I try to Deserialize string to this class and it's not working but it works for other classes. I think the problem is the 'T' class accepting.
my class;
public class ApiResponse<T>
{
public T Value { get; set; }
public bool HasError { get; set; }
public string ErrorMessage { get; set; }
public string DetailedErrorMessage { get; set; }
public string ErrorCode { get; set; }
public ApiResponse(string errorCode, string errorMessage, string detailedErrorMessage = "")
{
this.ErrorMessage = errorMessage;
this.ErrorCode = errorCode;
this.HasError = true;
this.DetailedErrorMessage = detailedErrorMessage;
}
public ApiResponse(T value)
{
this.Value = value;
this.HasError = false;
ErrorMessage = string.Empty;
}
public ApiResponse()
{
this.HasError = false;
ErrorMessage = string.Empty;
}
public ApiResponse(OLException e)
{
ErrorCode = e.ErrorCode;
ErrorMessage = e.Message;
HasError = true;
DetailedErrorMessage = e.StackTrace;
}
public ApiResponse(Exception e)
{
ErrorMessage = e.Message;
HasError = true;
DetailedErrorMessage = e.StackTrace;
}
}
and here is my Deserialize function.
private T JsonDeserialize<T>(string jsonString)
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
T obj = (T)ser.ReadObject(ms);
return obj;
}
and here is the How I use the following method
ApiResponse<bool> response = JsonDeserialize<ApiResponse<bool>>(responseString);
Response string -->
{"value":false,"hasError":true,"errorMessage":"user1 client already exists.","detailedErrorMessage":"","errorCode":null}
There is no error just response class seems null after deserialize but as you see, the response string has some values.
