I am currently trying to implement MessagePack in a solution which contains 2 projects : AspNet Core WebAPI and a simple console app. The following package was added :
How fo I Deserialize the object back on the client, here is the code snippets, also when Posting back an object from the client to the api, do I just Serialize on the client and send it to the Post method in the api, which will take a string, take the string and Deserialize it again, I will need to pass the type somehow to the controller also.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MessagePack;
using Microsoft.AspNetCore.Mvc;
namespace WebApplication1.Controllers
{
[Route("api/[controller]")]
public class ValuesController : Controller
{
[HttpGet]
public IActionResult Get()
{
var results = new List<Superhero>();
results.Add(new Superhero { HeroID = 1, HeroName = "Bruce Wayne" });
results.Add(new Superhero { HeroID = 2, HeroName = "Selina Kyle" });
results.Add(new Superhero { HeroID = 3, HeroName = "Clark Kent" });
var bytes = MessagePackSerializer.Serialize(results);
return Ok(bytes);
}
[HttpGet("{id}")]
public string Get(int id)
{
return "value";
}
[HttpPost]
public void Post([FromBody]string value)
{
// how to I Deserialize here ? what do I just post from client to
// with the Serialized object and pass the type also ???
}
[HttpPut("{id}")]
public void Put(int id, [FromBody]string value)
{
}
[HttpDelete("{id}")]
public void Delete(int id)
{
}
}
[MessagePackObject]
public class Superhero
{
[Key(0)]
public int HeroID { get; set; }
[Key(1)]
public string HeroName { get; set; }
}
}
using MessagePack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace MessagePackExampleOne
{
class Program
{
static HttpClient client = new HttpClient();
static void Main(string[] args)
{
client.BaseAddress = new Uri("http://localhost:57752");
client.DefaultRequestHeaders.Accept.Clear();
HttpResponseMessage response = client.GetAsync("/api/values").Result;
if (response.IsSuccessStatusCode)
{
var result = response.Content.ReadAsStringAsync().Result;
//how to Deserialize this objec ??
// Console.WriteLine(MessagePackSerializer.ToJson(result));
// var mc2 = MessagePackSerializer.Deserialize<List<Superhero>>(result);
}
Console.Read();
}
}
[MessagePackObject]
public class Superhero
{
[Key(0)]
public int HeroID { get; set; }
[Key(1)]
public string HeroName { get; set; }
}
}