프로그래밍/C#

[C#] JsonSerialize Enum -> String

Victory_HA 2024. 6. 27. 15:23
Enum ScoreType
{
   High,
   Middle,
   Low
}
Class ResultData
{
   ScoreType type {get;set;}
   double value {get;set;}
}
// init value
var resdata = new ResultData()
{
    type = ScoreType.High,
    value = 0.1
};


//json option setting
var jsonSetting = new JsonSerializerSettings
{
    Converters = new List<JsonConverter> { new StringEnumConverter() },
    Formatting = Formatting.Indented,
};

//convert to json
string dataStream = JsonConvert.SerializeObject(resdata, jsonSetting);

File.WriteAllText(fileSavePath, dataStream);



결과데이터

type = "High"
value = 0.1