【发布时间】:2015-10-24 08:13:40
【问题描述】:
今天我正在使用 ServiceStack Ormlite 在 postgres 中使用 jsonb 数据类型。一个基本模型有一个复杂的类型属性,它本身包含一个接口对象的字典(下面的伪代码)。
通常,ServiceStack 通过向 json 添加一个“__type”标识符来处理这个问题。它正确地做到了这一点。但是,对于其中一个字典项,“__type”标识符列在属性下方的第二个位置,这导致从 postgres 检索该项时返回一个空对象。更有趣的是,据我所知,序列化程序首先列出了“__type”,但是一旦它存储在 postgres 中,它就会被重新排序。 jsonb 数据类型会重新排序 json 属性吗?
模型的伪代码(在真实代码中更广泛)
public class StrategyContainer
{
public Dictionary<int, List<IStrategy>> SetOfStrategies { get; set; }
}
public interface IStrategy
{
int Health { get; set; }
string Name { get; set; }
}
public interface IAttack : IStrategy
{
int Strength { get; set; }
}
public abstract class AbstractStrategy : IStrategy
{
public int Health { get; set; }
public string Name { get; set; }
}
public class Attack : AbstractStrategy, IAttack
{
public int Strength { get; set; }
}
这是从 postgres jsonb 列检索时 json 的显示方式。
{
"SetOfStrategies": {
"1": [{
"__type": "Test.Attack, Test",
"Strength": 10,
"Health": 5,
"Name": "Testing"
},
{
"Strength": 20,
"__type": "Test.Attack, Test",
"Health": 10,
"Name": "Testing2"
}]
}
注意:字典列表中还有许多其他项目。其中只有一个有错误的“__type”。所有其他正确加载。
现在我已经回到标准的文本列类型,一切正常。
【问题讨论】:
标签: json postgresql servicestack ormlite-servicestack servicestack-text