2

For a .NET Core project, I'm consuming a public API that returns data formatted as JSON. However, some (not all) of their responses have a BOM character at the start of the string, which causes Visual Studio and Json.NET to not recognize the string as valid JSON. As a result, I get an error when using JsonConvert.DeserializeObject() to deserialize the string into my POCO object. I've been told by the API developers that the BOM is included by design, and that I should "set Json.Net to expect that". Is there a way to set Json.NET to handle the BOM without stripping it off the string manually?

Example follows, by request. You can see when the API GET completes successfully, I'm having to trim the BOM manually from the start of the string, otherwise the call to DeserializeObject() fails because the string is not valid JSON.

private static MyPOCO GetObjectFromApi(string url)
{
    MyPOCO poco = new MyPOCO();

    RestClient client = new RestClient(url);
    RestRequest request = new RestRequest(Method.GET);
    IRestResponse response = client.Execute(request);

    if (response.IsSuccessful)
    {
        poco = JsonConvert.DeserializeObject<MyPOCO>(response.Content.TrimStart((char)65279)); // trim the byte order marker character at the start of the string
        //poco = JsonConvert.DeserializeObject<MyPOCO>(response.Content); // this would throw an error because response.Content is not valid JSON
    }
    else
    {
        MyLogger.WriteLog("Api returned failure response");
    }

    return poco;
}
melicent
  • 1,221
  • 15
  • 22
  • Most likely you are reading some sequence of bytes using `Encoding.UTF8.GetString(rawData);`. Instead, you should use a `StreamReader`, it consumes the BOM automatically. See [How do I ignore the UTF-8 Byte Order Marker in String comparisons?](https://stackoverflow.com/a/2915239/3744182). See also [Strip Byte Order Mark from string in C#](https://stackoverflow.com/q/1317700/3744182) which has some other options, but IMO using a `StreamReader` is best since it works automatically. – dbc Aug 14 '18 at 20:52
  • @dbc I'm using RestSharp to execute an API call and then using Json.NET to deserialize the IRestResponse.Content string into a POCO class. I don't mind stripping the BOM character if that's what I need to do, but the developer of the API indicated that I should be able to "set" Json.NET to handle this automatically. That's what I'm asking about. Sorry if I wasn't clear. – melicent Aug 14 '18 at 21:01
  • Then, can you share a [mcve] showing what you are doing now and indicate the point at which the developer indicated you *should be able to "set" Json.NET to handle this automatically*? Json.NET does support [deserializing from a stream](https://www.newtonsoft.com/json/help/html/Performance.htm#MemoryUsage) but beyond that I'm not aware of any such setting in [`JsonTextReader`](https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_JsonTextReader.htm). – dbc Aug 14 '18 at 21:10
  • @dbc I've edited my post with the example. They didn't say where I could set Json.NET to handle it - they only said that I should. – melicent Aug 14 '18 at 21:19

0 Answers0