0

My WebApi returning UTF16 JSON result. Attempt to deserialize this using JSON.NET resulted in error:

Unexpected character encountered while parsing value: . Path '', line 0, position 0.

I looked into Deserialize method with setting, looks like no option to specify the encoding. UTF8 is hardcoded into the parser?

It seems like JSON.NET can't deserialize UTF16, either the server has to response with UTF8, or the consumer need to re-encode the result from 16 to 8. Are these 2 only the resolution?

Kelmen
  • 1,053
  • 1
  • 11
  • 24
  • Are you saying that .NET has no native charset-independent text type? – Ignacio Vazquez-Abrams Sep 23 '15 at 03:37
  • 1
    How are you using Json.NET? If you are feeding it a `string`, then you might have a BOM at the beginning, see [Parse JSON C# Error](http://stackoverflow.com/questions/14181193/parse-json-c-sharp-error). If you are using [`JsonTextReader`](http://www.newtonsoft.com/json/help/html/ReadJson.htm), then the underlying `StreamReader` needs to have the correct encoding. – dbc Sep 23 '15 at 05:46

2 Answers2

0

I'm using System.Net.WebClient to invoke the service. 1 of the googling result hinting need to tackle content being compressed, and my service response does showing gzip in the header

By switching to use System.Net.WebRequest, the problem resolved, and I have no need to concern with compressed content.

So the cause is probably nothing about JSON.NET nor encoding, but due to compressed content when using System.Net.WebClient.

Kelmen
  • 1,053
  • 1
  • 11
  • 24
0

I was getting a similar issue but the reason was Encoding. My JSON file was in UTF-16 LE (Unicode). So at the time of reading data, we need to provide encoding as Unicode only.

var jsonString = System.IO.File.ReadAllText("c://filepath", Encoding.Unicode);

var data = JsonConvert.DeserializeObject<YourModel>(jsonString);