英文:
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; }
}
- 如果你使用 Visual Studio 的 "Add... > Azure Function" 功能,它创建的模板会包括
Newtonsoft.Json的导入,这也是我意识到这个问题的唯一原因。另请参考 这个相关问题(感谢 Eldar 提供的信息)。
英文:
The problem is the class code uses the wrong JsonIgnore attribute because of this:
> csharp
> using System.Text.Json.Serialization;
>
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; // <====================
// ...
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!).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论