Open棟梁Project - マイクロソフト系技術情報 Wiki
とある案件の要件で、
非構造化データを粘土細工のように
ゴリゴリと処理したいが、そういったことは、可能なのだろうか?
ということを調査した。
ココが参考になる。
[JsonObject("aaa")] public class AAA { [JsonProperty("prop1")] public int property1 { get; set; } [JsonProperty("prop2")] public string property2 { get; set; } }
AAA aaa = new AAA(); aaa.property1 = 100; aaa.property1 = "xxx"; string json = JsonConvert.SerializeObject(aaa);
AAA aaa = JsonConvert.DeserializeObject<AAA>(json);
object obj = JsonConvert.DeserializeObject(json);
[JsonObject("aaa")] public class AAA { [JsonProperty("prop1")] public int property1 { get; set; } [JsonProperty("prop2")] [DefaultValue("hogehoge")] public string property2 { get; set; } }
設定値 | 動作 |
Include | シリアライズ時に既定値の項目をJSONに含める(既定)。 |
Ignore | シリアライズ時に既定値の項目をJSONに含めない。 |
Populate | デシリアライズ時にJSON文字列中に要素が存在しない場合でも既定値を設定。 |
IgnoreAndPopulate? | IgnoreとPopulateの同時指定。 |
[JsonProperty("prop2", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)] [DefaultValue("hogehoge")] public string property2 { get; set; }
string json = JsonConvert.SerializeObject(aaa, Formatting.Indented, new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate });
AAA aaa = JsonConvert.DeserializeObject<UserModel>(jsonstring, new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate });
上記に
「階層構造を持ったJSONでも問題なくデシリアライズ可能。
JSON配列もListなどにパースしてくれる。」
とあるが、何処までやってくれるか?
・・・検証の結果、
[JsonObject("aaa")] public class AAA { [JsonProperty("prop1")] public int property1 { get; set; } [JsonProperty("prop2")] [DefaultValue("hogehoge")] public string property2 { get; set; } [JsonProperty("prop3")] public AAA property3 { get; set; } } static void Main(string[] args) { AAA aaa = new AAA(); aaa.property1 = 100; aaa.property2 = "xxx"; aaa.property3 = new AAA(); aaa.property3.property1 = 200; aaa.property3.property2 = "yyy"; string json = JsonConvert.SerializeObject(aaa); Console.WriteLine(json); aaa = JsonConvert.DeserializeObject<AAA>(json);
[JsonObject("aaa")] public class AAA { [JsonProperty("prop1")] public int property1 { get; set; } [JsonProperty("prop2")] [DefaultValue("hogehoge")] public string property2 { get; set; } [JsonProperty("prop3")] public object property3 { get; set; } } static void Main(string[] args) { AAA aaa1 = new AAA(); AAA aaa2 = new AAA(); aaa1.property1 = 100; aaa1.property2 = "xxx"; aaa2.property1 = 200; aaa2.property2 = "yyy"; aaa1.property3 = aaa2; string json = JsonConvert.SerializeObject(aaa1); Console.WriteLine(json); aaa1 = JsonConvert.DeserializeObject<AAA>(json);
配列(Generic)も問題なく処理できる。
[JsonObject("aaa")] public class AAA { [JsonProperty("prop1")] public int property1 { get; set; } [JsonProperty("prop2")] [DefaultValue("hogehoge")] public string property2 { get; set; } [JsonProperty("prop3")] public AAA property3 { get; set; } }
static void Main(string[] args) { AAA aaa1 = new AAA(); AAA aaa2 = new AAA(); aaa1.property1 = 100; aaa1.property2 = "xxx"; aaa2.property1 = 200; aaa2.property2 = "yyy"; aaa1.property3 = aaa2; List<AAA> lstaaa = new List<AAA>(); lstaaa.Add(aaa1); lstaaa.Add(aaa2); string json = JsonConvert.SerializeObject(lstaaa); Console.WriteLine(json); lstaaa = JsonConvert.DeserializeObject<List<AAA>>(json);
static void Main(string[] args) { List<Dictionary<string, string>> parent = new List<Dictionary<string, string>>(); Dictionary<string, string> child; child = new Dictionary<string, string>(); child["aaa"] = "AAA"; child["bbb"] = "BBB"; child["ccc"] = "CCC"; parent.Add(child); child = new Dictionary<string, string>(); child["xxx"] = "XXX"; child["yyy"] = "YYY"; child["zzz"] = "ZZZ"; parent.Add(child); string json = JsonConvert.SerializeObject(parent); Console.WriteLine(json); parent = JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(json);
派生型をシリアライズできるが、問題は派生型にデシリアライズできないこと。
[JsonObject("aaa")] public class AAA { [JsonProperty("prop1")] public int property1 { get; set; } [JsonProperty("prop2")] [DefaultValue("hogehoge")] public string property2 { get; set; } [JsonProperty("prop3")] public AAA property3 { get; set; } } [JsonObject("bbb")] public class BBB : AAA { [JsonProperty("prop4")] public int property4 { get; set; } } [JsonObject("ccc")] public class CCC : AAA { [JsonProperty("prop4")] public string property4 { get; set; } }
static void Main(string[] args) { AAA aaa1 = new AAA(); AAA aaa2 = new AAA(); aaa1.property1 = 100; aaa1.property2 = "xxx"; aaa1.property3 = new BBB(); aaa2.property1 = 200; aaa2.property2 = "yyy"; aaa2.property3 = new CCC(); List<AAA> lstaaa = new List<AAA>(); lstaaa.Add(aaa1); lstaaa.Add(aaa2); string json = JsonConvert.SerializeObject(lstaaa); Console.WriteLine(json); lstaaa = JsonConvert.DeserializeObject<List<AAA>>(json);
型が特定できない構造のJSONをparseする例