-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonparser.h
More file actions
37 lines (35 loc) · 1.1 KB
/
Copy pathjsonparser.h
File metadata and controls
37 lines (35 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#pragma once
#include "json.hpp"
#include <string>
#include <fstream>
class JsonParser{
public:
bool load(const std::string& path){
std::ifstream file(path);
if(!file.is_open()) return false;
data = nlohmann::json::parse(file);
return true;
}
std::string get_string(const std::string& key, const std::string& default_val = ""){
if(data.contains(key)) return data[key].get<std::string>();
return default_val;
}
int get_int(const std::string& key, int default_val = 0){
if(data.contains(key)) return data[key].get<int>();
return default_val;
}
float get_float(const std::string& key, float default_val = 0.0){
if(data.contains(key)) return data[key].get<float>();
return default_val;
}
bool get_bool(const std::string& key, bool default_val = false){
if(data.contains(key)) return data[key].get<bool>();
return default_val;
}
nlohmann::json get_data(const std::string& key){
if(data.contains(key)) return data[key];
return nullptr;
}
private:
nlohmann::json data;
};