I can’t serialize BaseResponse<NoContent>
object. But Device
object serialized successfully. I didnt find reason
var data = new BaseResponse<NoContent>();
var json1 = JsonSerializer.Serialize(data);
var data2 = new Device();
var json2 = JsonSerializer.Serialize(data2);
BaseResponse
Content :
public class BaseResponse<T> where T : class, new()
{
public BaseResponse()
{
Data = new T();
}
public T Data;
public string Status = "Success";
public List<string> Errors;
public BaseResponse<T> AddError(string message)
{
Errors ??= new List<string>();
Status = "Error";
Errors.Add(message);
return this;
}
}
Edit: Newtonsoft.Json can serialize both of them. I decided use Newtonsoft.
2
Answers
you have to add getters and setters
test
output
NoContent class
What’s missing is the option to include fields, by default
System.Text.Json
does not serialize fields, only properties.You can change this behaviour by supplying
JsonSerializerOptions
, e.g: