Created
June 27, 2018 17:07
-
-
Save GtTmy/b1edf7669347d3cd20ec022f64f6fa91 to your computer and use it in GitHub Desktop.
Json.Netでのデシリアライズの確認
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Newtonsoft.Json; | |
using System; | |
using System.Collections.Generic; | |
namespace JsonDemo | |
{ | |
class Program | |
{ | |
public static void Main(string[] arg) | |
{ | |
var x = new Test(); | |
x.Items.Clear(); | |
x.Items.Add(6); | |
var json = JsonConvert.SerializeObject(x); | |
var y = JsonConvert.DeserializeObject<Test>(json); | |
foreach (var i in y.Items) | |
{ | |
System.Diagnostics.Debug.Write(i.ToString() + ","); | |
// -> 1,2,3,4,5,6, | |
} | |
System.Diagnostics.Debug.WriteLine(""); | |
var settings = new JsonSerializerSettings | |
{ | |
ObjectCreationHandling = ObjectCreationHandling.Replace, | |
}; | |
var z = JsonConvert.DeserializeObject<Test>(json, settings); | |
foreach (var i in z.Items) | |
{ | |
System.Diagnostics.Debug.Write(i.ToString() + ","); | |
// -> 6, | |
} | |
System.Diagnostics.Debug.WriteLine(""); | |
} | |
public class Test | |
{ | |
public IList<int> Items { get; set; } = new List<int> { 1, 2, 3, 4, 5 }; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment