Bra*_*lon 5 c# serialization entity-framework json.net
我们有一种情况是我们使用JSON来序列化EF模型以用于数据同步.为了使同步正常工作,我们需要模型的表名.这并不难,我们的代码已经提供给我们了.主要问题是在JSON中传输该数据.
例如,假设我们有以下模型.
public class Foo
{
// ...
public virtual ICollection<Bar> Bars { get; set; }
}
public class Bar
{
// ...
public virtual ICollection<FooBar> FooBars { get; set; }
}
public class FooBar
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
我们通过includes下拉所有嵌套项,然后序列化它.问题是,我们需要将实体的表名作为元数据插入到JSON中,而不将其添加到模型本身.
例如,在上面的场景中,JSON看起来像
{
"__tableName": "Foos",
// ...
"Bars": [
{
"__tableName": "Bars"
// ...
"FooBars": [
{
"__tableName": "FooBars"
// ...
}
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
我认为JSON.Net中的自定义序列化程序是最好的方法,但要么我没有插入正确的位置,要么它们不像我想的那样工作.
我尝试制作自定义,JsonConverter因为这似乎是处理自定义序列化方案的默认方式.但是,它似乎只是在要对序列化的基础对象上调用,而不是任何子对象.
我是否需要插入JSON.Net以实际放入此元数据?我在这个主题上找到的几乎所有内容都指向JsonConverter,但我不确定在这种情况下实际上我需要做什么.
我有一个想法是将对象加载到一个JObject中JsonConverter,然后自己走模型树并根据需要插入键,但我希望能比这更优雅的东西.
谢谢.
尽管 aJsonConverter在这里似乎是合适的选择,但实际上它对于此类问题的实际效果并不好。问题是您希望以编程方式将转换器应用于广泛的类,并且这些类可以包含使用相同转换器的其他类。因此,您可能会遇到转换器中的递归循环问题,您需要解决这些问题。这是可以做到的,但可能会变得有点混乱。
幸运的是,对于这种情况有更好的替代方案。您可以将自定义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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
要将其插入序列化管道,请创建自定义解析器的实例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; }
}
Run Code Online (Sandbox Code Playgroud)
输出:
{
"__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
}
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
小提琴: https: //dotnetfiddle.net/zG5Zmm