一键导入
enforcing-json-options-predefine
Force JsonSerializerOptions to be declared as static readonly or readonly fields instead of creating new instances inside methods.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Force JsonSerializerOptions to be declared as static readonly or readonly fields instead of creating new instances inside methods.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
Force Regex to use GeneratedRegex Source Generator attribute instead of runtime Regex construction for compile-time optimization.
Force all DTO (Data Transfer Object) types to be declared as record instead of class for immutability and value-based equality.
Force record instantiation via positional constructor with named arguments instead of property initializer syntax for stronger immutability expression.
Enforce branch-based workflow with meaningful Korean commits and PR creation for traceable decision history.
Centralize external namespace imports (BCL, FCL, NuGet) in a GlobalUsings.cs file per project, keeping individual files clean.
基于 SOC 职业分类
| name | enforcing-json-options-predefine |
| description | Force JsonSerializerOptions to be declared as static readonly or readonly fields instead of creating new instances inside methods. |
JsonSerializerOptions는 읽기 전용(ReadOnly) 객체로 설계되어야 합니다. 메서드 내부에서 매번 새로 생성하면 불필요한 메모리 할당과 성능 저하가 발생합니다.
JsonSerializerOptions는 내부적으로 리플렉션 캐시를 유지하므로, 재사용하면 직렬화/역직렬화 성능이 크게 향상됩니다.
JsonSerializerOptions는 static readonly 필드 또는 readonly 인스턴스 필드로 사전 정의합니다new JsonSerializerOptions { ... } 를 직접 생성하지 않습니다// 메서드 내부에서 매번 JsonSerializerOptions를 새로 생성 - 성능 저하
public string GenerateJson(int count)
{
var data = GetData(count);
// 매 호출마다 새 인스턴스 생성 -> 리플렉션 캐시 재생성 -> 느림
var jsonContent = JsonSerializer.Serialize(data, new JsonSerializerOptions
{
WriteIndented = true,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
});
return jsonContent;
}
public async Task<T> Deserialize<T>(HttpResponseMessage response)
{
var json = await response.Content.ReadAsStringAsync();
// 매 호출마다 새 인스턴스 생성
return JsonSerializer.Deserialize<T>(json, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
})!;
}
// static readonly로 사전 정의 - 리플렉션 캐시 재사용, 메모리 절약
private static readonly JsonSerializerOptions JsonWriteOptions = new()
{
WriteIndented = true,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
};
private static readonly JsonSerializerOptions JsonReadOptions = new()
{
PropertyNameCaseInsensitive = true
};
public string GenerateJson(int count)
{
var data = GetData(count);
// 사전 정의된 옵션 재사용
return JsonSerializer.Serialize(data, JsonWriteOptions);
}
public async Task<T> Deserialize<T>(HttpResponseMessage response)
{
var json = await response.Content.ReadAsStringAsync();
// 사전 정의된 옵션 재사용
return JsonSerializer.Deserialize<T>(json, JsonReadOptions)!;
}
JsonSerializer.Serialize() 호출에서 사용하는 옵션JsonSerializer.Deserialize() 호출에서 사용하는 옵션