2

I am not making some sort of a connection between a string passed to DeserializeObject and when I copy the value of the same string and parse in in JsonLint.

In other words I serialize an object (a configuration object) and then take the produced file and add it as a resource to the wpf application.

Someone selects one of the templated configurations and I obviously want to deserialize the object and that is when I run into trouble.

There is something different in the way the string object is passed in code because when I take the string value and paste it into JsonLint it parses saying it is valid?

byte[] contents = global::[MyProj].Properties.Resources.config1;
string result = System.Text.Encoding.UTF8.GetString(contents);

var deserialized = JsonConvert.DeserializeObject<rtfMasterContext>(result);

Amy asked for the first 8 bytes to see if it was UTF8 Encoded. Now this will reveal my lack of knowledge but it wouldn't be encoded until Text.Encoding line finishes? On the other hand the resource is a json file that already is...utf8 encoded??

First Eight Bytes

According to wikipedia a UTF8 is 239 187 191 in decimal form.

I'm gonna look into that and I'll report back.


So based upon the article and information from the comments if my file is of type json then WPF reads it in as a byte[] and prepends a UTF8 BOM.

Converting it to a string is not a problem but then deserializing the BOM throws off the json parser.

So I changed my config file type to txt and now it works as the resource is read in as a string...but I would like to have an answer to my OP.

What is the proper way to read a json resource file so I can rehydrate it?

TIA

GPGVM
  • 5,515
  • 10
  • 56
  • 97
  • Are you sure the json was UTF8 encoded? Can you provide us with the first 8 bytes in `contents`? –  Oct 08 '15 at 15:35
  • sure can in a few minutes... – GPGVM Oct 08 '15 at 15:57
  • 1
    Is there a [BOM](https://en.wikipedia.org/wiki/Byte_order_mark) at the beginning? If so it's a duplicate of [json.net deserialization throwing exception when json is loaded from project resource](https://stackoverflow.com/questions/32881804/json-net-deserialization-throwing-exception-when-json-is-loaded-from-project-res) – dbc Oct 08 '15 at 17:25

0 Answers0