JsonIgnore 属性在抽象中不起作用 class
JsonIgnore attribute not work in abstract class
我不想将某些属性保存在 json 文件中,所以我使用了 [JsonIgnore] 属性但不起作用,所有属性都已保存
public abstract class SettingBase
{
[JsonIgnore]
public abstract string FileName { get; set; }
public void Save()
{
JsonFile.Save(FileName, this);
}
}
public static class JsonFile
{
public static void Save<T>(string fileName, T @object)
{
using (StreamWriter writer = File.CreateText(fileName))
{
options = new JsonSerializerOptions();
options.Converters.Add(new PolymorphicJsonConverter<T>());
string json = JsonSerializer.Serialize(@object, options);
writer.Write(json);
}
}
}
我也是用下面的转换器来解决多态问题
public class PolymorphicJsonConverter<T> : JsonConverter<T>
{
public override bool CanConvert(Type typeToConvert)
{
return typeof(T).IsAssignableFrom(typeToConvert);
}
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
{
if (value is null)
{
writer.WriteNullValue();
return;
}
writer.WriteStartObject();
foreach (var property in value.GetType().GetProperties())
{
if (!property.CanRead)
continue;
var propertyValue = property.GetValue(value);
writer.WritePropertyName(property.Name);
JsonSerializer.Serialize(writer, propertyValue, options);
}
writer.WriteEndObject();
}
}
这是我的演示
public class DemoSettings : SettingBase
{
[JsonIgnore]
public bool boo { get; set; }
public int integ { get; set; }
public string str { get; set; }
public override string FileName { get; set; } = "test.json";
}
我希望FileName和boo不会保存在文件中,但它们都保存了
{
"boo": true,
"integ": 25,
"str": "sss",
"FileName": "test.json"
}
您 Write 方法提取所有属性而不对 JsonIngoreAttribute.
进行任何处理
因为 GetType().GetProperties() 对此属性一无所知,它 returns 您拥有的所有属性。
如果您想忽略具有此属性的属性,您需要像这样修改您的代码。
if (!property.CanRead || property.GetCustomAttribute<JsonIgnoreAttribute>() != null)
continue;
我不想将某些属性保存在 json 文件中,所以我使用了 [JsonIgnore] 属性但不起作用,所有属性都已保存
public abstract class SettingBase
{
[JsonIgnore]
public abstract string FileName { get; set; }
public void Save()
{
JsonFile.Save(FileName, this);
}
}
public static class JsonFile
{
public static void Save<T>(string fileName, T @object)
{
using (StreamWriter writer = File.CreateText(fileName))
{
options = new JsonSerializerOptions();
options.Converters.Add(new PolymorphicJsonConverter<T>());
string json = JsonSerializer.Serialize(@object, options);
writer.Write(json);
}
}
}
我也是用下面的转换器来解决多态问题
public class PolymorphicJsonConverter<T> : JsonConverter<T>
{
public override bool CanConvert(Type typeToConvert)
{
return typeof(T).IsAssignableFrom(typeToConvert);
}
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
{
if (value is null)
{
writer.WriteNullValue();
return;
}
writer.WriteStartObject();
foreach (var property in value.GetType().GetProperties())
{
if (!property.CanRead)
continue;
var propertyValue = property.GetValue(value);
writer.WritePropertyName(property.Name);
JsonSerializer.Serialize(writer, propertyValue, options);
}
writer.WriteEndObject();
}
}
这是我的演示
public class DemoSettings : SettingBase
{
[JsonIgnore]
public bool boo { get; set; }
public int integ { get; set; }
public string str { get; set; }
public override string FileName { get; set; } = "test.json";
}
我希望FileName和boo不会保存在文件中,但它们都保存了
{
"boo": true,
"integ": 25,
"str": "sss",
"FileName": "test.json"
}
您 Write 方法提取所有属性而不对 JsonIngoreAttribute.
进行任何处理因为 GetType().GetProperties() 对此属性一无所知,它 returns 您拥有的所有属性。
如果您想忽略具有此属性的属性,您需要像这样修改您的代码。
if (!property.CanRead || property.GetCustomAttribute<JsonIgnoreAttribute>() != null)
continue;