我想从C#POCO类中创建一个自定义Json对象,Json对象的结构将从应用于这些类的自定义属性派生,下面是我要实现的示例。
下面是几个示例类
public class SomeBaseClass
{
public int BaseId { get; set; }
public string SomeBaseProperty { get; set; }
}
public class NestedClass
{
public string SomeNestedProp1 { get; set; }
public string SomeNestedProp2 { get; set; }
}
[ModelType(TargetModule.COMPLAINT, ModelAffinity.PARENT)]
public class ChildOne : SomeBaseClass
{
public ChildOne()
{
NestedClasses = new List<NestedClass>();
nestedClassesAgain = new List<NestedClass>();
}
public string SomeProperty { get; set; }
public string SomeProperty1 { get; set; }
public string SomeProperty2 { get; set; }
[ModelType(TargetModule.COMPLAINT, ModelAffinity.NESTED)]
public IList<NestedClass> NestedClasses { get; set; }
public IList<NestedClass> nestedClassesAgain { get; set; }
}
下面是上面使用的示例自定义属性。
[Serializable]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property)]
public sealed class ModelType : Attribute
{
public ModelType(TargetModule targetModule, ModelAffinity modelAffinity)
{
TargetModuleName = targetModule;
ModelAffinityType = modelAffinity;
}
public TargetModule TargetModuleName { get; }
public ModelAffinity ModelAffinityType { get; }
}
public enum TargetModule
{
COMPLAINT,
PAYMENT,
RECEIPTS
}
public enum ModelAffinity
{
PARENT,
NESTED,
}
下面是我计划使用自定义属性创建的Json对象。
{
"complaint": {
"someproperty": "sample value",
"someproperty1": "sample value1",
"someproperty2": "sample value2",
"baseid": "123",
"somebaseproperty": "sample value3"
"nested": [{
"somenestedprop1": "sample nested value1",
"somenestedprop2": "sample nested value2"
}
],
"nestedclassesagain": [{
"somenestedprop1": "sample nested again value1",
"somenestedprop2": "sample nested again value2"
}]
}
}
在上面的输出中,将包含自定义属性的属性/类转换为它们的值,即,将属性“ NestedClasses”转换为属性值“ nested”,如果其他属性“ NestedClassesAgain”包含相同的属性,则值/属性将合并到“嵌套的” Json Array对象中。
类似以下内容
"nested": [{
"somenestedprop1": "sample nested value1",
"somenestedprop2": "sample nested value2"
},
{
"somenestedprop1": "sample nested again value1",
"somenestedprop2": "sample nested again value2"
}
]
尝试使用以下自定义ContractResolver实现此目的
public class AttributeContractResolver : DefaultContractResolver
{
private readonly Dictionary<string, string> configDerivedList;
public AttributeContractResolver()
{
configDerivedList = new Dictionary<string, string>
{
{ "ChildOne", "BaseId,SomeProperty,SomeProperty1,NestedClasses,nestedClassesAgain" },
{ "ChildTwo", "BaseId,SomeBaseProperty,SomeOtherProperty1,SomeOtherProperty2" },
{ "NestedClass", "SomeNestedProp1, SomeNestedProp2"},
{ "COMPLAINT", "BaseId,SomeProperty,SomeProperty1,SomeNestedProp1" },
};
}
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
IList<JsonProperty> properties = base.CreateProperties(type, memberSerialization);
IEnumerable<(ModelType[] attributes, string propWithAttribute)> tt = (from x in base.GetSerializableMembers(type)
where x.GetCustomAttributes(typeof(ModelType), false).Length > 0
let attributes = (ModelType[])x.GetCustomAttributes(typeof(ModelType), false)
let propWithAttribute = x.Name
select (attributes, propWithAttribute));
var moduleType = (ModelType[])type.GetCustomAttributes(typeof(ModelType), false);
List<string> requiredProperties = (from key in configDerivedList
where moduleType.All(mod => mod
.TargetModuleName
.ToString() == key.Key) &&
tt.All(a => a
.attributes
.All(mod => mod
.TargetModuleName
.ToString() == key.Key))
select key).FirstOrDefault().Value.Split(',').ToList();
requiredProperties.AddRange(from propss in properties
from t in tt
where propss.PropertyName == t.propWithAttribute
select propss.PropertyName);
properties = properties.Where(prop => requiredProperties.Contains(prop.PropertyName, new StringComparer())).ToList();
return properties;
}
}
在上面的示例转换器中,我还要基于属性值对属性进行序列化,即基于类上设置的属性的选择性属性。
希望我可以详细说明用例。
谢谢。