JsonKit.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. // JsonKit v0.5 - A simple but flexible Json library in a single .cs file.
  2. //
  3. // Copyright (C) 2014 Topten Software (contact@toptensoftware.com) All rights reserved.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this product
  6. // except in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed under the
  11. // License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  12. // either express or implied. See the License for the specific language governing permissions
  13. // and limitations under the License.
  14. using System;
  15. using System.IO;
  16. namespace Topten.JsonKit
  17. {
  18. // API
  19. public static class Json
  20. {
  21. static Json()
  22. {
  23. WriteWhitespaceDefault = true;
  24. StrictParserDefault = false;
  25. Json.SetFormatterResolver(Emit.MakeFormatter);
  26. Json.SetParserResolver(Emit.MakeParser);
  27. Json.SetIntoParserResolver(Emit.MakeIntoParser);
  28. }
  29. // Pretty format default
  30. public static bool WriteWhitespaceDefault
  31. {
  32. get;
  33. set;
  34. }
  35. // Strict parser
  36. public static bool StrictParserDefault
  37. {
  38. get;
  39. set;
  40. }
  41. // Write an object to a text writer
  42. public static void Write(TextWriter w, object o, JsonOptions options = JsonOptions.None)
  43. {
  44. var writer = new JsonWriter(w, ResolveOptions(options));
  45. writer.WriteValue(o);
  46. }
  47. public static bool SavePreviousVersions
  48. {
  49. get;
  50. set;
  51. }
  52. // Write a file atomically by writing to a temp file and then renaming it - prevents corrupted files if crash
  53. // in middle of writing file.
  54. public static void WriteFileAtomic(string filename, object o, JsonOptions options = JsonOptions.None, string backupFilename = null)
  55. {
  56. var tempName = filename + ".tmp";
  57. try
  58. {
  59. // Write the temp file
  60. WriteFile(tempName, o, (options | JsonOptions.Flush));
  61. if (System.IO.File.Exists(filename))
  62. {
  63. bool savePreviousVersion = false;
  64. if ((options & JsonOptions.AutoSavePreviousVersion)!=0)
  65. {
  66. savePreviousVersion = SavePreviousVersions;
  67. }
  68. else if ((options & JsonOptions.SavePreviousVersion)!=0)
  69. {
  70. savePreviousVersion = true;
  71. }
  72. // Work out backup filename
  73. if (savePreviousVersion)
  74. {
  75. // Make sure have a backup filename
  76. if (backupFilename == null)
  77. {
  78. backupFilename = filename + ".previous";
  79. }
  80. }
  81. else
  82. {
  83. // No backup
  84. backupFilename = null;
  85. }
  86. // Replace it
  87. int retry = 0;
  88. while (true)
  89. {
  90. try
  91. {
  92. File.Replace(tempName, filename, backupFilename);
  93. break;
  94. }
  95. catch (System.IO.IOException x)
  96. {
  97. retry++;
  98. if (retry >= 5)
  99. {
  100. throw new System.IO.IOException(string.Format("Failed to replace temp file {0} with {1} and backup {2}, reason {3}", tempName, filename, backupFilename, x.Message), x);
  101. }
  102. System.Threading.Thread.Sleep(2000);
  103. }
  104. }
  105. }
  106. else
  107. {
  108. // Rename it
  109. File.Move(tempName, filename);
  110. }
  111. }
  112. catch
  113. {
  114. Utils.DeleteFile(tempName);
  115. throw;
  116. }
  117. }
  118. // Write an object to a file
  119. public static void WriteFile(string filename, object o, JsonOptions options = JsonOptions.None)
  120. {
  121. using (var w = new StreamWriter(filename))
  122. {
  123. Write(w, o, options);
  124. if ((options & JsonOptions.Flush) != 0)
  125. {
  126. w.Flush();
  127. w.BaseStream.Flush();
  128. }
  129. }
  130. }
  131. // Format an object as a json string
  132. public static string Format(object o, JsonOptions options = JsonOptions.None)
  133. {
  134. var sw = new StringWriter();
  135. var writer = new JsonWriter(sw, ResolveOptions(options));
  136. writer.WriteValue(o);
  137. return sw.ToString();
  138. }
  139. // Parse an object of specified type from a text reader
  140. public static object Parse(TextReader r, Type type, JsonOptions options = JsonOptions.None)
  141. {
  142. JsonReader reader = null;
  143. try
  144. {
  145. reader = new JsonReader(r, ResolveOptions(options));
  146. var retv = reader.Parse(type);
  147. reader.CheckEOF();
  148. return retv;
  149. }
  150. catch (Exception x)
  151. {
  152. var loc = reader == null ? new LineOffset() : reader.CurrentTokenPosition;
  153. Console.WriteLine("Exception thrown while parsing JSON at {0}, context:{1}\n{2}", loc, reader?.Context, x.ToString());
  154. throw new JsonParseException(x, reader?.Context, loc);
  155. }
  156. }
  157. // Parse an object of specified type from a text reader
  158. public static T Parse<T>(TextReader r, JsonOptions options = JsonOptions.None)
  159. {
  160. return (T)Parse(r, typeof(T), options);
  161. }
  162. // Parse from text reader into an already instantied object
  163. public static void ParseInto(TextReader r, Object into, JsonOptions options = JsonOptions.None)
  164. {
  165. if (into == null)
  166. throw new NullReferenceException();
  167. if (into.GetType().IsValueType)
  168. throw new InvalidOperationException("Can't ParseInto a value type");
  169. JsonReader reader = null;
  170. try
  171. {
  172. reader = new JsonReader(r, ResolveOptions(options));
  173. reader.ParseInto(into);
  174. reader.CheckEOF();
  175. }
  176. catch (Exception x)
  177. {
  178. var loc = reader == null ? new LineOffset() : reader.CurrentTokenPosition;
  179. Console.WriteLine("Exception thrown while parsing JSON at {0}, context:{1}\n{2}", loc, reader.Context, x.ToString());
  180. throw new JsonParseException(x,reader.Context,loc);
  181. }
  182. }
  183. // Parse an object of specified type from a file
  184. public static object ParseFile(string filename, Type type, JsonOptions options = JsonOptions.None)
  185. {
  186. using (var r = new StreamReader(filename))
  187. {
  188. return Parse(r, type, options);
  189. }
  190. }
  191. // Parse an object of specified type from a file
  192. public static T ParseFile<T>(string filename, JsonOptions options = JsonOptions.None)
  193. {
  194. using (var r = new StreamReader(filename))
  195. {
  196. return Parse<T>(r, options);
  197. }
  198. }
  199. // Parse from file into an already instantied object
  200. public static void ParseFileInto(string filename, Object into, JsonOptions options = JsonOptions.None)
  201. {
  202. using (var r = new StreamReader(filename))
  203. {
  204. ParseInto(r, into, options);
  205. }
  206. }
  207. // Parse an object from a string
  208. public static object Parse(string data, Type type, JsonOptions options = JsonOptions.None)
  209. {
  210. return Parse(new StringReader(data), type, options);
  211. }
  212. // Parse an object from a string
  213. public static T Parse<T>(string data, JsonOptions options = JsonOptions.None)
  214. {
  215. return (T)Parse<T>(new StringReader(data), options);
  216. }
  217. // Parse from string into an already instantiated object
  218. public static void ParseInto(string data, Object into, JsonOptions options = JsonOptions.None)
  219. {
  220. ParseInto(new StringReader(data), into, options);
  221. }
  222. // Create a clone of an object
  223. public static T Clone<T>(T source)
  224. {
  225. return (T)Reparse(source.GetType(), source);
  226. }
  227. // Create a clone of an object (untyped)
  228. public static object Clone(object source)
  229. {
  230. return Reparse(source.GetType(), source);
  231. }
  232. // Clone an object into another instance
  233. public static void CloneInto(object dest, object source)
  234. {
  235. ReparseInto(dest, source);
  236. }
  237. // Reparse an object by writing to a stream and re-reading (possibly
  238. // as a different type).
  239. public static object Reparse(Type type, object source)
  240. {
  241. if (source == null)
  242. return null;
  243. var ms = new MemoryStream();
  244. try
  245. {
  246. // Write
  247. var w = new StreamWriter(ms);
  248. Json.Write(w, source);
  249. w.Flush();
  250. // Read
  251. ms.Seek(0, SeekOrigin.Begin);
  252. var r = new StreamReader(ms);
  253. return Json.Parse(r, type);
  254. }
  255. finally
  256. {
  257. ms.Dispose();
  258. }
  259. }
  260. // Typed version of above
  261. public static T Reparse<T>(object source)
  262. {
  263. return (T)Reparse(typeof(T), source);
  264. }
  265. // Reparse one object into another object
  266. public static void ReparseInto(object dest, object source)
  267. {
  268. var ms = new MemoryStream();
  269. try
  270. {
  271. // Write
  272. var w = new StreamWriter(ms);
  273. Json.Write(w, source);
  274. w.Flush();
  275. // Read
  276. ms.Seek(0, SeekOrigin.Begin);
  277. var r = new StreamReader(ms);
  278. Json.ParseInto(r, dest);
  279. }
  280. finally
  281. {
  282. ms.Dispose();
  283. }
  284. }
  285. // Register a callback that can format a value of a particular type into json
  286. public static void RegisterFormatter(Type type, Action<IJsonWriter, object> formatter)
  287. {
  288. JsonWriter._formatters[type] = formatter;
  289. }
  290. // Typed version of above
  291. public static void RegisterFormatter<T>(Action<IJsonWriter, T> formatter)
  292. {
  293. RegisterFormatter(typeof(T), (w, o) => formatter(w, (T)o));
  294. }
  295. // Register a parser for a specified type
  296. public static void RegisterParser(Type type, Func<IJsonReader, Type, object> parser)
  297. {
  298. JsonReader._parsers.Set(type, parser);
  299. }
  300. // Register a typed parser
  301. public static void RegisterParser<T>(Func<IJsonReader, Type, T> parser)
  302. {
  303. RegisterParser(typeof(T), (r, t) => parser(r, t));
  304. }
  305. // Simpler version for simple types
  306. public static void RegisterParser(Type type, Func<object, object> parser)
  307. {
  308. RegisterParser(type, (r, t) => r.ReadLiteral(parser));
  309. }
  310. // Simpler and typesafe parser for simple types
  311. public static void RegisterParser<T>(Func<object, T> parser)
  312. {
  313. RegisterParser(typeof(T), literal => parser(literal));
  314. }
  315. // Register an into parser
  316. public static void RegisterIntoParser(Type type, Action<IJsonReader, object> parser)
  317. {
  318. JsonReader._intoParsers.Set(type, parser);
  319. }
  320. // Register an into parser
  321. public static void RegisterIntoParser<T>(Action<IJsonReader, object> parser)
  322. {
  323. RegisterIntoParser(typeof(T), parser);
  324. }
  325. // Register a factory for instantiating objects (typically abstract classes)
  326. // Callback will be invoked for each key in the dictionary until it returns an object
  327. // instance and which point it will switch to serialization using reflection
  328. public static void RegisterTypeFactory(Type type, Func<IJsonReader, string, object> factory)
  329. {
  330. JsonReader._typeFactories.Set(type, factory);
  331. }
  332. // Register a callback to provide a formatter for a newly encountered type
  333. public static void SetFormatterResolver(Func<Type, Action<IJsonWriter, object>> resolver)
  334. {
  335. JsonWriter._formatterResolver = resolver;
  336. }
  337. // Register a callback to provide a parser for a newly encountered value type
  338. public static void SetParserResolver(Func<Type, Func<IJsonReader, Type, object>> resolver)
  339. {
  340. JsonReader._parserResolver = resolver;
  341. }
  342. // Register a callback to provide a parser for a newly encountered reference type
  343. public static void SetIntoParserResolver(Func<Type, Action<IJsonReader, object>> resolver)
  344. {
  345. JsonReader._intoParserResolver = resolver;
  346. }
  347. // Resolve passed options
  348. static JsonOptions ResolveOptions(JsonOptions options)
  349. {
  350. JsonOptions resolved = JsonOptions.None;
  351. if ((options & (JsonOptions.WriteWhitespace|JsonOptions.DontWriteWhitespace))!=0)
  352. resolved |= options & (JsonOptions.WriteWhitespace | JsonOptions.DontWriteWhitespace);
  353. else
  354. resolved |= WriteWhitespaceDefault ? JsonOptions.WriteWhitespace : JsonOptions.DontWriteWhitespace;
  355. if ((options & (JsonOptions.StrictParser | JsonOptions.NonStrictParser)) != 0)
  356. resolved |= options & (JsonOptions.StrictParser | JsonOptions.NonStrictParser);
  357. else
  358. resolved |= StrictParserDefault ? JsonOptions.StrictParser : JsonOptions.NonStrictParser;
  359. return resolved;
  360. }
  361. }
  362. }