File size: 4,037 Bytes
24b81cb |
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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
class JsonFileLoader<Class T>
{
protected static ref JsonSerializer m_Serializer = new JsonSerializer();
static bool LoadFile(string filename, out T data, out string errorMessage)
{
if (FileExist(filename))
{
FileHandle handle = OpenFile(filename, FileMode.READ);
if (handle == 0)
{
errorMessage = string.Format("Cannot open file \"%1\" for reading", filename);
return false;
}
string fileContent;
string lineContent;
while (FGets(handle, lineContent) >= 0)
{
fileContent += lineContent;
}
CloseFile(handle);
if (!m_Serializer)
m_Serializer = new JsonSerializer();
string error;
if (!m_Serializer.ReadFromString(data, fileContent, error))
{
errorMessage = string.Format("Cannot load data from \"%1\":\n%2", filename, error);
return false;
}
return true;
}
else
{
errorMessage = string.Format("File \"%1\" does not exist, check the provided path", filename, error);
return false;
}
}
static bool SaveFile(string filename, T data, out string errorMessage)
{
string fileContent;
if (!m_Serializer)
m_Serializer = new JsonSerializer();
string makeDataError;
if (!MakeData(data, fileContent, makeDataError, true))
{
errorMessage = string.Format("Cannot save data to file \"%1\", %2", filename, makeDataError);
return false;
}
FileHandle handle = OpenFile(filename, FileMode.WRITE);
if (handle == 0)
{
errorMessage = string.Format("Cannot open file \"%1\" for writing", filename);
return false;
}
FPrint(handle, fileContent);
CloseFile(handle);
return true;
}
static bool LoadData(string string_data, out T data, out string errorMessage)
{
string error;
if (!m_Serializer)
m_Serializer = new JsonSerializer();
if (!m_Serializer.ReadFromString(data, string_data, error))
{
errorMessage = string.Format("Cannot load provided JSON data (deserialization error) - check syntax: %1", error);
return false;
}
return true;
}
static bool MakeData(T inputData, out string outputData, out string errorMessage, bool prettyPrint = true)
{
if (!m_Serializer)
m_Serializer = new JsonSerializer();
if (!m_Serializer.WriteToString(inputData, prettyPrint, outputData))
{
errorMessage = "Cannot create JSON from provided data (serialization error)";
return false;
}
return true;
}
//! DEPRECATED
//! ----------
//! #ifndef DIAG_DEVELOPER
//! use JsonFileLoader::LoadFile instead
static void JsonLoadFile(string filename, out T data)
{
if (FileExist(filename))
{
string file_content;
string line_content;
string error;
FileHandle handle = OpenFile(filename, FileMode.READ);
if (handle == 0)
return;
while (FGets(handle, line_content) >= 0)
{
file_content += line_content;
}
CloseFile(handle);
if (!m_Serializer)
m_Serializer = new JsonSerializer();
if (!m_Serializer.ReadFromString(data, file_content, error))
ErrorEx(string.Format("Cannot load data from \"%1\":\n%2", filename, error));
}
}
//! use JsonFileLoader::SaveFile instead
static void JsonSaveFile(string filename, T data)
{
string file_content;
if (!m_Serializer)
m_Serializer = new JsonSerializer();
m_Serializer.WriteToString(data, true, file_content);
FileHandle handle = OpenFile(filename, FileMode.WRITE);
if (handle == 0)
return;
FPrint(handle, file_content);
CloseFile(handle);
}
//!use JsonFileLoader::LoadData instead
static void JsonLoadData(string string_data, out T data)
{
string error;
if (!m_Serializer)
m_Serializer = new JsonSerializer();
if (!m_Serializer.ReadFromString(data, string_data, error))
ErrorEx(string.Format("Cannot load data %1", error));
}
//!use JsonFileLoader::MakeData instead
static string JsonMakeData(T data)
{
string string_data;
if (!m_Serializer)
m_Serializer = new JsonSerializer();
m_Serializer.WriteToString(data, true, string_data);
return string_data;
}
//#endif
}
|