1

ServiceStack ORMLite不會從Postgresql反序列化我的類。ServiceStack ormlite json不會從數據庫反序列化

保持緩存中的對象解決了問題,但無法將它們加載回去(保存無誤)。

下面是重現問題的代碼。

void Main() 
{ 
    var inv = new Inventory(); 
    inv.Items.Add(new Item{Id=1,CreatedAt=DateTime.Now, Key="potion10", Descriptions=new Dictionary<int, string>{{1,"Poção que recupera um pouco de vida."},{2,"Potion that restores a little of health."}}, HealthModifier=10,IsUseable=true, Names=new Dictionary<int, string>{{1,"Poção Leve"},{2,"Minor Potion"}}, UpdatedAt=DateTime.Now}, 2); 

    var invJson = inv.ToJson().To<Inventory>(); // can't deserialize 
    var invJsv = inv.ToJsv().To<Inventory>(); // same problem 
} 

public class Item 
{ 
    public Item() 
    { 
     Names = new Dictionary<int, string>(); 
     Descriptions = new Dictionary<int, string>(); 
    } 

    public long Id { get; set; } 

    public string Key { get; set; } 

    public Dictionary<int, string> Names { get; set; } 

    public Dictionary<int, string> Descriptions { get; set; } 

    public int HealthModifier { get; set; } 

    public bool IsUseable { get; set; } 

    public DateTime CreatedAt { get; set; } 

    public DateTime UpdatedAt { get; set; } 
} 

public class Inventory 
{ 
    public Inventory() 
    { 
     Items = new Dictionary<Item, int>(); 
    } 

    public Dictionary<Item, int> Items { get; set; } 
} 

Postgresql上的JSON與上面的代碼相同。

{ 
    "Items":{ 
     "{"Id":1, 
     "Key":"potion10", 
     "Names":{ 
      "1":"Poção Leve", 
      "2":"Minor Potion" 
     }, 
     "Descriptions":{ 
      "1":"Poção que recupera um pouco de vida.", 
      "2":"Potion that restores a little of health." 
     }, 
     "HealthModifier":10, 
     "IsUseable":true, 
     "CreatedAt":"\/Date(1430743156138-0300)\/", 
     "UpdatedAt":"\/Date(1430743156139-0300)\/" 
    }:2 
} 
} 
+0

'inv.ToJson()'返回的json是什麼樣的? – dbc

+0

另外,Postgresql的JSON看起來像什麼? – dbc

回答

1

的問題是,你的Inventory類有一個複雜的類鍵入字典

public Dictionary<Item, int> Items { get; set; } 

然而,根據ServiceStack.Text documentation

任何IDictionary的序列化爲一個標準JSON對象,即:

 {"A":1,"B":2,"C":3,"D":4} 

很遺憾,您Item類不能被表示爲一個簡單的字符串,因此不能被用來作爲一個JSON屬性

您可以做的是將您的項目序列化爲一組鍵值對。由於ServiceStack文本串行support [DataMember] aliasessupport ignoring of data members,您可以執行以下操作:

[DataContract] 
public class Inventory 
{ 
    public Inventory() 
    { 
     Items = new Dictionary<Item, int>(); 
    } 

    [IgnoreDataMember] 
    public Dictionary<Item, int> Items { get; set; } 

    [DataMember(Name = "Items")] 
    public KeyValuePair<Item, int> [] ItemArray 
    { 
     get 
     { 
      return Items == null ? null : Items.ToArray(); 
     } 
     set 
     { 
      (Items ?? (Items = new Dictionary<Item, int>())).Clear(); 
      if (value != null) 
       foreach (var pair in value) 
        Items[pair.Key] = pair.Value; 
     } 
    } 
} 

這將序列化和反序列化有效的JSON應該是這個樣子:

{ 
    "Items": [ 
    { 
     "Key": { 
     "Id": 1, 
     "Key": "potion10", 
     "Names": { 
      "1": "Poção Leve", 
      "2": "Minor Potion" 
     }, 
     "Descriptions": { 
      "1": "Poção que recupera um pouco de vida.", 
      "2": "Potion that restores a little of health." 
     }, 
     "HealthModifier": 10, 
     "IsUseable": true, 
     "CreatedAt": "2015-05-04T02:07:10.7216263-04:00", 
     "UpdatedAt": "2015-05-04T02:07:10.7216263-04:00" 
     }, 
     "Value": 2 
    } 
    ] 
} 

然而,你提到你的JSON來自Postgresql。那個JSON是什麼樣的?您可能需要將您的序列化或類修改爲您實際收到的內容。

+1

謝謝!我可能只是使用包含鍵和值的列表,因爲它會更有意義。 – Mielke

+0

是的,它是!標記! – Mielke