How to ignore properties of the parent class? #102952
Replies: 3 comments
-
Right now, realizing this would be a bit of a pain. It might be worthwhile to wait a little, if you can... In the near future, it would be possible to modify the JsonTypeInfo contract of types of interest by filtering out / removing such JsonPropertyInfo's from the contract whose The commit introducing |
Beta Was this translation helpful? Give feedback.
-
I implemented 3-way Converter and it works fine for me(Write method only,Read method will broke, Probably because I used public class JsonIgnoreConverter<T> : JsonConverter<T>
{
private readonly List<string> _ignoredProperties = [];
public JsonIgnoreConverter(List<string> ignoredProperties)
{
_ignoredProperties = ignoredProperties;
}
public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return JsonSerializer.Deserialize<T>(ref reader, options);
}
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
{
writer.WriteStartObject();
var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(p => p.GetCustomAttribute<JsonIgnoreAttribute>() is null)
.OrderBy(p => p.GetCustomAttribute<JsonPropertyOrderAttribute>()?.Order);
foreach (var property in properties)
{
if (!_ignoredProperties.Contains(property.Name))
{
writer.WritePropertyName(property.Name);
JsonSerializer.Serialize(writer, property.GetValue(value), options);
}
}
writer.WriteEndObject();
}
} public class JsonIgnoreBasePropertyConverter<T, TBase> : JsonConverter<T>
{
public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return JsonSerializer.Deserialize<T>(ref reader, options);
}
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
{
writer.WriteStartObject();
var baseType = typeof(TBase);
var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(p => p.DeclaringType != baseType && p.GetCustomAttribute<JsonIgnoreAttribute>() is null)
.OrderBy(p => p.GetCustomAttribute<JsonPropertyOrderAttribute>()?.Order);
foreach (var property in properties)
{
writer.WritePropertyName(property.Name);
JsonSerializer.Serialize(writer, property.GetValue(value), options);
}
writer.WriteEndObject();
}
}
public class JsonIgnoreBasePropertyConverter<T> : JsonConverter<T>
{
public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return JsonSerializer.Deserialize<T>(ref reader, options);
}
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
{
writer.WriteStartObject();
var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
.Where(p => p.GetCustomAttribute<JsonIgnoreAttribute>() is null)
.OrderBy(p => p.GetCustomAttribute<JsonPropertyOrderAttribute>()?.Order);
foreach (var property in properties)
{
writer.WritePropertyName(property.Name);
JsonSerializer.Serialize(writer, property.GetValue(value), options);
}
writer.WriteEndObject();
}
} |
Beta Was this translation helpful? Give feedback.
-
With the help of ChatGPT, I gradually implemented public static class JsonConverterHelper
{
public static T? Read<T>(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
// Finding constructors with the [JsonConstructor] feature
var constructor = typeToConvert.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
.FirstOrDefault(c => c.GetCustomAttribute<JsonConstructorAttribute>() is not null);
if (constructor is null)
{
// If the [JsonConstructor] feature is not found, use the default deserialization
return JsonSerializer.Deserialize<T>(ref reader, options);
}
// Getting the arguments to the constructor
var parameters = constructor.GetParameters();
object?[] parameterValues = new object?[parameters.Length];
if (reader.TokenType != JsonTokenType.StartObject)
{
throw new JsonException("Expected StartObject token.");
}
// Dictionary for holding attributes and attribute values
var propertyValues = new Dictionary<PropertyInfo, object?>();
// Reading JSON objects
while (reader.Read())
{
if (reader.TokenType == JsonTokenType.EndObject)
{
break;
}
if (reader.TokenType != JsonTokenType.PropertyName)
{
throw new JsonException("Expected PropertyName token.");
}
string propertyName = reader.GetString()!;
reader.Read();
// Find Parameters
var parameter = parameters.FirstOrDefault(p => String.Equals(p.Name, propertyName, StringComparison.InvariantCultureIgnoreCase));
if (parameter is not null)
{
// Deserializing Parameters with JsonSerializer
object? parameterValue = JsonSerializer.Deserialize(ref reader, parameter.ParameterType, options);
parameterValues[Array.IndexOf(parameters, parameter)] = parameterValue;
}
else
{
// If the property is not in the constructor argument, try to set it to the object's property
var property = typeToConvert.GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance);
if (property?.CanWrite is true && property.GetCustomAttribute<JsonIgnoreAttribute>() is null)
{
object? propertyValue = JsonSerializer.Deserialize(ref reader, property.PropertyType, options);
propertyValues[property] = propertyValue;
}
else
{
reader.Skip();
}
}
}
// Create an object using a constructor with [JsonConstructor].
var instance = (T)constructor.Invoke(parameterValues);
// Setting property values to instances using reflection
foreach (var kvp in propertyValues)
{
kvp.Key.SetValue(instance, kvp.Value);
}
return instance;
}
} |
Beta Was this translation helpful? Give feedback.
-
How to impl Read/Write methods?
That's the kind of grammar I'd expect:
Beta Was this translation helpful? Give feedback.
All reactions