1

there is a json file like this, without bom, use gbk code set. The boost::property_tree can parse it successfully in the majority.

try {
    boost::property_tree::read_json(filename, tree);
}
catch (exception &e) {
    cerr << e.what() << endl;
}

However, if the file has chinese character"ċކ"(c0fa)or"çıž"(c040), the property_tree will throw exception"invalid code sequence"

Li Da
  • 41
  • 4

1 Answers1

0

You could try to use the overload that takes a stream and imbue a proper locale before hand:

#include <fstream>
#include <iostream>
#include <boost/locale.hpp>

Where you use Boost Locale to generate a locale e.g., on POSIX:

boost::locale::generator gen;
auto CN = gen.generate("zh_CN.GBK");

And then imbue that:

std::ifstream ifs(filename, std::ios::binary);
ifs.imbue(CN);

boost::property_tree::ptree pt;
read_json(ifs, pt); 
sehe
  • 374,641
  • 47
  • 450
  • 633
  • Thank you for your help! But the read_json throw exception "invalid code sequence" as before when the json file has chinese character"ċކ"(c0fa) – Li Da Aug 01 '17 at 02:24