3

I am looking a way to Serialize object using System.Text.Json but some property is ignored based on other property value.

for example:

public class Data
{
   public string Type { get; set; }
   public string A { get; set; }
   public string B { get; set; }
   public string BC { get; set; }
}

and i fill the class with this data

var data = new Data
{
   Type = "A",
   A = "data A",
   B = "data B",
   BC = "data BC"
};

when the Type value is A, i want to serialize property Type and A.

when the Type value is B, i want to serialize property Type, B, and BC.

and so on.

Is there a way to do this?

I ever made the Xml version with Custom Attribute, but when searching for System.Text.Json solution, didn't find any.

Here's my Xml version:

public static bool HasAttribute<TAttribute>(
            this PropertyInfo prop)
            where TAttribute : Attribute
{
    return prop.GetCustomAttributes(typeof(TAttribute), false).Any();
}

public static TValue GetAttributeValue<TAttribute, TValue>(
            this PropertyInfo prop,
            Func<TAttribute, TValue> valueSelector)
            where TAttribute : Attribute
{
    var att = prop.GetCustomAttributes(typeof(TAttribute), false).FirstOrDefault() as TAttribute;
    if (att != null)
    {
        return valueSelector(att);
    }
    return default(TValue);
}

private void GenerateAttributeOverrides<T>(string type, XmlAttributeOverrides attributeOverrides, PropertyInfo[] properties)
{
    foreach (var prop in properties)
    {
        var attributes = new XmlAttributes();
        if (prop.HasAttribute<ShouldSerializeAttribute>())
        {
            var acceptedTypes = prop.GetAttributeValue<ShouldSerializeAttribute, string[]>(x => x.AcceptedTypes);
            if (acceptedTypes.Contains(type))
            {
                attributes.XmlElements.Add(new XmlElementAttribute(prop.Name, prop.PropertyType);
            }
            else
            {
                attributes.XmlIgnore = true;
            }
        }
    }

thanks in advance.

5
  • 5
    You'll likely want to look at custom converters Commented Jan 15, 2021 at 10:01
  • @pinkfloydx33 how do you do with custom converters? any example for it? Commented Jan 15, 2021 at 10:14
  • Just Google it. Examples are found in the main documentation page for System.Text.Json Commented Jan 15, 2021 at 10:23
  • Are you using .NET 5 or .NET Core 3.x? Commented Jan 16, 2021 at 20:38
  • @dbc yes, i'm using NET 5 Commented Jan 18, 2021 at 0:29

1 Answer 1

3

After looking in Custom Converters, already found my answer.

here's my converter

public class DataConverter : JsonConverter<Data>
{
    public override Data Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        throw new NotImplementedException();
    }

    public override void Write(Utf8JsonWriter writer, Data value, JsonSerializerOptions options)
    {
        var properties = value.GetType().GetProperties();

        writer.WriteStartObject();

        foreach (var prop in properties)
        {
            if (prop.HasAttribute<ShouldSerializeAttribute>())
            {
                var acceptedTypes = prop.GetAttributeValue<ShouldSerializeAttribute, string[]>(x => x.AcceptedTypes);
                if (acceptedTypes != null && acceptedTypes.Contains(value.Type))
                {
                    var propValue = prop.GetValue(value);
                    writer.WritePropertyName(prop.Name);
                    JsonSerializer.Serialize(writer, propValue, prop.PropertyType, options);
                }
            }
        }

        writer.WriteEndObject();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.