Skip to content

Instantly share code, notes, and snippets.

@jonwis
Last active July 1, 2023 23:20
Show Gist options
  • Save jonwis/f0985179d50501e36a0e32bac79a7c79 to your computer and use it in GitHub Desktop.
Save jonwis/f0985179d50501e36a0e32bac79a7c79 to your computer and use it in GitHub Desktop.
UTF-8 Encoded std::wifstream support
#include <iostream>
#include <ifstream>
#include <string>
// 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;
}
// Use notepad to create a file called "test-app.txt", and save it with the "UTF-8 with BOM" encoding
void do_test()
{
auto stream = open_utf8_with_maybe_bom(std::filesystem::path("test-app.txt"));
std::wstring t;
while (std::getline(stream, t))
{
std::wcout << L"Line " << t << L" " << t.length() << std::endl;
}
}
// Take the utf8wifstream.cpp, and add:
#include <sstream>
#include <winrt/Windows.Data.Json.h>
void read_json_file()
{
auto stream = open_utf8_with_maybe_bom("test-app.json");
std::wstringstream content;
content << stream.rdbuf();
auto obj = winrt::Windows::Data::Json::JsonObject::Parse(content.str());
std::wcout << obj.ToString().c_str() << std::endl;
}
@jonwis
Copy link
Author

jonwis commented Jul 1, 2023

I suggest not using the Windows.Data.Json types - there are many other C++ JSON processing libraries out there that are much much better and easier to use.

@jonwis
Copy link
Author

jonwis commented Jul 1, 2023

Oh and because I love cryptic one-liners:

    auto obj = winrt::Windows::Data::Json::JsonObject::Parse((std::wstringstream{} << stream.rdbuf()).str());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment