Exemplo n.º 1
0
        static void Main(String[] args)
        {
            BlogSite1 bsObj = new BlogSite1()
            {
                Name        = "C-sharpcorner",
                Description = "Share Knowledge"
            };
//Serializes objects to the json.and deserialize json data to object. this class cannot be inherited.
            DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(BlogSite1));
            //creating a stream whose backing store is memory.
            MemoryStream msObj = new MemoryStream();

//Serializes a specific object to json data and writes the resulting json to a stream.
            js.WriteObject(msObj, bsObj);
//set or get the current position within the stream.
            msObj.Position = 0;
//Implements a TextReader that reads character from a byte stream in a particular encoding.
            StreamReader sr = new StreamReader(msObj);
//Reads all characters from the current position to the end of stream.
            string json = sr.ReadToEnd();

            Console.WriteLine(json);
            sr.Close();
            msObj.Close();
            Console.ReadLine();
        }
Exemplo n.º 2
0
        static void Main(String[] args)
        {
            string json = "{\"Description\":\"Share Knowledge\",\"Name\":\"C-sharpcorner\"}";

            using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
            {
                DataContractJsonSerializer deserializer = new DataContractJsonSerializer(typeof(BlogSite1));
//Reads a document stream in the JSON format.and return the deserialized object.
                BlogSite1 bsObj2 = (BlogSite1)deserializer.ReadObject(ms);
                Console.WriteLine("Name: " + bsObj2.Name);
                Console.WriteLine("Discription: " + bsObj2.Description);
                Console.ReadLine();
            }
        }