I have an Action in Controller which has an argument of type Class1
[HttpPost]
public IActionResult Create(Class1 c)
{
}
I send data to it using JQuery's Ajax function.
I would like to write my own code to deserialize SampleProperty
:
class Class1
{
public string SampleProperty { get; set; }
}
Is it possible? I would like to override default deserialization.
I've tried many things, for example writing converter:
public class SamplePropertyConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(string);
}
public override object ReadJson(JsonReader reader, Type objectType,
object existingValue, JsonSerializer serializer)
{
if ((string)existingValue == "abc")
return "abc123";
else
return existingValue;
}
public override void WriteJson(JsonWriter writer, object value,
JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override bool CanWrite => false;
public override bool CanRead => true;
}
and then using it like this:
class Class1
{
[JsonConverter(typeof(SamplePropertyConverter))]
public string SampleProperty { get; set; }
}
but in such case SamplePropertyConverter
is not used at all.
I also tried to add it in Startup, but then I see that it enters CanConvert
method, but only for some other requests, not sending Class1
to Create
Action.