41
1
using System;
2
using Newtonsoft.Json;
3
4
using System.ComponentModel;
5
6
7
public class Program
8
{
9
public static void Main()
10
{
11
12
13
14
JsonSerializerSettings ser = new JsonSerializerSettings ();
15
16
17
ser.DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate;
18
19
//Serialization
20
var intObj = new ColorInfo() { ColorId=2, ColorName="Red",EffectiveDate = DateTime.MinValue, ColorCode=20 };
21
22
var strOut = JsonConvert.SerializeObject(intObj,ser);
23
Console.WriteLine(strOut);
24
25
//Deserialization
26
var jsonInput ="{'ColorId':1,'ColorName':'Red','EffectiveDate':'0001-01-01T00:00:00'}";
27
var clsInfo = JsonConvert.DeserializeObject<ColorInfo>(jsonInput,ser);
28
Console.WriteLine(String.Format("Id : {0}, Name : {1}, Effective Date: {2}, ColorCode : {3}",
29
clsInfo.ColorId, clsInfo.ColorName, clsInfo.EffectiveDate, clsInfo.ColorCode));
30
}
31
32
public class ColorInfo
33
{
34
public int ColorId {get;set;}
35
36
public string ColorName{get;set;}
37
public DateTime EffectiveDate{get;set;}
38
[DefaultValue(20)]
39
public int ColorCode{get;set;}
40
}
41
}
Cached Result