Cysharp / MemoryPack

Zero encoding extreme performance binary serializer for C# and Unity.
MIT License
3.29k stars 193 forks source link

Example how to Serialize to string then from string orginal data #203

Closed guillaume-chervet closed 7 months ago

guillaume-chervet commented 11 months ago

Hi Thank you for your awseome library.

I am not good at writing optimized code in C#. I would like to know what is the best way to serialize an object to string.

I do not manage to make it works.

var bytes = MemoryPackSerializer.Serialize(data});
// then convert to string
string dataString = ...
// then serialize back to bytes
var finalBytes = ... // From string
var finalData = MemoryPackSerializer.Deserialize<DataType>(bytes)

Other question, do you have a sample how to make an HTTP client POST a bytes array (for example a multipart form data) to an C# API and how to read it back and convert it back to the original data inside ASP.NET Core ?

hadashiA commented 7 months ago

Hi,

I would like to know what is the best way to serialize an object to string.

Officially, we do not provide any views as string format.

Therefore, please consider the common method of encoding binaries into strings. For example, the standard lib has Convert.ToBase64String.

Other question, do you have a sample how to make an HTTP client POST a bytes array

Seems like a basic topic that is off topic for this repo. Also, I think it depends on the framework you are using, so I will just suggest the following here.

You can probably find samples by looking up these keywords.

EDIT: I brought up application/octet-stream in general terms, but let me correct myself. The README defined a Content-Type of application/x-memorypack. The following sections of the README may be helpful.

If you call from HttpClient, you can set application/x-memorypack to content-header. var content = new ByteArrayContent(bin) content.Headers.ContentType = new MediaTypeHeaderValue("application/x-memorypack");

hadashiA commented 7 months ago

Sorry, I forgot to mention. Officially, we provide MemoryPack.AspNetCoreMvcFormatter package, which may be helpful if you are using ASP .NET Core. thanks.