小编Chr*_*tta的帖子

来自 ASP.NET Core API 的 JSON 响应中缺少派生类型的属性

来自我的 ASP.NET Core 3.1 API 控制器的 JSON 响应缺少属性。当属性使用派生类型时会发生这种情况;派生类型中定义但未在基/接口中定义的任何属性都不会序列化为 JSON。响应中似乎缺乏对多态性的支持,好像序列化基于属性的定义类型而不是其运行时类型。如何更改此行为以确保所有公共属性都包含在 JSON 响应中?

例子:

我的 .NET Core Web API 控制器返回具有接口类型属性的对象。

    // controller returns this object
    public class Result
    {
        public IResultProperty ResultProperty { get; set; }   // property uses an interface type
    }

    public interface IResultProperty
    { }
Run Code Online (Sandbox Code Playgroud)

这是一个派生类型,它定义了一个名为 的新公共属性Value

    public class StringResultProperty : IResultProperty
    {
        public string Value { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

如果我像这样从控制器返回派生类型:

    return new MainResult {
        ResultProperty = new StringResultProperty { Value = "Hi there!" }
    }; …
Run Code Online (Sandbox Code Playgroud)

.net-core asp.net-core .net-core-3.1

23
推荐指数
3
解决办法
7005
查看次数

postgres 的自定义用户定义类型不适用于可变长度文本

概述

我正在尝试使用 C 为 postgres 创建一个简单的自定义用户定义类型;但是,每当我使用自定义类型查询表时,数据似乎都会被截断(或者存在对齐问题)。我相信我在处理输入的可变性质时做错了。

这是我的代码:

PG_FUNCTION_INFO_V1(hierarchy_in);
PG_FUNCTION_INFO_V1(hierarchy_out);

typedef struct Hierarchy
{
    int32 length;
    char path[FLEXIBLE_ARRAY_MEMBER];
} Hierarchy;


Datum
hierarchy_in(PG_FUNCTION_ARGS)
{
    char *input_str = PG_GETARG_CSTRING(0);
    int32 input_len = strlen(input_str);
    Hierarchy *result;

    result = (Hierarchy *)palloc(VARHDRSZ + input_len);
    SET_VARSIZE(result, VARHDRSZ + input_len);
    strncpy(result->path, input_str, input_len);

    PG_RETURN_POINTER(result);
}

Datum
hierarchy_out(PG_FUNCTION_ARGS)
{
    Hierarchy *input = (Hierarchy *)PG_GETARG_POINTER(0);
    char *result;
    int32 input_len = VARSIZE(input) - VARHDRSZ;

    result = pnstrdup(input->path, input_len);

    PG_RETURN_CSTRING(result);
}
Run Code Online (Sandbox Code Playgroud)

这是我的测试用例:

DROP TABLE TESTING;
DROP EXTENSION hierarchy CASCADE;

CREATE EXTENSION hierarchy; …
Run Code Online (Sandbox Code Playgroud)

c postgresql types user-defined-types

5
推荐指数
1
解决办法
254
查看次数