Engine
Frameworkcreatedbymeusableforthecreationofsimplegames.CurrentlysupportsOpenGL(Verysimple)andVulkan.
JSONHelper.cpp
Go to the documentation of this file.
1 #include "JSONHelper.hpp"
2 
3 #include "../FileSystem/file_system.hpp"
4 #include "../engine.hpp"
5 #include "rapidjson/prettywriter.h"
6 #include <iostream>
7 
8 namespace Common
9 {
10 rapidjson::Document JsonHelper::OpenJSON(std::string const& filePath)
11 {
12  FileSystems::FileSystem& fs = Engine::GetFileSystem();
13  FileSystems::File& jsonFile = fs.LoadFile(filePath);
14 
15  std::vector<u8> data(static_cast<size_t>(jsonFile.Size()) + 1);
16  jsonFile.ReadData(data, jsonFile.Size());
17 
18  rapidjson::StringStream stream(reinterpret_cast<char const *const>(data.data()));
19  rapidjson::Document document;
20  document.ParseStream(stream);
21 
22  jsonFile.Close();
23 
24  return document;
25 }
26 
27 rapidjson::Document JsonHelper::WriteToJson(std::string const filePath, rapidjson::Document& document,
28  bool destroyOldJson)
29 {
30  rapidjson::Document oldDocument = OpenJSON(filePath);
31 
32  if(destroyOldJson)
33  {
34  oldDocument.RemoveAllMembers();
35  }
36 
37  MergeDocuments(oldDocument, document);
38 
39  FileSystems::FileSystem& fs = Engine::GetFileSystem();
40  FileSystems::File& jsonFile = fs.LoadFile(filePath, FileSystems::File::OpenMode::write);
41 
42  rapidjson::StringBuffer buffer;
43  rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
44  oldDocument.Accept(writer);
45 
46  jsonFile.WriteString(buffer.GetString());
47 
48  return oldDocument;
49 }
50 
51 void JsonHelper::MergeDocuments(rapidjson::Document& originalDocument,
52  rapidjson::Document& documentToAdd, bool overwriteValues)
53 {
54  rapidjson::Document::AllocatorType& allocator = originalDocument.GetAllocator();
55 
56  if(!originalDocument.ObjectEmpty())
57  {
58  for(rapidjson::Value::MemberIterator it = originalDocument.MemberBegin(), end = originalDocument.MemberEnd(); it != end; ++it)
59  {
60  rapidjson::Value first(it->name.GetString(), allocator);
61  rapidjson::Value second;
62  second.CopyFrom(it->value, allocator);
63 
64  if(overwriteValues)
65  {
66  if(!documentToAdd[first].IsNull())
67  {
68  second.CopyFrom(documentToAdd[first], allocator);
69  }
70  }
71 
72  documentToAdd.EraseMember(first);
73 
74  originalDocument[first].Swap(second);
75  }
76  }
77 
78  for(rapidjson::Document::MemberIterator it = documentToAdd.MemberBegin(), end = documentToAdd.MemberEnd(); it != end; ++it)
79  {
80  originalDocument.AddMember(it->name, it->value, allocator);
81  }
82 }
83 } // namespace Common
IMGUI_API void Value(const char *prefix, bool b)
Definition: imgui.cpp:10418
static void MergeDocuments(rapidjson::Document &originalDocument, rapidjson::Document &documentToAdd, bool overwriteValues=true)
Definition: JSONHelper.cpp:51
static rapidjson::Document OpenJSON(std::string const &filePath)
Enter a full path towards the json folder location, and it&#39;ll return you a document.
Definition: JSONHelper.cpp:10
static rapidjson::Document WriteToJson(std::string const filePath, rapidjson::Document &document, bool destroyOldJson=false)
Definition: JSONHelper.cpp:27