JSON file add transform creates UTF-8-BOM encoding files while source file is UTF-8. This UTF-8-BOM is not supported by most decoders.
Marangal opened this issue · 1 comments
Hi
When I do Add Transform on JSON file that is UTF-8 encoding, it creates environment files that are UTF-8-BOM encoding.
UTF-8-BOM encoding on JSON files is not supported for many application.
When looking at the preview we see also the message: These files have different encodings. Left file: Unicode (UTF-8) without signature. Right file: Unicode (UTF-8) with signature.
After building the project when trying to work with the transformed file with Unicode (UTF-8) with signature.
The error we receive: System.Runtime.Serialization.SerializationException: 'There was an error deserializing. Encountered unexpected character 'ï'.' (Because none supported encodig).
In the JSON spec it says JSON requires UTF-8 support by decoders.
Error thrown by System.Runtime.Serialization.Json, Version=6.0.0.0
var jsonSettings = new DataContractJsonSerializerSettings() { UseSimpleDictionaryFormat = true };
jsonSerializer.ReadObject(filestream)
Thanks for the support or workaround!
My current solution to this issue is by reading the json file first and put it in memory stream instead of file stream and using that memory stream for the jsonSerializer.ReadObject(ms)
string configurationFilePath = "..../appsettings.json");
string jsonData = File.ReadAllText(configurationFilePath);
using (var ms = new MemoryStream(System.Text.Encoding.Default.GetBytes(jsonData)))
{
var jsonSerializer = new DataContractJsonSerializer(typeof(CustomConfiguration), new DataContractJsonSerializerSettings());
var options = jsonSerializer.ReadObject(ms) as CustomConfiguration;
}