Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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. #255

Open
Marangal opened this issue Jun 14, 2022 · 1 comment

Comments

@Marangal
Copy link

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!

@Marangal Marangal changed the title on json file add transform creates utf-8-BOM encoding files while source file is utf-8 (utf-8-bom not supported) 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. Jun 14, 2022
@Marangal
Copy link
Author

Marangal commented Nov 7, 2023

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;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant