Configure JSON member mapping and behavior with attributes
masterYou can control how SpanJson handles specific members of your classes using several attributes:
[DataMember(Name="MemberName")]: Sets a specific name for the field in the JSON output.[IgnoreDataMember]: Prevents a specific member from being serialized or deserialized.[JsonConstructor]: Specifies which constructor to use during deserialization. If parameter names match member names (case-insensitive), they are mapped automatically. You can also use[JsonConstructor(nameof(Member1), nameof(Member2))]to explicitly map constructor parameters to specific members.[JsonCustomSerializer(typeof(FormatterType))]: Directs SpanJson to use a specificICustomJsonFormatter<T>for a member or type instead of the default formatter.[JsonExtensionData]: When applied to anIDictionary<string, object>, all unknown properties during deserialization are added to this dictionary. During serialization, all entries in the dictionary are written as additional properties of the object.
public class ExtensionTest
{
public string Key;
public string Value;
[JsonExtensionData]
public IDictionary<string, object> AdditionalValues { get; set; }
}
public class Input
{
[DataMember(Name="custom_name")]
public string Text { get; set; }
[IgnoreDataMember]
public string Secret { get; set; }
}