-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathjsonentityloader.cpp
More file actions
72 lines (54 loc) · 2.08 KB
/
jsonentityloader.cpp
File metadata and controls
72 lines (54 loc) · 2.08 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <fea/entity/jsonentityloader.hpp>
#include <fstream>
#include <json/reader.h>
#include <json/value.h>
namespace fea
{
std::unordered_map<std::string, std::string> JsonEntityLoader::jsonObjToStringMap(const Json::Value& obj)
{
std::unordered_map<std::string, std::string> result;
for(const std::string& attribute : obj.getMemberNames())
result.emplace(attribute, obj[attribute].asString());
return result;
}
std::vector<std::pair<std::string, EntityTemplate>> JsonEntityLoader::loadEntityTemplates(const std::string& path)
{
std::ifstream file(path);
if(!file)
throw FileNotFoundException("Error! Entity file not found: " + path + '\n');
Json::Value root;
Json::Reader reader;
reader.parse(file, root, false);
file.close();
std::vector<std::pair<std::string, EntityTemplate>> result;
const Json::Value& entities = root["entities"];
if(entities.isArray())
{
for(const Json::Value& entity : entities)
{
EntityTemplate entityTemplate;
std::string entityName = entity["name"].asString();
const Json::Value& parents = entity["inherits"];
if(parents.isArray())
{
for(const Json::Value& parent : parents)
entityTemplate.mInherits.push_back(parent.asString());
}
entityTemplate.mAttributes = jsonObjToStringMap(entity["attributes"]);
result.emplace_back(entityName, entityTemplate);
}
}
return result;
}
std::unordered_map<std::string, std::string> JsonEntityLoader::loadEntityAttributes(const std::string& path)
{
std::ifstream file(path);
if(!file)
throw FileNotFoundException("Error! Entity file not found: " + path + '\n');
Json::Value root;
Json::Reader reader;
reader.parse(file, root, false);
file.close();
return jsonObjToStringMap(root);
}
}