暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

SimpleJSONBinary.cs 10KB

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