1 class JsonFileLoader<
Class T>
5 static bool LoadFile(
string filename, out T data, out
string errorMessage)
12 errorMessage =
string.Format(
"Cannot open file \"%1\" for reading", filename);
18 while (
FGets(handle, lineContent) >= 0)
20 fileContent += lineContent;
29 if (!m_Serializer.ReadFromString(data, fileContent, error))
31 errorMessage =
string.Format(
"Cannot load data from \"%1\":\n%2", filename, error);
39 errorMessage =
string.Format(
"File \"%1\" does not exist, check the provided path", filename, error);
44 static bool SaveFile(
string filename, T data, out
string errorMessage)
52 if (!MakeData(data, fileContent, makeDataError,
true))
54 errorMessage =
string.Format(
"Cannot save data to file \"%1\", %2", filename, makeDataError);
61 errorMessage =
string.Format(
"Cannot open file \"%1\" for writing", filename);
65 FPrint(handle, fileContent);
71 static bool LoadData(
string string_data, out T data, out
string errorMessage)
78 if (!m_Serializer.ReadFromString(data, string_data, error))
80 errorMessage =
string.Format(
"Cannot load provided JSON data (deserialization error) - check syntax: %1", error);
87 static bool MakeData(T inputData, out
string outputData, out
string errorMessage,
bool prettyPrint =
true)
92 if (!m_Serializer.WriteToString(inputData, prettyPrint, outputData))
94 errorMessage =
"Cannot create JSON from provided data (serialization error)";
107 static void JsonLoadFile(
string filename, out T data)
120 while (
FGets(handle, line_content) >= 0)
122 file_content += line_content;
130 if (!m_Serializer.ReadFromString(data, file_content, error))
131 ErrorEx(
string.Format(
"Cannot load data from \"%1\":\n%2", filename, error));
136 static void JsonSaveFile(
string filename, T data)
142 m_Serializer.WriteToString(data,
true, file_content);
148 FPrint(handle, file_content);
153 static void JsonLoadData(
string string_data, out T data)
159 if (!m_Serializer.ReadFromString(data, string_data, error))
160 ErrorEx(
string.Format(
"Cannot load data %1", error));
164 static string JsonMakeData(T data)
170 m_Serializer.WriteToString(data,
true, string_data);