gpt4 book ai didi

c# - ServiceStack 和 JSV : When I serialize a Dictionary 'object' 的类型在反序列化时丢失

转载 作者:行者123 更新时间:2023-11-30 17:50:52 28 4
gpt4 key购买 nike

在我坚持使用 OrmLite 的 POCO 对象中,我具有以下属性:

....
public Dictionary<string, object> CustomData { get; set; }
...

这个属性充满了数据,比如:

customData.Add("_GooglePassword", "blabla"); // this is a String
customData.Add("_ServerPort", 8093); // this is an Int32
// etc...

它以JSV的形式保存在数据库中,像这样:

{_GooglePassword:blabla,_ServerPort:8093}

当我反序列化它,返回到 C# 中的字典时,问题就来了,然后它将所有内容都作为字符串放回,正如 VS 中的这个异常所示:

enter image description here

因此,我没有将它返回到类中定义的 Int32,而是得到了字符串,这会带来问题。我很确定使用普通 JSON 和 DataContract 方法不会有这个问题,因为我在其他部分使用 JSON。

我是不是漏掉了什么?

谢谢 =)

最佳答案

为什么 DTO 中的继承不是一个好主意的一些有趣观点 from ss authorhere .

如果您的设置可以在配置文件中拆分,我建议您在接口(interface)/类中拆分每个属性集群,并将它们存储在单独的强类型属性中。

根据您的上下文,如果您只是存储信息而不是处理这些设置或将业务规则应用于这些设置,您可能会在属性中收到动态 json 并将其存储为字符串。这样您就不会仅仅为了存储目的而创 build 置类。

关于c# - ServiceStack 和 JSV : When I serialize a Dictionary<string, 对象 > 'object' 的类型在反序列化时丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20314481/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com