虽然JsonConverter 在这里似乎是合适的选择,但在实践中对于此类问题它并不能很好地工作。问题是您希望以编程方式将转换器应用于广泛的类,并且这些类可以包含使用相同转换器的其他类。因此,您可能会遇到转换器中的递归循环问题,您需要解决这些问题。可以,但可能会有点乱。
幸运的是,对于这种情况有更好的选择。您可以将自定义IContractResolver 与IValueProvider 结合使用,将__tableName 属性插入到每个具有表名的对象的JSON 中。解析器负责检查特定对象类型是否具有关联的表名,如果有,则为该类型设置额外的属性。值提供者只是在被询问时返回表名。
这是您需要的代码:
class TableNameInsertionResolver : DefaultContractResolver
{
private Dictionary<string, string> tableNames;
public TableNameInsertionResolver(Dictionary<string, string> tableNames)
{
this.tableNames = tableNames;
}
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
IList<JsonProperty> props = base.CreateProperties(type, memberSerialization);
// If there is an associated table name for this type,
// add a virtual property that will return the name
string tableName;
if (tableNames.TryGetValue(type.FullName, out tableName))
{
props.Insert(0, new JsonProperty
{
DeclaringType = type,
PropertyType = typeof(string),
PropertyName = "__tableName",
ValueProvider = new TableNameValueProvider(tableName),
Readable = true,
Writable = false
});
}
return props;
}
class TableNameValueProvider : IValueProvider
{
private string tableName;
public TableNameValueProvider(string tableName)
{
this.tableName = tableName;
}
public object GetValue(object target)
{
return tableName;
}
public void SetValue(object target, object value)
{
throw new NotImplementedException();
}
}
}
要将其插入序列化管道,请创建JsonSerializerSettings 的实例并将ContractResolver 属性设置为自定义解析器的实例。然后将设置传递给序列化程序。就是这样;它应该“正常工作”。
这是一个演示:
class Program
{
static void Main(string[] args)
{
Foo foo = new Foo
{
Id = 1,
Bars = new List<Bar>
{
new Bar
{
Id = 10,
FooBars = new List<FooBar>
{
new FooBar { Id = 100 },
new FooBar { Id = 101 }
}
},
new Bar
{
Id = 11,
FooBars = new List<FooBar>
{
new FooBar { Id = 110 },
new FooBar { Id = 111 },
}
}
}
};
// Dictionary mapping class names to table names.
Dictionary<string, string> tableNames = new Dictionary<string, string>();
tableNames.Add(typeof(Foo).FullName, "Foos");
tableNames.Add(typeof(Bar).FullName, "Bars");
tableNames.Add(typeof(FooBar).FullName, "FooBars");
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.ContractResolver = new TableNameInsertionResolver(tableNames);
settings.Formatting = Formatting.Indented;
string json = JsonConvert.SerializeObject(foo, settings);
Console.WriteLine(json);
}
}
public class Foo
{
// ...
public int Id { get; set; }
public virtual ICollection<Bar> Bars { get; set; }
}
public class Bar
{
// ...
public int Id { get; set; }
public virtual ICollection<FooBar> FooBars { get; set; }
}
public class FooBar
{
// ...
public int Id { get; set; }
}
输出:
{
"__tableName": "Foos",
"Id": 1,
"Bars": [
{
"__tableName": "Bars",
"Id": 10,
"FooBars": [
{
"__tableName": "FooBars",
"Id": 100
},
{
"__tableName": "FooBars",
"Id": 101
}
]
},
{
"__tableName": "Bars",
"Id": 11,
"FooBars": [
{
"__tableName": "FooBars",
"Id": 110
},
{
"__tableName": "FooBars",
"Id": 111
}
]
}
]
}
小提琴:https://dotnetfiddle.net/zG5Zmm