Skip to content

Instantly share code, notes, and snippets.

@jonwis
Created September 10, 2023 17:01
Show Gist options
  • Save jonwis/834cf45b03d1d500a66ae9748cd6f8f1 to your computer and use it in GitHub Desktop.
Save jonwis/834cf45b03d1d500a66ae9748cd6f8f1 to your computer and use it in GitHub Desktop.
Read UTF8 stream file into a Windows.Data.Json.JsonObject isntance
// Opens a file by path name. Assumes the file is UTF-8 encoded. If the file
// has a BOM, it'll be skipped so the first access to the file is positioned
// correctly.
std::wifstream open_utf8_with_maybe_bom(std::filesystem::path const& path)
{
std::wifstream ss;
ss.imbue(std::locale(".utf-8"));
ss.open(path);
if (ss.good() && (ss.peek() == 0xFEFF))
(void)ss.get();
return ss;
}
void read_json_file(std::filesystem::path const& path)
{
auto stream = open_utf8_with_maybe_bom(path);
auto obj = winrt::Windows::Data::Json::JsonObject::Parse((std::wstringstream{} << stream.rdbuf()).str());
std::wcout << obj.ToString().c_str() << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment