You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

297 lines
9.9 KiB

  1. //#define USE_SharpZipLib
  2. /* * * * *
  3. * This is an extension of the SimpleJSON framework to provide methods to
  4. * serialize a JSON object tree into a compact binary format. Optionally the
  5. * binary stream can be compressed with the SharpZipLib when using the define
  6. * "USE_SharpZipLib"
  7. *
  8. * Those methods where originally part of the framework but since it's rarely
  9. * used I've extracted this part into this seperate module file.
  10. *
  11. * You can use the define "SimpleJSON_ExcludeBinary" to selectively disable
  12. * this extension without the need to remove the file from the project.
  13. *
  14. *
  15. * The MIT License (MIT)
  16. *
  17. * Copyright (c) 2012-2017 Markus Göbel (Bunny83)
  18. *
  19. * Permission is hereby granted, free of charge, to any person obtaining a copy
  20. * of this software and associated documentation files (the "Software"), to deal
  21. * in the Software without restriction, including without limitation the rights
  22. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  23. * copies of the Software, and to permit persons to whom the Software is
  24. * furnished to do so, subject to the following conditions:
  25. *
  26. * The above copyright notice and this permission notice shall be included in all
  27. * copies or substantial portions of the Software.
  28. *
  29. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  30. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  31. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  32. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  33. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  34. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  35. * SOFTWARE.
  36. *
  37. * * * * */
  38. using System;
  39. namespace SimpleJSON
  40. {
  41. #if !SimpleJSON_ExcludeBinary
  42. public abstract partial class JSONNode
  43. {
  44. public abstract void SerializeBinary(System.IO.BinaryWriter aWriter);
  45. public void SaveToBinaryStream(System.IO.Stream aData)
  46. {
  47. var W = new System.IO.BinaryWriter(aData);
  48. SerializeBinary(W);
  49. }
  50. #if USE_SharpZipLib
  51. public void SaveToCompressedStream(System.IO.Stream aData)
  52. {
  53. using (var gzipOut = new ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream(aData))
  54. {
  55. gzipOut.IsStreamOwner = false;
  56. SaveToBinaryStream(gzipOut);
  57. gzipOut.Close();
  58. }
  59. }
  60. public void SaveToCompressedFile(string aFileName)
  61. {
  62. System.IO.Directory.CreateDirectory((new System.IO.FileInfo(aFileName)).Directory.FullName);
  63. using(var F = System.IO.File.OpenWrite(aFileName))
  64. {
  65. SaveToCompressedStream(F);
  66. }
  67. }
  68. public string SaveToCompressedBase64()
  69. {
  70. using (var stream = new System.IO.MemoryStream())
  71. {
  72. SaveToCompressedStream(stream);
  73. stream.Position = 0;
  74. return System.Convert.ToBase64String(stream.ToArray());
  75. }
  76. }
  77. #else
  78. public void SaveToCompressedStream(System.IO.Stream aData)
  79. {
  80. throw new Exception("Can't use compressed functions. You need include the SharpZipLib and uncomment the define at the top of SimpleJSON");
  81. }
  82. public void SaveToCompressedFile(string aFileName)
  83. {
  84. throw new Exception("Can't use compressed functions. You need include the SharpZipLib and uncomment the define at the top of SimpleJSON");
  85. }
  86. public string SaveToCompressedBase64()
  87. {
  88. throw new Exception("Can't use compressed functions. You need include the SharpZipLib and uncomment the define at the top of SimpleJSON");
  89. }
  90. #endif
  91. public void SaveToBinaryFile(string aFileName)
  92. {
  93. System.IO.Directory.CreateDirectory((new System.IO.FileInfo(aFileName)).Directory.FullName);
  94. using (var F = System.IO.File.OpenWrite(aFileName))
  95. {
  96. SaveToBinaryStream(F);
  97. }
  98. }
  99. public string SaveToBinaryBase64()
  100. {
  101. using (var stream = new System.IO.MemoryStream())
  102. {
  103. SaveToBinaryStream(stream);
  104. stream.Position = 0;
  105. return System.Convert.ToBase64String(stream.ToArray());
  106. }
  107. }
  108. public static JSONNode DeserializeBinary(System.IO.BinaryReader aReader)
  109. {
  110. JSONNodeType type = (JSONNodeType)aReader.ReadByte();
  111. switch (type)
  112. {
  113. case JSONNodeType.Array:
  114. {
  115. int count = aReader.ReadInt32();
  116. JSONArray tmp = new JSONArray();
  117. for (int i = 0; i < count; i++)
  118. tmp.Add(DeserializeBinary(aReader));
  119. return tmp;
  120. }
  121. case JSONNodeType.Object:
  122. {
  123. int count = aReader.ReadInt32();
  124. JSONObject tmp = new JSONObject();
  125. for (int i = 0; i < count; i++)
  126. {
  127. string key = aReader.ReadString();
  128. var val = DeserializeBinary(aReader);
  129. tmp.Add(key, val);
  130. }
  131. return tmp;
  132. }
  133. case JSONNodeType.String:
  134. {
  135. return new JSONString(aReader.ReadString());
  136. }
  137. case JSONNodeType.Number:
  138. {
  139. return new JSONNumber(aReader.ReadDouble());
  140. }
  141. case JSONNodeType.Boolean:
  142. {
  143. return new JSONBool(aReader.ReadBoolean());
  144. }
  145. case JSONNodeType.NullValue:
  146. {
  147. return JSONNull.CreateOrGet();
  148. }
  149. default:
  150. {
  151. throw new Exception("Error deserializing JSON. Unknown tag: " + type);
  152. }
  153. }
  154. }
  155. #if USE_SharpZipLib
  156. public static JSONNode LoadFromCompressedStream(System.IO.Stream aData)
  157. {
  158. var zin = new ICSharpCode.SharpZipLib.BZip2.BZip2InputStream(aData);
  159. return LoadFromStream(zin);
  160. }
  161. public static JSONNode LoadFromCompressedFile(string aFileName)
  162. {
  163. using(var F = System.IO.File.OpenRead(aFileName))
  164. {
  165. return LoadFromCompressedStream(F);
  166. }
  167. }
  168. public static JSONNode LoadFromCompressedBase64(string aBase64)
  169. {
  170. var tmp = System.Convert.FromBase64String(aBase64);
  171. var stream = new System.IO.MemoryStream(tmp);
  172. stream.Position = 0;
  173. return LoadFromCompressedStream(stream);
  174. }
  175. #else
  176. public static JSONNode LoadFromCompressedFile(string aFileName)
  177. {
  178. throw new Exception("Can't use compressed functions. You need include the SharpZipLib and uncomment the define at the top of SimpleJSON");
  179. }
  180. public static JSONNode LoadFromCompressedStream(System.IO.Stream aData)
  181. {
  182. throw new Exception("Can't use compressed functions. You need include the SharpZipLib and uncomment the define at the top of SimpleJSON");
  183. }
  184. public static JSONNode LoadFromCompressedBase64(string aBase64)
  185. {
  186. throw new Exception("Can't use compressed functions. You need include the SharpZipLib and uncomment the define at the top of SimpleJSON");
  187. }
  188. #endif
  189. public static JSONNode LoadFromBinaryStream(System.IO.Stream aData)
  190. {
  191. using (var R = new System.IO.BinaryReader(aData))
  192. {
  193. return DeserializeBinary(R);
  194. }
  195. }
  196. public static JSONNode LoadFromBinaryFile(string aFileName)
  197. {
  198. using (var F = System.IO.File.OpenRead(aFileName))
  199. {
  200. return LoadFromBinaryStream(F);
  201. }
  202. }
  203. public static JSONNode LoadFromBinaryBase64(string aBase64)
  204. {
  205. var tmp = System.Convert.FromBase64String(aBase64);
  206. var stream = new System.IO.MemoryStream(tmp);
  207. stream.Position = 0;
  208. return LoadFromBinaryStream(stream);
  209. }
  210. }
  211. public partial class JSONArray : JSONNode
  212. {
  213. public override void SerializeBinary(System.IO.BinaryWriter aWriter)
  214. {
  215. aWriter.Write((byte)JSONNodeType.Array);
  216. aWriter.Write(m_List.Count);
  217. for (int i = 0; i < m_List.Count; i++)
  218. {
  219. m_List[i].SerializeBinary(aWriter);
  220. }
  221. }
  222. }
  223. public partial class JSONObject : JSONNode
  224. {
  225. public override void SerializeBinary(System.IO.BinaryWriter aWriter)
  226. {
  227. aWriter.Write((byte)JSONNodeType.Object);
  228. aWriter.Write(m_Dict.Count);
  229. foreach (string K in m_Dict.Keys)
  230. {
  231. aWriter.Write(K);
  232. m_Dict[K].SerializeBinary(aWriter);
  233. }
  234. }
  235. }
  236. public partial class JSONString : JSONNode
  237. {
  238. public override void SerializeBinary(System.IO.BinaryWriter aWriter)
  239. {
  240. aWriter.Write((byte)JSONNodeType.String);
  241. aWriter.Write(m_Data);
  242. }
  243. }
  244. public partial class JSONNumber : JSONNode
  245. {
  246. public override void SerializeBinary(System.IO.BinaryWriter aWriter)
  247. {
  248. aWriter.Write((byte)JSONNodeType.Number);
  249. aWriter.Write(m_Data);
  250. }
  251. }
  252. public partial class JSONBool : JSONNode
  253. {
  254. public override void SerializeBinary(System.IO.BinaryWriter aWriter)
  255. {
  256. aWriter.Write((byte)JSONNodeType.Boolean);
  257. aWriter.Write(m_Data);
  258. }
  259. }
  260. public partial class JSONNull : JSONNode
  261. {
  262. public override void SerializeBinary(System.IO.BinaryWriter aWriter)
  263. {
  264. aWriter.Write((byte)JSONNodeType.NullValue);
  265. }
  266. }
  267. internal partial class JSONLazyCreator : JSONNode
  268. {
  269. public override void SerializeBinary(System.IO.BinaryWriter aWriter)
  270. {
  271. }
  272. }
  273. #endif
  274. }