Search code examples
c#.netunity-game-engineencodingjson.net

UTF8 to ASCII conversion in C#


I am aware that there is already a very similar question here, but unfortunately the top answer did not help me.

I want to POST some data to a RESTful web service via Unity Webrequest. As the documentation says,

The data in postData will be escaped, then interpreted into a byte stream via System.Text.Encoding.UTF8.

The data in postData is stored in JSON format. The remote web service will deserialize all incoming data using NewtonSoft.Json.

Unfortunately, the remote web service throws a JsonReaderException, with the message "Unexpected character encountered while parsing value: %. Path '', line 0, position 0." That is because the posted data look something like this as a string (truncated):

%7b%22LogType%22%3a%22Drill%22%2c%22Action%22%3a%22Started%22%2c%22Description%22%3a%22...

I tried to translate the incoming data from UTF-8 into ASCII before calling the Newtonsoft.Json library to deserialize it:

byte[] contentBytes = await Request.Content.ReadAsByteArrayAsync();
string postContents = Encoding.ASCII.GetString(Encoding.Convert(Encoding.UTF8, Encoding.ASCII, contentBytes));

However, JsonReaderException is still thrown, because postContents still looks something like this:

%7b%22LogType%22%3a%22Drill%22%2c%22Action%22%3a%22Started%22%2c%22Description%22%3a%22...

What should I do to ensure that the remote web service can successfully deserialize the data it receives?


Solution

  • The data you're receiving is URL encoded.

    For example:

    • %7b is {
    • %22 is "
    • %3a is :

    So the start of the received URL encoded string is this:

    {"LogType":"Drill"

    which is clearly a valid JSON string.

    So, what you need to do is to url decode it to a regular JSON string, and then use JSON.NET to deserialize it.