I have a json file file.json
encoded KOI8-R.
Boost Json only works in UTF-8 encoding, so I'm converting the file from KOI8-R to UTF-8:
boost::property_tree::ptree tree;
std::locale loc = boost::locale::generator().generate(ru_RU.UTF-8);
std::ifstream ifs("file.json", std::ios::binary);
ifs.imbue(loc)
boost::property_tree::read_json(ifs, tree);
However, the file cannot be read .. What am I doing wrong?
UPDATE:
I made up a JSON file "test.txt":
{
"соплодие": "лысеющий",
"обсчитавший": "перегнавший",
"кариозный": "отдёргивающийся",
"суверенен": "носившийся",
"рецидивизм": "поляризуются"
}
And saved it in koi8-r.
I have a code:
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
int main() {
boost::property_tree::ptree pt;
boost::property_tree::read_json("test.txt", pt);
}
Compiled, ran and got the following error:
terminate called after throwing an instance of 'boost::wrapexcept<boost::property_tree::json_parser::json_parser_error>'
what(): test.txt(2): invalid code sequence
Aborted (core dumped)
Then I use boost locale:
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/locale/generator.hpp>
#include <boost/locale/encoding.hpp>
int main() {
std::locale loc = boost::locale::generator().generate("ru_RU.utf8");
std::ifstream ifs("test.txt", std::ios::binary);
ifs.imbue(loc);
boost::property_tree::ptree pt;
boost::property_tree::read_json(ifs, pt);
}
Compiled (g++ main.cpp -lboost_locale
), ran and got the following error:
terminate called after throwing an instance of 'boost::wrapexcept<boost::property_tree::json_parser::json_parser_error>'
what(): <unspecified file>(2): invalid code sequence
Aborted (core dumped)
The JSON spec requires UTF8:
It makes sense for a general purpose library to only support that. See here for more context: JSON character encoding - is UTF-8 well-supported by browsers or should I use numeric escape sequences?
How To Do It Anyways
Maybe with libiconv or libicu, Boost locale supports the latter.
Using Boost Locale/ICU
This requires that your library was built with ICU support, and maybe(?) you have the required locales, which is likely the case already on your system.
It also assumes the source code is in UTF8 encoding, which, again, is likely.
Live On Compiler Explorer
I made up a random JSON:
And saved it in koi8-r as "input.txt":
Now running that program shows: