OkObjectResult为什么忽略了JsonIgnore?

huangapple go评论59阅读模式
英文:

Why is OkObjectResult ignoring JsonIgnore?

问题

I'm returning an object from my Azure function using OkObjectResult, but the object's JSON still contains properties I've marked JsonIgnore. Why is that, and how can I fix it?

Details:

I have this class:

using System.Text.Json.Serialization;
// ...
public class Example
{
    public string A { get; set; }
    public string B { get; set; }
    [JsonIgnore]
    public string C { get; set; }
}

When returning from an Azure function, if I do:

var result = new Example() { A = "Ayy", B = "Bee", C = "See" };
return new OkObjectResult(result);

...the JSON returned is (I've added formatting):

{
    a: "Ayy",
    b: "Bee",
    c: "See"
}

Notice that the JsonIgnore attribute didn't take effect, the JSON has a c property.

But if I serialize explicitly:

var result = new Example() { A = "Ayy", B = "Bee", C = "See" };
return new OkObjectResult(
    JsonSerializer.Serialize(
        result,
        new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase}
    )
);

...c is left out as desired:

{
    a: "Ayy",
    b: "Bee"
}

Why isn't the property being left out when I give the object to OkObjectResult directly, and how do I fix it?

英文:

I'm returning an object from my Azure function using OkObjectResult, but the object's JSON still contains properties I've marked JsonIgnore. Why is that, and how can I fix it?

Details:

I have this class:

using System.Text.Json.Serialization;
// ...
public class Example
{
    public string A { get; set; }
    public string B { get; set; }
    [JsonIgnore]
    public string C { get; set; }
}

When returning from an Azure function, if I do:

var result = new Example() { A = "Ayy", B = "Bee", C = "See" };
return new OkObjectResult(result);

...the JSON returned is (I've added formatting):

{
    a: "Ayy",
    b: "Bee",
    c: "See"
}

Notice that the JsonIgnore attribute didn't take effect, the JSON has a c property.

But if I serialize explicitly:

var result = new Example() { A = "Ayy", B = "Bee", C = "See" };
return new OkObjectResult(
    JsonSerializer.Serialize(
        result,
        new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase}
    )
);

...c is left out as desired:

{
    a: "Ayy",
    b: "Bee"
}

Why isn't the property being left out when I give the object to OkObjectResult directly, and how do I fix it?

答案1

得分: 5

问题在于类代码使用了错误的 JsonIgnore 属性,原因是:

using System.Text.Json.Serialization;

截止到本写作时,Azure函数(仍然)默认使用 Newtonsoft.Json,所以该属性被标记为错误的属性,而序列化程序不知道要排除它。

为了修复这个问题,应该导入 Newtonsoft.Json,如下所示:

using Newtonsoft.Json; // <====================
// ...
public class Example
{
    public string A { get; set; }
    public string B { get; set; }
    [JsonIgnore]
    public string C { get set; }
}
  1. 如果你使用 Visual Studio 的 "Add... > Azure Function" 功能,它创建的模板会包括 Newtonsoft.Json 的导入,这也是我意识到这个问题的唯一原因。另请参考 这个相关问题(感谢 Eldar 提供的信息)。
英文:

The problem is the class code uses the wrong JsonIgnore attribute because of this:

> csharp
&gt; using System.Text.Json.Serialization;
&gt;

As of this writing, Azure functions (still) use Newtonsoft.Json by default,<sup>1</sup> so the property is marked with the wrong attribute, and the serializer doesn't know to leave it out.

To fix it, import Newtonsoft.Json instead:

using Newtonsoft.Json; // &lt;====================
// ...
public class Example
{
    public string A { get; set; }
    public string B { get; set; }
    [JsonIgnore]
    public string C { get; set; }
}

<sup>1</sup> If you use Visual Studio's Add... > Azure Function, the template it creates includes the import of Newtonsoft.Json, which is the only reason I realized this. See also this related issue (thanks for that, Eldar!).

huangapple
  • 本文由 发表于 2023年3月7日 20:41:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75662107.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定