gpt4 book ai didi

c# - 属性 : get property name 的自定义 JsonConverter

转载 作者:太空宇宙 更新时间:2023-11-03 10:30:34 25 4
gpt4 key购买 nike

我有这个用于 guid 属性的示例转换器:

public class CustomGuidConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof (Guid?) || objectType == typeof (Guid);
}

public override void WriteJson(JsonWriter writer, object oldValue, JsonSerializer serializer)
{
if (value != null)
{
var newValue = convert(oldValue); // do some conversion
writer.WriteValue(newValue);
}
}
}

像这样使用它:

public class Outer {
public int Id { get; set; }

[JsonConverter(typeof(InterfaceLabelConverter))]
public Guid? ProductFamilyId { get; set; }
}

如何在 WriteJson 方法中访问当前属性的名称?我想用这样的另一个属性名将旧值写入 writer:

{ Id: 1234, ProductFamilyId: 'newValue', ProductFamilyIdOld: 'oldValue' }

最佳答案

我建议简单地使转换后的 GUID 成为包含类的私有(private)只读属性。如果你用 [JsonProperty] 标记它它将被序列化:

public class Outer
{
public int Id { get; set; }

public Guid? ProductFamilyId { get; set; }

[JsonProperty(NullValueHandling=NullValueHandling.Ignore)]
Guid? OldProductFamilyId
{
get
{
return Convert(ProductFamilyId);
}
}

private Guid? Convert(Guid? guid)
{
if (guid != null)
{
var bytes = guid.Value.ToByteArray();
bytes[0] = unchecked((byte)~bytes[0]); // For example
guid = new Guid(bytes);
}
return guid;
}
}

也就是说,您可以从 JsonWriter.Path 中选择当前属性名称属性:

public class InterfaceLabelConverter : JsonConverter
{
private Guid? Convert(Guid? guid)
{
if (guid != null)
{
var bytes = guid.Value.ToByteArray();
bytes[0] = unchecked((byte)~bytes[0]); // For example
guid = new Guid(bytes);
}
return guid;
}

public override bool CanConvert(Type objectType)
{
throw new InvalidOperationException(); // This converter should only be applied directly to a property.
}

public override bool CanRead { get { return false; } }

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var path = writer.Path;
var propertyName = path.Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries).Last(); // Throw an exception if not found.
if (propertyName.StartsWith("[") && propertyName.EndsWith("]"))
throw new InvalidOperationException(); // Trying to use this converter for an array element.
var guid = (Guid?)value;
writer.WriteValue(guid);

if (guid != null)
{
// Note -- actually the converter isn't called for null values, see
// https://stackoverflow.com/questions/8833961/serializing-null-in-json-net
var nextGuid = Convert(guid);
var nextName = "Old" + propertyName;

writer.WritePropertyName(nextName);
writer.WriteValue(nextGuid);
}
}
}

像这样使用它:

public class Outer
{
public int Id { get; set; }

[JsonConverter(typeof(InterfaceLabelConverter))]
public Guid? ProductFamilyId { get; set; }
}

关于c# - 属性 : get property name 的自定义 JsonConverter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30376611/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com