Json - Serialize and DeSerialize objects - sample project - return webapi data with httppost- parameters , frombody and string parameter

 




** json serialize - deserialize

  public static void Serialize(object value, Stream s)
        {
            using (StreamWriter writer = new StreamWriter(s))
            using (JsonTextWriter jsonWriter = new JsonTextWriter(writer))
            {
                JsonSerializer ser = new JsonSerializer();
                ser.Serialize(jsonWritervalue);
                jsonWriter.Flush();
            }
        }

        public static T Deserialize<T>(Stream s)
        {
            using (StreamReader reader = new StreamReader(s))
            using (JsonTextReader jsonReader = new JsonTextReader(reader))
            {
                JsonSerializer ser = new JsonSerializer();
                return ser.Deserialize<T>(jsonReader);
            }
        }


**** webapi - [HttpPost]
  [HttpPost]
        [Route("Urunler/UretimdenKaldirilanlar")]
        public async Task<System.Web.Http.IHttpActionResultUretimdenKaldirilanlar(
          [FromBodyUserModel dsUser, string dsApiPass)
        {
          //first control dsuser and dsapipass..

            var s1 = await (from u in db.Products
                            where u.Discontinued == true 
                            select new
                            {
                                u.ProductID
                            ,
                                u.ProductName
                            ,
                                u.UnitPrice
                            }).OrderBy(p => p.ProductName).ToListAsync();

            if (s1 == null)
            {
                return NotFound();
            }

            return Ok(s1);
        } 

    }

******** console project - main -
static async Task Main(string[] args) { Uri url = new Uri("http://localhost:51062/Urunler/UretimdenKaldirilanlar?dsApiPass=1234"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json")); client.DefaultRequestHeaders.Add("User-Agent", "aydin"); UserModel usr = new UserModel(); usr.KulaniciAdi = "a.turker"; usr.Sifre= "1234"; var contentUser = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(usr), Encoding.UTF8, "application/json"); var ctn = await client.PostAsync(url.ToString(), contentUser); var result = await ctn.Content.ReadAsStreamAsync(); List<Product> Urunler = Utils.Deserialize<List<Product>>(result); foreach (var urun in Urunler) { Console.WriteLine(string.Format("ID : {0} ", urun.ProductID)); Console.WriteLine(string.Format("NAME : {0} ", urun.ProductName)); Console.WriteLine(string.Format("PRICE : {0} ", urun.UnitPrice)); Console.WriteLine("-------------------------------------------"); } Console.ReadLine(); }

****************** product class
 public class Product
    {
        public int ProductID{get;set;}
        public string ProductName { getset; }
        public double UnitPrice { getset; }
         

    }

****************************** usermodel class

  public class UserModel
    {

        public string KulaniciAdi;
        public string Sifre;



    }

Yorumlar