static void Main(string[] args) { List <Lizard> lizards = Lizard.GetLizards(); //SerializaOneLizard("one.json", lizards[0]); //SerializeAllLizards("all.json", lizards); //DeserializaOneLizard("one.json"); DeserializaAllLizards("all.json"); }
static void DeserializaOneLizard(string filename) { using (TextReader reader = new StreamReader(filename)) { JavaScriptSerializer serializer = new JavaScriptSerializer(); string input = reader.ReadToEnd(); Lizard lizard = serializer.Deserialize <Lizard>(input); Console.WriteLine(lizard); } }
static void SerializaOneLizard(string filename, Lizard lizard) { JavaScriptSerializer serializer = new JavaScriptSerializer(); string output = serializer.Serialize(lizard); using (TextWriter writer = new StreamWriter(filename)) { writer.Write(output); } }
static void SerializeOneLizard(string filename, Lizard lizard) { JavaScriptSerializer serializer = new JavaScriptSerializer(); using (TextWriter writer = new StreamWriter(filename)) { string result = serializer.Serialize(lizard); writer.WriteLine(result); } }
static void DeserializeOneLizard(string filename) { JavaScriptSerializer serializer = new JavaScriptSerializer(); using (TextReader reader = new StreamReader(filename)) { //read the file and store into a string string data = reader.ReadToEnd(); //data cast to LIZARD, deserialize and store on the Lizard list. Lizard lizard = serializer.Deserialize <Lizard>(data); Console.WriteLine(lizard); } }