Runtime: System.Text.Json ๊ธฐ๋ณธ DateTimeZoneHandling

์— ๋งŒ๋“  2019๋…„ 10์›” 01์ผ  ยท  3์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: dotnet/runtime

JSON.NET์—์„œ ๋‹ค์Œ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

JsonConvert.DefaultSettings = () => new JsonSerializerSettings {
    DateTimeZoneHandling = DateTimeZoneHandling.Utc
};

System.Text.Json ๋น„์Šทํ•œ ๊ฒƒ์ด ์žˆ์Šต๋‹ˆ๊นŒ?
๊ธฐ๋ณธ๊ฐ’์„ ์„ค์ •ํ•˜๋Š” ์˜ต์…˜์ด ์—†์œผ๋ฉด ์ˆ˜๋™์œผ๋กœ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๊นŒ?

๋˜ํ•œ ๊ธฐ๋ณธ๊ฐ’( Local , Utc , Unspecified )์€ ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?

area-System.Text.Json

๊ฐ€์žฅ ์œ ์šฉํ•œ ๋Œ“๊ธ€

@btecu ๋ถˆํ–‰ํžˆ๋„ System.Text.Json ์—์„œ ๋น„์Šทํ•œ ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜๋Š” ์œ ์ผํ•œ ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

services.AddControllers()
    .AddJsonOptions(options =>
     {
         options.JsonSerializerOptions.Converters.Add(new DateTimeConverter());
     });

๊ทธ๋Ÿฐ ์‹์œผ๋กœ DateTimeConverter๋ฅผ ๊ตฌํ˜„ํ•˜์‹ญ์‹œ์˜ค.

public class DateTimeConverter : JsonConverter<DateTime>
{
    public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        return DateTime.Parse(reader.GetString());
    }

    public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
    {
         writer.WriteStringValue(value.ToUniversalTime().ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ssZ"));
    }
}

๋ชจ๋“  3 ๋Œ“๊ธ€

cc @layomia on ๋‚ ์งœ

DateTime์€ ์ด๋Ÿฌํ•œ ๊ฐ ํ˜•์‹(์˜ˆ: timezone\offset ํฌํ•จ ๋ฐ ์ œ์™ธ)์œผ๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ํŒ๋…๊ธฐ\์ž‘์„ฑ๊ธฐ(๋ฐ ์ง๋ ฌ ๋ณ€ํ™˜๊ธฐ)๋Š” ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๊ฒƒ์„ ์‚ฌ์šฉํ•˜๊ณ  ISO 8601 ํ˜•์‹์„ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฅผ ํ‘œํ˜„ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค.

@btecu ISO 8601 ํ”„๋กœํ•„์€ ์ง๋ ฌ ๋ณ€ํ™˜๊ธฐ์—์„œ ๊ธฐ๋ณธ์ ์œผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค(์˜ˆ: 2019-07-26T16:59:57-05:00).

์—ญ์ง๋ ฌํ™”๋ฅผ ์œ„ํ•ด datetime offset์ด "hh:mm"๋กœ ์ฃผ์–ด์ง€๋ฉด DateTimeKind.Local ๊ฐ€ ์žˆ๋Š” DateTime์ด ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. "Z"๋Š” DateTimeKind.Utc ํ•ฉ๋‹ˆ๋‹ค. ์˜คํ”„์…‹์ด ์ œ๊ณต๋˜์ง€ ์•Š์œผ๋ฉด DateTimeKind.Unspecified ๊ฐ€ ์žˆ๋Š” DateTime์ด ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.
์—ญ ๋งคํ•‘์€ ์ง๋ ฌํ™”์— ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. DateTimeKind.Local ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ DateTime ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด "hh:mm" ํ˜•์‹์˜ ์˜คํ”„์…‹์ด ์žˆ๋Š” ISO ํ‘œํ˜„์ด ์ƒ์„ฑ๋˜๋Š” ์‹์ž…๋‹ˆ๋‹ค.

System.Text.Json์˜ DateTime ๋ฐ DateTimeOffset ์ง€์›๊ณผ ์‚ฌ์šฉ์ž ์ง€์ • ๊ตฌ๋ฌธ ๋ถ„์„ ๋˜๋Š” ์„œ์‹์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ https://docs.microsoft.com/en-us/dotnet/standard/datetime/system-text-json์„ ์ฐธ์กฐ

@btecu ๋ถˆํ–‰ํžˆ๋„ System.Text.Json ์—์„œ ๋น„์Šทํ•œ ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜๋Š” ์œ ์ผํ•œ ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

services.AddControllers()
    .AddJsonOptions(options =>
     {
         options.JsonSerializerOptions.Converters.Add(new DateTimeConverter());
     });

๊ทธ๋Ÿฐ ์‹์œผ๋กœ DateTimeConverter๋ฅผ ๊ตฌํ˜„ํ•˜์‹ญ์‹œ์˜ค.

public class DateTimeConverter : JsonConverter<DateTime>
{
    public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        return DateTime.Parse(reader.GetString());
    }

    public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
    {
         writer.WriteStringValue(value.ToUniversalTime().ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ssZ"));
    }
}
์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰