ServiceStack Ormlite - Postgres 使用 MaxDate 将日期 属性 序列化为 JsonB

ServiceStack Ormlite - Postgres serializing Date property with MaxDate to JsonB

我有一个复杂的对象,我使用 Ormlite 将其保存到 postgres 中的 JsonB 字段。 属性 之一是 DateTime 并设置为 DateTime.Max.

正在从 Postgres 检索对象,DateTime 属性 值设置为 DateTime.Min 值

01/01/0001 00:00:00

不确定这是 Ormlite 还是 json 序列化程序的错误。

要复制的代码片段

class Program
{
    static void Main(string[] args)
    {
        var item = new LicenseCheckTemp();
        item.Body = new CheckHistory();
        item.Body.List.Add(new ItemHistory() {AddedOn = DateTime.MaxValue, Note = "Test"});

        var factory = GetFactory(ConfigurationManager.AppSettings["PostgresConnectionString"]);
        using (var db = factory.OpenDbConnection())
        {
            db.CreateTableIfNotExists<LicenseCheckTemp>();
            db.Save(item);
        }

        using (var db = factory.OpenDbConnection())
        {
          var items =  db.Select<LicenseCheckTemp>();

            foreach (var licenseCheck in items.OrderBy(x=>x.Id))
            {
                if (licenseCheck.Body != null && licenseCheck.Body.List.Any())
                {
                    foreach (var itemHistory in licenseCheck.Body.List)
                    {
                        Console.WriteLine($"{itemHistory.AddedOn} :  Note {itemHistory.Note}");
                    }

                }

            }
        }

        Console.ReadKey();


    }


    public static IDbConnectionFactory GetFactory(string connection)
    {
        var factory = new OrmLiteConnectionFactory(connection,
           PostgreSqlDialect.Provider);

        factory.DialectProvider.NamingStrategy = new OrmLiteNamingStrategyBase();

        return factory;
    }
}

public class LicenseCheckTemp
{
    [AutoIncrement]
    public int Id { get; set; }

    [CustomField("json")]
    public CheckHistory Body { get; set; }
}

public class CheckHistory
{
    public List<ItemHistory> List { get; set; } = new List<ItemHistory>();
}

public class ItemHistory
{
    public string Note { get; set; }

    public DateTime AddedOn { get; set; }

}

虽然 OrmLite 没有明确支持 PostgreSQL JSON 数据类型,OrmLite 现有的 JSON ComplexType 属性的序列化应该允许它自然地工作,如下所示:

我添加了一个示例 this test in this commit:

OrmLiteConfig.DialectProvider.NamingStrategy = new OrmLiteNamingStrategyBase();

var item = new LicenseCheckTemp();
item.Body = new CheckHistory();
item.Body.List.Add(new ItemHistory { AddedOn = DateTime.MaxValue, Note = "Test" });

using (var db = OpenDbConnection())
{
    db.DropAndCreateTable<LicenseCheckTemp>();
    db.GetLastSql().Print();
    db.Save(item);
}

using (var db = OpenDbConnection())
{
    var items = db.Select<LicenseCheckTemp>();
    items.PrintDump();

    foreach (var licenseCheck in items.OrderBy(x => x.Id))
    {
        if (licenseCheck.Body != null && licenseCheck.Body.List.Any())
        {
            foreach (var itemHistory in licenseCheck.Body.List)
            {
                $"{itemHistory.AddedOn} :  Note {itemHistory.Note}".Print();
            }
        }
    }
}

按预期工作,即打印出来:

CREATE TABLE "LicenseCheckTemp" 
(
  "Id" INTEGER PRIMARY KEY AUTOINCREMENT, 
  "Body" json NULL 
); 

[
    {
        Id: 1,
        Body: 
        {
            List: 
            [
                {
                    Note: Test,
                    AddedOn: 9999-12-31T23:59:59.9999999-05:00
                }
            ]
        }
    }
]
12/31/9999 11:59:59 PM :  Note Test

正在显示 CreateTable 为 Body 属性 创建 "json" 类型,其中行被序列化并返回完全填充。

无法重现为什么它对您不起作用,您使用的是最新的 v4.0.54 版本的 OrmLite 吗?它适用于较小的 DateTime 吗? (也许您的 TimeZone 中的 Max DateTime 超出了您的 PgSql 配置实例支持的范围)。