- 追加された行はこの色です。
- 削除された行はこの色です。
[[Open棟梁Project>http://opentouryo.osscons.jp/]] - [[マイクロソフト系技術情報 Wiki>http://techinfoofmicrosofttech.osscons.jp/]]
* 目次 [#pe665aa8]
#contents
*概要 [#o65cd171]
とある案件の要件で、非構造化データを粘土細工のように~
ゴリゴリと処理したいが、そういったことは、可能なのだろうか?
*JSON.NET [#tf2395f2]
ココが参考になる。
-C#でJSONを扱うライブラリ「Json.NET」を使ってみました - Qiita~
http://qiita.com/ta-yamaoka/items/a7ff1d9651310ade4e76
**基本 [#tf0108ab]
***シリアライズ・デシリアライズ [#df8d3cd2]
[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);
---AAA型にparseされる。
--型を指定せずにデシリアライズ
object obj = JsonConvert.DeserializeObject(json);
---JObject型にparseされる。
***既定値 [#adf4b3f4]
-プロパティの既定値の設定~
プロパティの属性にSystem.ComponentModel.DefaultValue属性を指定する。
[JsonObject("aaa")]
public class AAA
{
[JsonProperty("prop1")]
public int property1 { get; set; }
[JsonProperty("prop2")]
[DefaultValue("hogehoge")]
public string property2 { get; set; }
}
-シリアライズ(デシリアライズ時)の既定値の取り扱い~
列挙体DefaultValueHandlingでシリアライズ(デシリアライズ時)の既定値の取り扱い方法を制御できる。
--DefaultValueHandling
|設定値|動作|h
|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 });
**応用 [#j19bc906]
上記に
>「階層構造を持ったJSONでも問題なくデシリアライズ可能。~
JSON配列もListなどにパースしてくれる。」
とあるが、何処までやってくれるか?
・・・検証の結果、型が特定できない構造は、~
JObjectを使用して匿名型にparseできそう。
***階層構造を持ったBean [#q9cc3f69]
-型を明示
[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);
-型を明示しない~
オブジェクト型を使用する(JObject型にparseされる)。
[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);
***配列(List<AAA>) [#c749f4e3]
配列(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);
***Primitive型とGeneric型 [#p067db84]
-Primitive型とGeneric型の範囲であれば、型が特定されるので、parseできる。
-このため、Primitive型とGeneric型の範囲で自由な構造を組むことができる。
-しかし、Genericでは、Beanのように、フィールド型が自由な構造を組めない。
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);
***継承を使用 [#q73512eb]
派生型をシリアライズできるが、問題は派生型にデシリアライズできないこと。
[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);
***その他 [#g2f2386f]
型が特定できない構造をparseする例
-memo: C#でJSON配列のパースに苦労した話~
https://kazyx.blogspot.com/2013/09/cjson.html
-JSON.NETで変なJSONを読み込む方法 - かずきのBlog@hatena~
http://blog.okazuki.jp/entry/2014/04/19/235806
**参考 [#w963879c]
-Json.NET - Newtonsoft~
http://www.newtonsoft.com/json