Deserializing using JsonSerialize.DeserializeAsync
and a custom converter, e.g.
public class MyStringJsonConverter : JsonConverter<string>
{
public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return reader.GetString();
}
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
}
Here I would get all string
properties, which is kind of okay, though is there any way to check the property name for a given value, e.g. something like this, where to process only the Body
property:
class MyMailContent
{
public string Name { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
}
public class MyStringJsonConverter : JsonConverter<string>
{
public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.PropertyName.Equals("Body"))
{
var s = reader.GetString();
//do some process with the string value
return s;
}
return reader.GetString();
}
}
Or is there some other way to single out a given property?
Note, I am looking for a solution using System.Text.Json
.
System.Text.Json does not make the parent property name, or more generally the path to the current value, available inside JsonConverter<T>.Read()
. This information is tracked internally -- it's in ReadStack.JsonPath()
-- but ReadStack
is internal and never passed to applications code.
However, as explained in Registration sample - [JsonConverter] on a property, you can apply your MyStringJsonConverter
directly to public string Body { get; set; }
by using JsonConverterAttribute
:
class MyMailContent
{
public string Name { get; set; }
public string Subject { get; set; }
[JsonConverter(typeof(MyStringJsonConverter))]
public string Body { get; set; }
}
By doing this, MyStringJsonConverter.Read()
and .Write()
will fire only for MyMailContent.Body
. Even if you have some overall JsonConverter<string>
in JsonSerializerOptions.Converters
, the converter applied to the property will take precedence:
During serialization or deserialization, a converter is chosen for each JSON element in the following order, listed from highest priority to lowest:
[JsonConverter]
applied to a property.- A converter added to the
Converters
collection.[JsonConverter]
applied to a custom value type or POCO.
(Note this differs partially from Newtonsoft where converters applied to types supersede converters in settings.)