So i am in a situation where my NewtonJson custom converters do not work with from body api calls. ( its using System.Text.Json by default to convert).
so currently i had a temporary solution to write some wrappers which will ultimately call Newtonjson until the text.json converters are written and tested.
what i want to do is to read the entire object as a string and pass it along to newtons converter
my StartUp.cs
services.AddControllers(options =>
options.Filters.Add<ApiExceptionFilterAttribute>())
.AddFluentValidation(x => x.AutomaticValidationEnabled = false)//ValidationBehaviour handle fluent validations.
.AddJsonOptions(x =>
{
x.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
x.JsonSerializerOptions.Converters.Add(new SystemModelConverter());
x.JsonSerializerOptions.IgnoreNullValues = true;
});
my converter class
public class SystemModelConverter : JsonConverter<ISystemModel>
{
public override bool CanConvert(Type typeToConvert)
{
return (typeToConvert.GetInterface(typeof(ISystemModel).Name) != null);
}
public override ISystemModel Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var value = new StringBuilder();
while(reader.Read())
{
var str = reader.GetString();
value.Append(str);
}
//passing the string along
return Newtonsoft.Json.JsonConvert.DeserializeObject(value.ToString(), CoreExtensions.GetJsonSerializerSettings()) as ISystemModel;
}
public override void Write(Utf8JsonWriter writer, ISystemModel value, JsonSerializerOptions options)
{
throw new NotImplementedException();
}