[C#, Json.NET] Ignore, Default ํ™•์ธํ•˜๊ธฐ

using Newtonsoft.Json;
using System.ComponentModel;

struct JsonObject
{
    // Null๊ฐ’์„ ์„ธํŒ…ํ•˜์—ฌ ์ง๋ ฌํ™” ์‹œ ๋ฌด์‹œ๋˜๋Š”์ง€ ์ฒดํฌ
    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public int? IgnoreNullInt { get; set; }

    // Null๊ฐ’์„ ์„ธํŒ…ํ•˜์—ฌ ์ง๋ ฌํ™” ์‹œ ๋ฌด์‹œ๊ฐ€ ๋˜์ง€ ์•Š๋Š”์ง€ ์ฒดํฌ
    [JsonProperty(NullValueHandling = NullValueHandling.Include)]
    public int? IncludeNullInt { get; set; }

    // Null์ด ์•„๋‹Œ ๊ฐ’์„ ์„ธํŒ…ํ•˜์—ฌ ๋ฌด์‹œ๋˜์ง€ ์•Š๋Š”์ง€ ์ฒดํฌ
    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public int? NotNullInt { get; set; }

    // intํ˜•์— default๋ฅผ ํ• ๋‹นํ•˜๋ฉด 0์ด ํ• ๋‹น๋˜๋Š”๋ฐ, DefaultValue๋กœ 0์„ ์ง€์ •ํ•˜์—ฌ ๋ฌด์‹œ๋˜๋Š”์ง€ ์ฒดํฌ
    [JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
    [DefaultValue(0)]
    public int IgnoreDefaultValue { get; set; }

    // intํ˜•์— default๋ฅผ ํ• ๋‹นํ•˜๋ฉด 0์ด ํ• ๋‹น๋˜๋Š”๋ฐ, DefaultValue๋กœ 0์„ ์ง€์ •ํ•˜์—ฌ ๋ฌด์‹œ๋˜์ง€ ์•Š๋Š”์ง€ ์ฒดํฌ
    [JsonProperty(DefaultValueHandling = DefaultValueHandling.Include)]
    [DefaultValue(0)]
    public int IncludeDefaultValue { get; set; }

    // DefaultValue๋ฅผ 30์œผ๋กœ ์„ค์ •ํ•˜์—ฌ, 30์„ ํ• ๋‹นํ•œ ํ›„ ๋ฌด์‹œ๋˜๋Š”์ง€ ์ฒดํฌ
    [JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
    [DefaultValue(30)]
    public int IgnoreChangeDefaultValue { get; set; }

    // DefaultValue๋ฅผ 30์œผ๋กœ ์„ค์ •ํ•˜์—ฌ, 30์ด์™ธ์˜ ๊ฐ’์„ ํ• ๋‹นํ•œ ํ›„ ๋ฌด์‹œ๋˜๋Š”์ง€ ์ฒดํฌ
    [JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
    [DefaultValue(30)]
    public int IgnoreChangeDefaultValue2 { get; set; }

    // DefaultValue๋ฅผ 30์œผ๋กœ ์„ค์ •ํ•˜์—ฌ, intํ˜•์˜ default ๊ฐ’์„ ํ• ๋‹นํ•œ ํ›„ ๋ฌด์‹œ๋˜๋Š”์ง€ ์ฒดํฌ
    [JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
    [DefaultValue(30)]
    public int IgnoreChangeDefaultValue3 { get; set; }

    // DefaultValue๋ฅผ 30์œผ๋กœ ์„ค์ •ํ•˜์—ฌ, ์•„๋ฌด๊ฒƒ๋„ ํ• ๋‹นํ•˜์ง€ ์•Š๊ณ  DeserializeObject์‹œ DefauleValue๊ฐ€ ์ ์šฉ๋˜๋Š”์ง€ ์ฒดํฌ
    [JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
    [DefaultValue(30)]
    public int IgnoreChangeDefaultValue4 { get; set; }
}

JsonObject test = new()
{
    IgnoreNullInt = null, // ๋ฌด์‹œ๋จ.
    IncludeNullInt = null, // ๋ฌด์‹œ ์•ˆ ๋จ.
    NotNullInt = 30, // ๋ฌด์‹œ ์•ˆ ๋จ.
    IgnoreDefaultValue = 0, // ๋ฌด์‹œ๋จ.
    IncludeDefaultValue = 0, // ๋ฌด์‹œ ์•ˆ ๋จ.
    IgnoreChangeDefaultValue = 30, // ๋ฌด์‹œ๋จ.
    IgnoreChangeDefaultValue2 = 40, // ๋ฌด์‹œ ์•ˆ ๋จ.
    IgnoreChangeDefaultValue3 = default, // ๋ฌด์‹œ ์•ˆ ๋จ.
};
string serializeString = JsonConvert.SerializeObject(test, Formatting.Indented);
Console.WriteLine(serializeString);

string testString = @"{
    ""IncludeNullInt"": null,
    ""NotNullInt"": 30,
    ""IncludeDefaultValue"": 0,
    ""IgnoreChangeDefaultValue2"": 40,
    ""IgnoreChangeDefaultValue3"": 0,
  }";

JsonObject obj = JsonConvert.DeserializeObject<JsonObject>(testString);
Console.WriteLine(obj.IgnoreChangeDefaultValue4); // Populate ์†์„ฑ์œผ๋กœ ์ง€์ •๋˜์—ˆ๋‹ค๋ฉด, DefaultValue ์— ์„ค์ •๋œ ๊ฐ’์œผ๋กœ ๊ธฐ๋ณธ๊ฐ’์ด ์ ์šฉ๋œ๋‹ค.

Json.NET์€ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” .NET Json ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ธ๋ฐ์š”. ์†์„ฑ์— ๊ฐ’์„ ์ฃผ์ง€ ์•Š์„ ๊ฒฝ์šฐ ์ง๋ ฌํ™” ์‹œ ๋ฌด์‹œํ•˜๊ณ  ์‹ถ๊ฑฐ๋‚˜, ๊ธฐ๋ณธ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜์—ฌ ์กฐ๊ฑด๋ถ€๋กœ ๋ฌด์‹œํ•˜๊ณ  ์‹ถ์„ ๊ฒฝ์šฐ๋Š” ์–ด๋–ป๊ฒŒ ํ•˜๋Š”์ง€ ์•Œ์•„๋ดค์Šต๋‹ˆ๋‹ค.

DefaultValueHandling.Populate ํ…Œ์ŠคํŠธ๋ฅผ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค.

์ข‹์•„์š” 3

Json.NET์€ ๊ฑฐ์˜ ๋Œ€๋ถ€๋ถ„์˜ ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ์ ‘ํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ผ๊ณ  ์ƒ๊ฐํ•˜๋Š”๋ฐ, ๊ด€๋ จํ•ด์„œ ๊ท€ํ•œ ํ…Œ์ŠคํŠธ ์ž๋ฃŒ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!

์ข‹์•„์š” 3