简体   繁体   English

ServiceStack ormlite json不会从数据库反序列化

[英]ServiceStack ormlite json does not deserialize from database

ServiceStack ORMLite does not deserialize my class from Postgresql. ServiceStack ORMLite不会从Postgresql反序列化我的类。

Keeping the objects on cache solves, but it can't load them back (saving is ok). 将对象保留在高速缓存中可以解决问题,但是无法将它们重新加载(可以保存)。

Below is a code that reproduces the problem. 下面是重现该问题的代码。

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; }
}

The JSON on Postgresql is the same from the code above. 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
}
}

The problem is that your class Inventory has a dictionary keyed by a complex class: 问题是您的班级“ Inventory有一个由复杂班级键入的字典:

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

However, according to the ServiceStack.Text documentation 但是,根据ServiceStack.Text文档

Any IDictionary is serialized into a standard JSON object, ie: 任何IDictionary都序列化为标准JSON对象,即:

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

Unfortunately your Item class cannot be represented as a simple string thus cannot be used as a JSON property name . 不幸的是,您的Item类不能表示为简单的字符串,因此不能用作JSON属性名称

What you could do is to serialize your items as an array of key-value pairs. 您可以做的是将项序列化为键值对数组。 Since the ServiceStack Text serializers support [DataMember] aliases and also support ignoring of data members , you can do the following: 由于ServiceStack文本序列化程序支持[DataMember]别名 ,还支持忽略数据成员 ,因此您可以执行以下操作:

[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;
        }
    }
}

This will serialize and deserialize valid JSON that should look something like this: 这将对有效的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 } ] } 

However, you mentioned that your JSON was coming from Postgresql. 但是,您提到您的JSON来自Postgresql。 What does that JSON look like? JSON是什么样的? You may need to adapt your serialization or classes to what you are actually receiving. 您可能需要根据实际收到的内容调整序列化或类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM