Dayz Explorer  1.24.157551 (v105080)
Dayz Code Explorer by Zeroy
jsonfileloader.c
Go to the documentation of this file.
1 class JsonFileLoader<Class T>
2 {
3  protected static ref JsonSerializer m_Serializer = new JsonSerializer();
4 
5  static bool LoadFile(string filename, out T data, out string errorMessage)
6  {
7  if (FileExist(filename))
8  {
9  FileHandle handle = OpenFile(filename, FileMode.READ);
10  if (handle == 0)
11  {
12  errorMessage = string.Format("Cannot open file \"%1\" for reading", filename);
13  return false;
14  }
15 
16  string fileContent;
17  string lineContent;
18  while (FGets(handle, lineContent) >= 0)
19  {
20  fileContent += lineContent;
21  }
22 
23  CloseFile(handle);
24 
25  if (!m_Serializer)
26  m_Serializer = new JsonSerializer();
27 
28  string error;
29  if (!m_Serializer.ReadFromString(data, fileContent, error))
30  {
31  errorMessage = string.Format("Cannot load data from \"%1\":\n%2", filename, error);
32  return false;
33  }
34 
35  return true;
36  }
37  else
38  {
39  errorMessage = string.Format("File \"%1\" does not exist, check the provided path", filename, error);
40  return false;
41  }
42  }
43 
44  static bool SaveFile(string filename, T data, out string errorMessage)
45  {
46  string fileContent;
47 
48  if (!m_Serializer)
49  m_Serializer = new JsonSerializer();
50 
51  string makeDataError;
52  if (!MakeData(data, fileContent, makeDataError, true))
53  {
54  errorMessage = string.Format("Cannot save data to file \"%1\", %2", filename, makeDataError);
55  return false;
56  }
57 
58  FileHandle handle = OpenFile(filename, FileMode.WRITE);
59  if (handle == 0)
60  {
61  errorMessage = string.Format("Cannot open file \"%1\" for writing", filename);
62  return false;
63  }
64 
65  FPrint(handle, fileContent);
66  CloseFile(handle);
67 
68  return true;
69  }
70 
71  static bool LoadData(string string_data, out T data, out string errorMessage)
72  {
73  string error;
74 
75  if (!m_Serializer)
76  m_Serializer = new JsonSerializer();
77 
78  if (!m_Serializer.ReadFromString(data, string_data, error))
79  {
80  errorMessage = string.Format("Cannot load provided JSON data (deserialization error) - check syntax: %1", error);
81  return false;
82  }
83 
84  return true;
85  }
86 
87  static bool MakeData(T inputData, out string outputData, out string errorMessage, bool prettyPrint = true)
88  {
89  if (!m_Serializer)
90  m_Serializer = new JsonSerializer();
91 
92  if (!m_Serializer.WriteToString(inputData, prettyPrint, outputData))
93  {
94  errorMessage = "Cannot create JSON from provided data (serialization error)";
95  return false;
96  }
97 
98  return true;
99  }
100 
103 
105 
107  static void JsonLoadFile(string filename, out T data)
108  {
109 
110  if (FileExist(filename))
111  {
112  string file_content;
113  string line_content;
114  string error;
115 
116  FileHandle handle = OpenFile(filename, FileMode.READ);
117  if (handle == 0)
118  return;
119 
120  while (FGets(handle, line_content) >= 0)
121  {
122  file_content += line_content;
123  }
124 
125  CloseFile(handle);
126 
127  if (!m_Serializer)
128  m_Serializer = new JsonSerializer();
129 
130  if (!m_Serializer.ReadFromString(data, file_content, error))
131  ErrorEx(string.Format("Cannot load data from \"%1\":\n%2", filename, error));
132  }
133  }
134 
136  static void JsonSaveFile(string filename, T data)
137  {
138  string file_content;
139  if (!m_Serializer)
140  m_Serializer = new JsonSerializer();
141 
142  m_Serializer.WriteToString(data, true, file_content);
143 
144  FileHandle handle = OpenFile(filename, FileMode.WRITE);
145  if (handle == 0)
146  return;
147 
148  FPrint(handle, file_content);
149  CloseFile(handle);
150  }
151 
153  static void JsonLoadData(string string_data, out T data)
154  {
155  string error;
156  if (!m_Serializer)
157  m_Serializer = new JsonSerializer();
158 
159  if (!m_Serializer.ReadFromString(data, string_data, error))
160  ErrorEx(string.Format("Cannot load data %1", error));
161  }
162 
164  static string JsonMakeData(T data)
165  {
166  string string_data;
167  if (!m_Serializer)
168  m_Serializer = new JsonSerializer();
169 
170  m_Serializer.WriteToString(data, true, string_data);
171  return string_data;
172  }
173 
174  //#endif
175 }
CloseFile
proto void CloseFile(FileHandle file)
Close the File.
OpenFile
proto FileHandle OpenFile(string name, FileMode mode)
Opens File.
FPrint
proto void FPrint(FileHandle file, void var)
Write to file.
JsonSerializer
Class for sending RPC over network.
Definition: gameplay.c:49
FileExist
proto bool FileExist(string name)
Check existence of file.
FileMode
FileMode
Definition: ensystem.c:382
ErrorEx
enum ShapeType ErrorEx
FGets
proto int FGets(FileHandle file, string var)
Get line from file, every next call of this function returns next line.
FileHandle
int[] FileHandle
Definition: ensystem.c:390
Class
Super root of all classes in Enforce script.
Definition: enscript.c:10