Krotos Modules 3
Loading...
Searching...
No Matches
JsonFile.cpp
Go to the documentation of this file.
1#include "JsonFile.h"
2
3namespace krotos
4{
5 JsonFile::JsonFile(File file, const Identifier& defaultProperty, const String& defaultFormat)
6 : m_jsonFile(file), m_defaultProperty(defaultProperty), m_defaultFormat(defaultFormat)
7 {
8 try
9 {
10 initialise();
11 }
12 catch (std::exception& ex)
13 {
14#if UNIT_TEST
15 // Don't print to std out in unit tests
16 static std::once_flag flag;
17 std::call_once(flag, [ex] {
18 std::cout << "JsonFile Error: " << ex.what() << "\n" << std::endl;
19 std::cout
20 << "\t UnitTest detected. Silencing additional JsonFile exception logging to prevent nuisance "
21 "log spamming"
22 << std::endl;
23 });
24#else
25 // Uh oh, something went wrong. Disable saving. Output error msg to the log.
26 std::cout << "JsonFile Error: " << ex.what() << "\n";
27#endif
28 try
29 {
30 const bool deleteSuccess = m_jsonFile.deleteFile();
31
32 if (deleteSuccess)
33 {
35 }
36 else
37 {
38 throw JsonException("Failed to delete file: " + m_jsonFile.getFullPathName(),
39 "On using fallback default JSON format.");
40 }
41 }
42 catch (std::exception& ex)
43 {
44#if UNIT_TEST
45 // Don't print to std out in unit tests
46 static std::once_flag flag;
47 std::call_once(flag, [ex] {
48 std::cout << "JsonFile Error: " << ex.what() << "\n" << std::endl;
49 std::cout << "\t UnitTest detected. Silencing additional JsonFile exception logging to prevent "
50 "nuisance log spamming"
51 << std::endl;
52 });
53#else
54 // Uh oh, something went wrong. Disable saving. Output error msg to the log.
55 std::cout << "JsonFile Error: " << ex.what() << "\n";
56#endif
57 }
58 }
59 }
60
62 {
63 // You must specify a file path!
64 jassert(m_jsonFile.getFullPathName().isNotEmpty());
65
66 if (m_jsonFile.existsAsFile())
67 {
68 m_data = JSON::parse(m_jsonFile);
69
70 if (!m_data.hasProperty(m_defaultProperty))
71 {
72 throw JsonException("File is invalid or empty and could not be initialised",
73 "On parsing the JSON file.");
74 }
75 }
76 else
77 {
79 }
80 }
81
83 {
84 // Default format must be valid json
85 jassert(!JSON::fromString(m_defaultFormat).isVoid());
86
87 m_data = JSON::fromString(m_defaultFormat);
88
89 save();
90 }
91
93 {
94 TemporaryFile tempFile{m_jsonFile, TemporaryFile::OptionFlags::useHiddenFile};
95
96 if (auto fileOutputStream = std::unique_ptr<FileOutputStream>(tempFile.getFile().createOutputStream()))
97 {
98 // Erase the contents of the file
99 fileOutputStream->setPosition(0);
100 fileOutputStream->truncate();
101
102 // Write registry to file
103 JSON::writeToStream(*fileOutputStream, m_data);
104
105 // Finished writing
106 fileOutputStream.reset();
107
108 const bool success = tempFile.overwriteTargetFileWithTemporary();
109
110 // File overwriting should be successful.
111 if (!success)
112 {
113 throw JsonException("Failed to overwrite " + m_jsonFile.getFullPathName().toStdString() +
114 "\t with temporary file.",
115 "When saving JSON file");
116 }
117 }
118 else
119 {
120 auto result = m_jsonFile.create();
121 if (result.failed())
122 {
123 throw JsonException("Failed to save file to " + m_jsonFile.getFullPathName().toStdString() +
124 "\t Reason: " + result.getErrorMessage().toStdString(),
125 "When creating JSON file");
126 }
127 else
128 {
129 saveInternal();
130 }
131 }
132 }
133
135 {
136 try
137 {
138 saveInternal();
139 }
140 catch (std::exception& ex)
141 {
142#if UNIT_TEST
143 // Don't print to std out in unit tests
144 static std::once_flag flag;
145 std::call_once(flag, [ex] {
146 std::cout << "JsonFile Error: " << ex.what() << "\n" << std::endl;
147 std::cout
148 << "\t UnitTest detected. Silencing additional JsonFile exception logging to prevent nuisance "
149 "log spamming"
150 << std::endl;
151 });
152#else
153 // Uh oh, something went wrong. Disable saving. Output error msg to the log.
154 std::cout << "JsonFile Error: " << ex.what() << "\n";
155#endif
156 }
157 }
158
159 JsonException::JsonException(const juce::String& whatHappened, const juce::String& whenDidItHappen)
160 : std::runtime_error(whatHappened.getCharPointer())
161 {
162 this->m_whenDidItHappen = whenDidItHappen;
163 }
164
165 char const* JsonException::what() const noexcept { return runtime_error::what(); }
166
167 String JsonException::when() const { return m_whenDidItHappen; };
168} // namespace krotos
An exception for the JSON file class.
Definition JsonFile.h:79
JsonException(const String &whatHappened, const String &whenDidItHappen)
Definition JsonFile.cpp:159
char const * what() const noexcept override
Definition JsonFile.cpp:165
String m_whenDidItHappen
Definition JsonFile.h:92
String when() const
Get the "when" status of the exception. This is the context behind the "what" action.
Definition JsonFile.cpp:167
void initialise()
Parse the file if it exists, or create it if it doesn't. Data is parsed to m_data;.
Definition JsonFile.cpp:61
void saveInternal()
Save any changes made to m_data back to the Json file.
Definition JsonFile.cpp:92
JsonFile(File jsonFile, const Identifier &defaultProperty, const String &defaultFormat)
Simple helper class for handling json files. It will attempt to create a json file with the given def...
Definition JsonFile.cpp:5
void createJsonFromDefaultFormat()
Generate the JSON file using the default format - used in cases where JSON fails to load.
Definition JsonFile.cpp:82
void save()
Call save internal.
Definition JsonFile.cpp:134
Identifier m_defaultProperty
Definition JsonFile.h:68
var m_data
The parsed json data.
Definition JsonFile.h:55
File m_jsonFile
The json file.
Definition JsonFile.h:60
String m_defaultFormat
Definition JsonFile.h:69
Definition AirAbsorptionFilter.cpp:2