gpt4 book ai didi

c# - 自定义 Json Serializer 通过忽略类属性来序列化和反序列化所有属性

转载 作者:太空宇宙 更新时间:2023-11-03 19:49:55 24 4
gpt4 key购买 nike

我想序列化我类的所有属性,但想在返回响应时隐藏一些属性。

我正在使用 NewtonSoft.Json.Net 进行序列化。

例如,在下面的类中,我想序列化这两个属性,但我只想返回 PlaceName。

有什么办法吗?

[DataContract]
public class Place
{
[DataMember(EmitDefaultValue = false)]
public int PlaceId { get; set; }

[DataMember(EmitDefaultValue = false, Order = 1)]
public string PlaceName { get; set; }
}

编辑 1:

下面是我当前的 Json 文件。

[
{
"placeId": 1,
"placeName": "Malacca"
},
{
"placeId": 2,
"placeName": "Kuala Lumpur"
},
{
"placeId": 3,
"placeName": "Genting Highlands"
},
{
"placeId": 4,
"placeName": "Singapore"
},
{
"placeId": 5,
"placeName": "Penang"
},
{
"placeId": 6,
"placeName": "Perak"
},
{
"placeId": 8,
"placeName": "Selangor"
}
]

编辑 2:找到解决方案

我通过一些研究找到了解决方案。

我创建了一个自定义契约解析器来序列化和反序列化所有属性并传递给它。

下面是我的代码

public  class AllPropertiesResolver : DefaultContractResolver
{
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
JsonProperty property = base.CreateProperty(member, memberSerialization);
property.Ignored = false;
return property;
}
}

下面是我调用它的代码。

 JsonConvert.SerializeObject(object, new JsonSerializerSettings() { ContractResolver = new AllPropertiesResolver() });
JsonConvert.DeserializeObject<T>(stream, new JsonSerializerSettings() { ContractResolver = new AllPropertiesResolver() });

谢谢大家的回复。

最佳答案

您可以使用[JsonIgnore]。由于您使用 asp.net-web-api 标记了您的问题,我认为您确实在使用它。因此,下面是一个示例,其中 Controller 将返回整个模型,但带有 JsonIgnore 的属性除外。 .通过使用自定义 ContractResolver我们序列化它以包含所有属性(即使它们得到了 JsonIgnore)。并在返回我们的响应时使用默认的 ContractResolver

但请注意,它会覆盖默认行为。所以你可能想要添加一些其他检查,而不仅仅是设置 Ignored = false;

public class PlaceController : ApiController
{
[HttpGet]
public IHttpActionResult Get()
{
var json = "[{\"placeId\": 1,\"placeName\": \"Malacca\"},{\"placeId\": 2,\"placeName\": \"Kuala Lumpur\"},{\"placeId\": 3,\"placeName\": \"Genting Highlands\"},{\"placeId\": 4,\"placeName\": \"Singapore\"},{\"placeId\": 5,\"placeName\": \"Penang\"},{\"placeId\": 6,\"placeName\": \"Perak\"},{\"placeId\": 8,\"placeName\": \"Selangor\"}]";

var settings = new JsonSerializerSettings();
settings.ContractResolver = new IncludeAllPropertiesContractResolver();

var places = JsonConvert.DeserializeObject<Place[]>(json, settings);
return Ok(places);
}
}


public class IncludeAllPropertiesContractResolver : DefaultContractResolver
{
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
IList<JsonProperty> properties = base.CreateProperties(type, memberSerialization);

// Or other way to determine...
foreach (var jsonProperty in properties)
{
// Include all properties.
jsonProperty.Ignored = false;
}
return properties;
}
}

[DataContract]
public class Place
{
[JsonIgnore]
[DataMember(EmitDefaultValue = false)]
public int PlaceId { get; set; }

[DataMember(EmitDefaultValue = false, Order = 1)]
public string PlaceName { get; set; }
}

输出:

[
{
"placeName": "Malacca"
},
{
"placeName": "Kuala Lumpur"
},
{
"placeName": "Genting Highlands"
},
{
"placeName": "Singapore"
},
{
"placeName": "Penang"
},
{
"placeName": "Perak"
},
{
"placeName": "Selangor"
}
]

或者,如果您不介意反射(reflection)一下。下面我们使用 JsonInclude 属性,它将覆盖 JsonIgnore 的默认行为。

public class JsonIncludeContractResolver : DefaultContractResolver
{
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
IList<JsonProperty> properties = base.CreateProperties(type, memberSerialization);

var actualProperties = type.GetProperties();

foreach (var jsonProperty in properties)
{
// Check if it got our JsonInclude attribute.
var property = actualProperties.FirstOrDefault(x => x.Name == jsonProperty.PropertyName);
if (property != null && property.GetCustomAttribute(typeof(JsonInclude)) != null)
{
jsonProperty.Ignored = false;
}
}
return properties;
}
}

[DataContract]
public class Place
{
[JsonInclude] // Will override JsonIgnore.
[JsonIgnore]
[DataMember(EmitDefaultValue = false)]
public int PlaceId { get; set; }

[DataMember(EmitDefaultValue = false, Order = 1)]
public string PlaceName { get; set; }
}

public class JsonInclude : Attribute
{

}

关于c# - 自定义 Json Serializer 通过忽略类属性来序列化和反序列化所有属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40742813/

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