Skip to content

Latest commit

 

History

History
37 lines (29 loc) · 993 Bytes

README.md

File metadata and controls

37 lines (29 loc) · 993 Bytes

Intro

System.Text.Json serializer does not support DataContract and DataMember attributes prior .NET 7.0. System.Text.Json v7.x+ supports custom type resolvers. This library adds support for DataContract and DataMember attributes using a custom resolver.

Usage

[DataContract]
public class Person
{
    [DataMember(EmitDefaultValue = false, Order = 2)]
    public string FullName { get; set; }

    [DataMember(Order = 1)]
    public int Age { get; set; }

    [DataMember(EmitDefaultValue = false)]
    public Dictionary<string, string> Dict { get; set; }

    [IgnoreDataMember]
    public string DoNotShow { get; set; }
}

// Serialize with System.Text.Json
var options = new System.Text.Json.JsonSerializerOptions()
{
    TypeInfoResolver = System.Text.Json.Serialization.Metadata.DataContractResolver.Default,
};

Person person = new Person();

string json = System.Text.JsonSerializer.Serialize(person, options);

License

This library is under the MIT License.