using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
public static string fileText = "";
static void Main(string[] args)
Dictionary<string, object> dict = new Dictionary<string, object>();
dict.Add("employee","john");
dict.Add("sale",new Sale(9,5243));
dict.Add("restaurant",new Restaurant("Cheese Cake Factory", "New York"));
DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(Dictionary<string, object>));
MemoryStream msObj = new MemoryStream();
js.WriteObject(msObj, dict);
StreamReader sr = new StreamReader(msObj);
string json = sr.ReadToEnd();
Dictionary<string, object> result = new Dictionary<string, object>();
using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(json)))
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Dictionary<string, object>));
result = serializer.ReadObject(stream) as Dictionary<string, object>;
[KnownType(typeof(Sale))]
[DataMember(Name = "SaleId")]
public int SaleId {get; set;}
[DataMember(Name = "Total")]
public int Total{ get; set;}
public Sale(int saleid, int total)
[DataContract(Name = "Restaurant", Namespace="")]
[KnownType(typeof(Restaurant))]
[DataMember(EmitDefaultValue = false)]
public string Name {get; set;}
[DataMember(EmitDefaultValue = false)]
public string City{get; set;}
public Restaurant(string name, string city)