0

This is the deserialization that produces the problem:

public MyType ProblematicDeserialization(jsonString)
{
    return Newtonsoft.Json.JsonConvert.DeserializeObject<MyType>(jsonString);   
}

It works or it doesn't deppending on how jsonString is loaded:

CASE 1:

myObjectType serialized with json.net as a string and then written into filePath:

//This line works correctly:
dynamic correctlyWorkingJson = IO.File.ReadAllText(filePath, Text.Encoding.UTF8);

CASE 2

Same as CASE 1, but the content of filePath has been copied and then pasted into a json resource in my project:

//This line gives an ERROR: ""Unexpected character encountered while parsing value: . Path '', line 0, position 0."
dynamic notWorkingJson = GetJsonFromResource(resourceName);

private string GetJsonFromResource(string resourceName)
{
    byte[] jsonBytes = Convert.ToByte(ResourcesManager.GetResource(resourceName));
    if (jsonBytes == null) {
        throw new Exception(string.Format("Resource '{0}.json' was not found.", resourceName));
    }
    string json = UTF8BytesToString(jsonBytes);
    return json;
}

On the debugger, both correctlyWorkingJson and notWorkingJson look exactly the same, but obviously there is something that makes the resource json not acceptable for the json.net deserialization.

Xavier Peña
  • 7,399
  • 9
  • 57
  • 99
  • 1
    Can you do a `for(var i=0;i<5;i++){Debug.WriteLine(jsonBytes[i]) ;}` after the Convert.ToByte? (I would think a simple cast to byte[] should work as well btw) – rene Oct 01 '15 at 07:40
  • @rene First five bytes are "239,187,191,123,34" – Xavier Peña Oct 01 '15 at 07:43
  • "239,187,191" are a [byte order mark](https://en.wikipedia.org/wiki/Byte_order_mark). It may have been put there by your method `UTF8BytesToString()`. What is the source code for this method? – dbc Oct 01 '15 at 07:48
  • @dbc UTF8BytesToString() has a single line: `Return System.Text.Encoding.UTF8.GetString(bytes)`. – Xavier Peña Oct 01 '15 at 07:50
  • Then the BOM is in the byte array. What is `Convert.ToByte(ResourcesManager.GetResource(resourceName));` intended to do? A JSON file is a text file, to read an embedded text file from resources see [How to read embedded resource text file](http://stackoverflow.com/questions/3314140/how-to-read-embedded-resource-text-file). – dbc Oct 01 '15 at 08:03
  • @dbc Thanks, your hint helped me solve the problem. I have written an answer below about it. About "reading embedded source text file", the thing is that if you want to keep the extension as .json in your file (which has the advantage of Visual Studio letting you edit is as json), then it does not quite treat it as a text file when loading the resource... at least with the default loader. I will take a look at your link and try the `StreamReader`. – Xavier Peña Oct 01 '15 at 08:09

2 Answers2

1

After /u/dbc's comment that the byte sequence indicated that the encoding of the resource file was UTF-8-BOM, I solved it this way:

  • I went to the source file in disk that was treated as a resource in my project
  • I edited it with Notepad++
  • Encoding -> Convert to UTF-8

After that, the exact same code posted in the original post worked perfectly.

Xavier Peña
  • 7,399
  • 9
  • 57
  • 99
0

First you sample does not compile, I assume you mean

public MyType ProblematicDeserialization(s)
{
    return Newtonsoft.Json.JsonConvert.DeserializeObject<MyType)(s);   
}
Richard Schneider
  • 34,944
  • 9
  • 57
  • 73