{
"sub_AssignedToCompanyId@odata.bind": "/accounts(f52f9dd7-35e5-e711-813b-480fcff40721)"
}
现在,我的C#类具有一个名为AssignedToCompany的属性,如下所示:
[JsonProperty(PropertyName = "sub_AssignedToCompanyId@odata.bind")]
public int AssignedToCompany{ get; set; }
我想要的是一种将其序列化为sub_AssignedToCompanyId@odata.bind的方法(我已经使用JsonProperty(“ sub_AssignedToCompanyId@odata.bind”完成了)。但是随后的定义并不好。我想要对此属性大胆地定义为“ assignedToCompanyId”。总之有这样做吗?我正在使用.Net Framework Web API。
第二季 而且,有没有一种方法可以翻译此输入: f52f9dd7-35e5-e711-813b-480fcff40721
到此输出: / accounts(f52f9dd7-35e5-e711-813b-480fcff40721)
在序列化过程中自动进行吗?
答案 0 :(得分:1)
您可以使用IOperationFilter
,ISchemaFilter
或IDocumentFilter
。哪种最适合您的需求。
ISchemaFilter
的示例可能是:
public class ODataTypeSchemaFilter : ISchemaFilter
{
private const string propToRename = "sub_AssignedToCompanyId@odata.bind";
public void Apply(Schema schema, SchemaRegistry schemaRegistry, Type, type)
{
if (type == typeof(MyObject))
{
// adding new schema property, and removing the old one
schema.properties.Add(nameof(MyObject.AssignedToCompany), schema.properties[propToRename]);
schema.properties.Remove(propToRename);
}
}
}
public class MyObject
{
[JsonProperty(PropertyName = "sub_AssignedToCompanyId@odata.bind")]
public int AssignedToCompany { get; set; }
}
这是一项强大的功能,如果操作不当,更改架构可能会导致其他问题。有关更多信息,请参见Swagger文档。在我的经验中,我已经使用了这三种方法,只是找到了最适合您的需求。
答案 1 :(得分:0)
您也可以尝试这个。先前的答案对我不起作用,但是对我有用。 但是,我建议在模型的属性名称中使用所需的名称,因为那样的话,也许您还必须解决在请求发生时将json反序列化为对象的问题。
public class CustomNameSchema : ISchemaFilter
{
public void Apply(OpenApiSchema schema, SchemaFilterContext context)
{
if (schema?.Properties == null)
{
return;
}
if (schema.Properties.Any())
{
foreach (var propType in context.Type.GetProperties().Where(x => x.CustomAttributes != null && x.CustomAttributes.Any()))
{
var schemaProp = schema.Properties.FirstOrDefault(x => x.Key.Equals(propType.Name, StringComparison.InvariantCultureIgnoreCase));
string newName = propType.GetCustomAttribute<DataMemberAttribute>()?.Name;
if (string.IsNullOrEmpty(newName))
continue;
if (schemaProp.Value.Enum != null && schemaProp.Value.Enum.Any())
{
for (int i = 0; i < schemaProp.Value.Enum.Count; i++)
{
OpenApiString curr = schemaProp.Value.Enum[i] as OpenApiString;
var memberInfo = propType.PropertyType.GetMember(curr.Value).FirstOrDefault();
string newValue = memberInfo.GetCustomAttribute<EnumMemberAttribute>()?.Value;
if (string.IsNullOrWhiteSpace(newValue))
continue;
OpenApiString newStr = new OpenApiString(newValue);
schemaProp.Value.Enum.Remove(curr);
schemaProp.Value.Enum.Insert(0, newStr);
}
}
var newSchemaProp = new KeyValuePair<string, OpenApiSchema>(newName, schemaProp.Value);
schema.Properties.Remove(schemaProp);
schema.Properties.Add(newSchemaProp);
}
}
var objAttribute = context.Type.GetCustomAttribute<DataContractAttribute>();
if (objAttribute != default && objAttribute?.Name?.Length > 0)
{
schema.Title = objAttribute.Name;
}
}
}
答案 2 :(得分:0)
对我来说,只需添加[System.Text.Json.Serialization.JsonPropertyName(“ property”)]即可解决问题
using System.Text.Json.Serialization;
[Serializable]
public class Foo
{
[JsonPropertyName("bar")]
public long Bar{ get; set; }
}