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)