binary json format introduced + specification
This commit is contained in:
+29
-4
@@ -92,16 +92,41 @@ bool files::write_string(fs::path filename, const std::string content) {
|
||||
return true;
|
||||
}
|
||||
|
||||
json::JObject* files::read_json(fs::path file) {
|
||||
std::string text = files::read_string(file);
|
||||
bool files::write_json(fs::path filename, const json::JObject* obj, bool nice) {
|
||||
// -- binary json tests
|
||||
//return write_binary_json(fs::path(filename.u8string()+".bin"), obj);
|
||||
return files::write_string(filename, json::stringify(obj, nice, " "));
|
||||
}
|
||||
|
||||
bool files::write_binary_json(fs::path filename, const json::JObject* obj) {
|
||||
std::vector<ubyte> bytes = json::to_binary(obj);
|
||||
return files::write_bytes(filename, (const char*)bytes.data(), bytes.size());
|
||||
}
|
||||
|
||||
json::JObject* files::read_json(fs::path filename) {
|
||||
// binary json tests
|
||||
// fs::path binfile = fs::path(filename.u8string()+".bin");
|
||||
// if (fs::is_regular_file(binfile)){
|
||||
// return read_binary_json(binfile);
|
||||
// }
|
||||
|
||||
std::string text = files::read_string(filename);
|
||||
try {
|
||||
return json::parse(file.string(), text);
|
||||
auto obj = json::parse(filename.string(), text);
|
||||
//write_binary_json(binfile, obj);
|
||||
return obj;
|
||||
} catch (const parsing_error& error) {
|
||||
std::cerr << error.errorLog() << std::endl;
|
||||
throw std::runtime_error("could not to parse "+file.string());
|
||||
throw std::runtime_error("could not to parse "+filename.string());
|
||||
}
|
||||
}
|
||||
|
||||
json::JObject* files::read_binary_json(fs::path file) {
|
||||
size_t size;
|
||||
std::unique_ptr<char[]> bytes (files::read_bytes(file, size));
|
||||
return json::from_binary((const ubyte*)bytes.get(), size);
|
||||
}
|
||||
|
||||
std::vector<std::string> files::read_list(fs::path filename) {
|
||||
std::ifstream file(filename);
|
||||
if (!file) {
|
||||
|
||||
Reference in New Issue
Block a user