有选择地将 Linq2sql 模型序列化为 JSON
我有来自 mssql 数据库的非常常见的 linq2sql 业务模型。表之间有一些关联,这很好。整个模型是单独组装的。我正在使用 JSON.NET 库进行序列化。
现在我需要将这些模型序列化为 JSON,并告诉它要使用哪些属性以及现在使用哪些属性。使用 if 属性是不可能的,但我也不喜欢元数据类的想法。
所以我一直在考虑以这种方式使用扩展方法:
public static class User {
public static object GetSerializable(this DataModel.User user) {
return new {
user.Id, user.LoginName, user.FirstName, user.LastName
}
}
}
这很好,但是我不确定在这样的情况下如何使用它:
[JsonObject]
public class AuthModel {
[JsonProperty]
public DataModel.User { get; set; }
}
您知道如何在那里有效地使用这些扩展方法吗?或者其他一些完全不同的想法?
I have quite common linq2sql bussiness model from mssql database. There are some associations between tables, which is good. Whole model is in separate assembly. I am using JSON.NET library for serialization.
Now i need to serialize those models to JSON and tell it which properties to use and which now. Using if attributes is impossible, but i don't like idea of metadata class either.
So i had been thinking about using extension method in this manner:
public static class User {
public static object GetSerializable(this DataModel.User user) {
return new {
user.Id, user.LoginName, user.FirstName, user.LastName
}
}
}
This would nice, however i am not sure how to use it in cases like this:
[JsonObject]
public class AuthModel {
[JsonProperty]
public DataModel.User { get; set; }
}
Do you have any idea how to effectively use those extensions methods there ? Or some other completely different ideas ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 Fluent Json 将它们转换为 json。此配置可以在代码中完成,无需使用属性。
选项 2:您可以使用自定义序列化器
选项 3:您可以使用 KeyValuePairConverter。将您的持久类转换为字典并使用它。
You can use Fluent Json to convert them to json. This configuration can be done in code without using attributes.
Option 2 : You can use Custom Serializer
Option 3 : You can use the KeyValuePairConverter. Convert your persistant class to a dictionary and use this.
好吧,我决定采用基于自定义 JsonConverter 的方法,其结果与上面的方法非常相似。它看起来像这样:
然后我只是创建一个类:
足够简单,没有一些复杂的配置或元数据类。所有内容都经过编译器正确验证,因此发生更改时不会出现拼写错误和问题。
All right i had decided for approach based on custom JsonConverter which is in result very similar to that approach above. It looks like this:
Then i just make a class like:
Simple enough without some complex configuration or metadata classes. Everything is properly validated by compiler, so no typos and problems when changes occurs.
我编写了一个自定义 JsonConverter 来一般处理这种情况,因为源类有一个枚举需要序列化的接口。
您的类加上序列化接口:
转换器:
可以对转换器进行很多优化...
I wrote a custom JsonConverter to handle this kind of case generically given that the source class has an interface enumerating what needs to be serialized.
Your class plus serialization interface:
The converter:
A lot of optimizations can be made to the converter...