最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - JsonConvert.DeserializeObject<T> always returns T, how to deserialize json as T only if it is T? - Stack Over

programmeradmin1浏览0评论

I was suprised, that any valid json, even empty "{ }" can be deserialized by JsonConvert.DeserializeObject as any T class. The missing values will simply be default.

To example, I can't do following

record Test(string must, string? optional); // "must" must be present in json

var input = "{ }";
if (JsonConvert.DeserializeObject<Test>(input) is Test test) { ... } // must = null, great, thanks

because deserialization is always successful and worse - must = null (nullable context is completely ignored). Here is a fiddle.

I was expecting to receive null instead of T, despite there is nothing written in docs. My bad, I got it.

How should I deserialize json as T only when it is T ? I can't change json to e.g. contain type info.

I was suprised, that any valid json, even empty "{ }" can be deserialized by JsonConvert.DeserializeObject as any T class. The missing values will simply be default.

To example, I can't do following

record Test(string must, string? optional); // "must" must be present in json

var input = "{ }";
if (JsonConvert.DeserializeObject<Test>(input) is Test test) { ... } // must = null, great, thanks

because deserialization is always successful and worse - must = null (nullable context is completely ignored). Here is a fiddle.

I was expecting to receive null instead of T, despite there is nothing written in docs. My bad, I got it.

How should I deserialize json as T only when it is T ? I can't change json to e.g. contain type info.

Share Improve this question asked Feb 3 at 11:23 SinatrSinatr 22k17 gold badges106 silver badges340 bronze badges 3
  • I'm curious why you would think this would work any differently? – DavidG Commented Feb 3 at 11:49
  • 1 "I was expecting to receive null instead of T" - why? I'd only expect the value to be null if input were the string null. A reference to an "empty" object (nothing set to a non-default value) is very, very different to a null reference. (Throwing an exception might be reasonable, but returning a null reference definitely wouldn't be IMO.) – Jon Skeet Commented Feb 3 at 11:52
  • Naive way of thinking: - (me) hey, library, parse for me this string as T please. - (library) Sure. - (me) But there is nothing of T in this json? - (library) not my problem, you got you T, be happy. – Sinatr Commented Feb 3 at 12:07
Add a comment  | 

1 Answer 1

Reset to default 1

deserialize json as T only when it is T

Disclaimer: for me json is T when all required by T properties are present in json.


Use Required property to mark required properties:

record Test([property: JsonProperty(Required = Required.Always)]string must, string? optional);

JsonSerializationException is thrown if such property is missing, which can be handled with try/catch.

Here is a fiddle with the fix.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论