[C#]Jsonシリアライズ サンプルコード

# 主題
Json.NETライブラリを使用したJSONシリアライズサンプルコードを備忘録として記載。
# Json.NET
https://www.newtonsoft.com/json
# 単一オブジェクトのシリアライズ
```c#
public static string SerializeObject()
{
    Product product = new Product();
    product.Name = "Apple";
    product.Expiry = new DateTime(2008, 12, 28);
    product.Price = 3.99M;
    product.Sizes = new string[] { "Small", "Medium", "Large" };
    product.list = new List<int>(); product.list.Add(1); product.list.Add(2); product.list.Add(3);

    return JsonConvert.SerializeObject(product, Formatting.Indented);
      
}
```
```
{
  "Name": "Apple",
  "Expiry": "2008-12-28T00:00:00",
  "Price": 3.99,
  "Sizes": [
    "Small",
    "Medium",
    "Large"
  ],
  "list": [
    1,
    2,
    3
  ]
}
```

メンバに配列やコレクションがあっても問題なく動作する。
ちなみに、publicメンバ以外はシリアライズされないことに注意。（実際これではまった）



# 一次元コレクションのシリアライズ
```c#
public static string SerializeSingleCollection()
{
    Product product1 = new Product();
    product1.Name = "Apple";
    product1.Expiry = new DateTime(2008, 12, 28);
    product1.Price = 3.99M;
    product1.Sizes = new string[] { "Small", "Medium", "Large" };
    product1.list = new List<int>(); product1.list.Add(1); product1.list.Add(2); product1.list.Add(3);

    Product product2 = new Product();
    product2.Name = "Orange";
    product2.Expiry = new DateTime(2008, 12, 28);
    product2.Price = 3.99M;
    product2.Sizes = new string[] { "Small", "Medium", "Large" };
    product2.list = new List<int>(); product2.list.Add(1); product2.list.Add(2); product2.list.Add(3);

    List<Product> products = new List<Product>();
    products.Add(product1);
    products.Add(product2);

    return JsonConvert.SerializeObject(products);
 
}

```

```
  {
    "Name": "Apple",
    "Expiry": "2008-12-28T00:00:00",
    "Price": 3.99,
    "Sizes": [
      "Small",
      "Medium",
      "Large"
    ],
    "list": [
      1,
      2,
      3
    ]
  },
  {
    "Name": "Orange",
    "Expiry": "2008-12-28T00:00:00",
    "Price": 3.99,
    "Sizes": [
      "Small",
      "Medium",
      "Large"
    ],
    "list": [
      1,
      2,
      3
    ]
  }
]
```

# 多次元コレクションのシリアライズ

公式のサンプルコードには載っていないが、
多次元コレクションの場合でも同じようにシリアライズ可能

```c#

private static List<Product> Products()
{
    Product product1 = new Product();
    product1.Name = "Apple";
    product1.Expiry = new DateTime(2008, 12, 28);
    product1.Price = 3.99M;
    product1.Sizes = new string[] { "Small", "Medium", "Large" };
    product1.list = new List<int>(); product1.list.Add(1); product1.list.Add(2); product1.list.Add(3);

    Product product2 = new Product();
    product2.Name = "Orange";
    product2.Expiry = new DateTime(2008, 12, 28);
    product2.Price = 3.99M;
    product2.Sizes = new string[] { "Small", "Medium", "Large" };
    product2.list = new List<int>(); product2.list.Add(1); product2.list.Add(2); product2.list.Add(3);

    List<Product> products = new List<Product>();
    products.Add(product1);
    products.Add(product2);

    return products;
}


public static string SerializeMultipleCollection()
{
    List<List<Product>> products_list = new List<List<Product>>();
    products_list.Add(Products());
    products_list.Add(Products());
    products_list.Add(Products());
    return JsonConvert.SerializeObject(products_list, Formatting.Indented);
   
}
```

```
[
  [
    {
      "Name": "Apple",
      "Expiry": "2008-12-28T00:00:00",
      "Price": 3.99,
      "Sizes": [
        "Small",
        "Medium",
        "Large"
      ],
      "list": [
        1,
        2,
        3
      ]
    },
    {
      "Name": "Orange",
      "Expiry": "2008-12-28T00:00:00",
      "Price": 3.99,
      "Sizes": [
        "Small",
        "Medium",
        "Large"
      ],
      "list": [
        1,
        2,
        3
      ]
    }
  ],
  [
    {
      "Name": "Apple",
      "Expiry": "2008-12-28T00:00:00",
      "Price": 3.99,
      "Sizes": [
        "Small",
        "Medium",
        "Large"
      ],
      "list": [
        1,
        2,
        3
      ]
    },
    {
      "Name": "Orange",
      "Expiry": "2008-12-28T00:00:00",
      "Price": 3.99,
      "Sizes": [
        "Small",
        "Medium",
        "Large"
      ],
      "list": [
        1,
        2,
        3
      ]
    }
  ],
  [
    {
      "Name": "Apple",
      "Expiry": "2008-12-28T00:00:00",
      "Price": 3.99,
      "Sizes": [
        "Small",
        "Medium",
        "Large"
      ],
      "list": [
        1,
        2,
        3
      ]
    },
    {
      "Name": "Orange",
      "Expiry": "2008-12-28T00:00:00",
      "Price": 3.99,
      "Sizes": [
        "Small",
        "Medium",
        "Large"
      ],
      "list": [
        1,
        2,
        3
      ]
    }
  ]
]
```
#参考

https://stackoverflow.com/questions/13469765/how-to-deseralize-json-object-that-contains-multidimensional-array