PetaJson.cs 130 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285
  1. // PetaJson 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. // Define PETAJSON_NO_DYNAMIC to disable Expando support
  15. // Define PETAJSON_NO_EMIT to disable Reflection.Emit
  16. // Define PETAJSON_NO_DATACONTRACT to disable support for [DataContract]/[DataMember]
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Linq;
  20. using System.Text;
  21. using System.IO;
  22. using System.Reflection;
  23. using System.Globalization;
  24. using System.Collections;
  25. using System.Threading;
  26. #if !PETAJSON_NO_DYNAMIC
  27. using System.Dynamic;
  28. #endif
  29. #if !PETAJSON_NO_EMIT
  30. using System.Reflection.Emit;
  31. #endif
  32. #if !PETAJSON_NO_DATACONTRACT
  33. using System.Runtime.Serialization;
  34. #endif
  35. namespace PetaJson
  36. {
  37. // Pass to format/write/parse functions to override defaults
  38. [Flags]
  39. public enum JsonOptions
  40. {
  41. None = 0,
  42. WriteWhitespace = 0x00000001,
  43. DontWriteWhitespace = 0x00000002,
  44. StrictParser = 0x00000004,
  45. NonStrictParser = 0x00000008,
  46. }
  47. // API
  48. public static class Json
  49. {
  50. static Json()
  51. {
  52. WriteWhitespaceDefault = true;
  53. StrictParserDefault = false;
  54. #if !PETAJSON_NO_EMIT
  55. Json.SetFormatterResolver(Internal.Emit.MakeFormatter);
  56. Json.SetParserResolver(Internal.Emit.MakeParser);
  57. Json.SetIntoParserResolver(Internal.Emit.MakeIntoParser);
  58. #endif
  59. }
  60. // Pretty format default
  61. public static bool WriteWhitespaceDefault
  62. {
  63. get;
  64. set;
  65. }
  66. // Strict parser
  67. public static bool StrictParserDefault
  68. {
  69. get;
  70. set;
  71. }
  72. // Write an object to a text writer
  73. public static void Write(TextWriter w, object o, JsonOptions options = JsonOptions.None)
  74. {
  75. var writer = new Internal.Writer(w, ResolveOptions(options));
  76. writer.WriteValue(o);
  77. }
  78. // Write an object to a file
  79. public static void WriteFile(string filename, object o, JsonOptions options = JsonOptions.None)
  80. {
  81. using (var w = new StreamWriter(filename))
  82. {
  83. Write(w, o, options);
  84. }
  85. }
  86. // Format an object as a json string
  87. public static string Format(object o, JsonOptions options = JsonOptions.None)
  88. {
  89. var sw = new StringWriter();
  90. var writer = new Internal.Writer(sw, ResolveOptions(options));
  91. writer.WriteValue(o);
  92. return sw.ToString();
  93. }
  94. // Parse an object of specified type from a text reader
  95. public static object Parse(TextReader r, Type type, JsonOptions options = JsonOptions.None)
  96. {
  97. Internal.Reader reader = null;
  98. try
  99. {
  100. reader = new Internal.Reader(r, ResolveOptions(options));
  101. var retv = reader.Parse(type);
  102. reader.CheckEOF();
  103. return retv;
  104. }
  105. catch (Exception x)
  106. {
  107. var loc = reader == null ? new JsonLineOffset() : reader.CurrentTokenPosition;
  108. Console.WriteLine("Exception thrown while parsing JSON at {0}, context:{1}\n{2}", loc, reader.Context, x.ToString());
  109. throw new JsonParseException(x, reader.Context, loc);
  110. }
  111. }
  112. // Parse an object of specified type from a text reader
  113. public static T Parse<T>(TextReader r, JsonOptions options = JsonOptions.None)
  114. {
  115. return (T)Parse(r, typeof(T), options);
  116. }
  117. // Parse from text reader into an already instantied object
  118. public static void ParseInto(TextReader r, Object into, JsonOptions options = JsonOptions.None)
  119. {
  120. if (into == null)
  121. throw new NullReferenceException();
  122. if (into.GetType().IsValueType)
  123. throw new InvalidOperationException("Can't ParseInto a value type");
  124. Internal.Reader reader = null;
  125. try
  126. {
  127. reader = new Internal.Reader(r, ResolveOptions(options));
  128. reader.ParseInto(into);
  129. reader.CheckEOF();
  130. }
  131. catch (Exception x)
  132. {
  133. var loc = reader == null ? new JsonLineOffset() : reader.CurrentTokenPosition;
  134. Console.WriteLine("Exception thrown while parsing JSON at {0}, context:{1}\n{2}", loc, reader.Context, x.ToString());
  135. throw new JsonParseException(x,reader.Context,loc);
  136. }
  137. }
  138. // Parse an object of specified type from a file
  139. public static object ParseFile(string filename, Type type, JsonOptions options = JsonOptions.None)
  140. {
  141. using (var r = new StreamReader(filename))
  142. {
  143. return Parse(r, type, options);
  144. }
  145. }
  146. // Parse an object of specified type from a file
  147. public static T ParseFile<T>(string filename, JsonOptions options = JsonOptions.None)
  148. {
  149. using (var r = new StreamReader(filename))
  150. {
  151. return Parse<T>(r, options);
  152. }
  153. }
  154. // Parse from file into an already instantied object
  155. public static void ParseFileInto(string filename, Object into, JsonOptions options = JsonOptions.None)
  156. {
  157. using (var r = new StreamReader(filename))
  158. {
  159. ParseInto(r, into, options);
  160. }
  161. }
  162. // Parse an object from a string
  163. public static object Parse(string data, Type type, JsonOptions options = JsonOptions.None)
  164. {
  165. return Parse(new StringReader(data), type, options);
  166. }
  167. // Parse an object from a string
  168. public static T Parse<T>(string data, JsonOptions options = JsonOptions.None)
  169. {
  170. return (T)Parse<T>(new StringReader(data), options);
  171. }
  172. // Parse from string into an already instantiated object
  173. public static void ParseInto(string data, Object into, JsonOptions options = JsonOptions.None)
  174. {
  175. ParseInto(new StringReader(data), into, options);
  176. }
  177. // Create a clone of an object
  178. public static T Clone<T>(T source)
  179. {
  180. return (T)Reparse(source.GetType(), source);
  181. }
  182. // Create a clone of an object (untyped)
  183. public static object Clone(object source)
  184. {
  185. return Reparse(source.GetType(), source);
  186. }
  187. // Clone an object into another instance
  188. public static void CloneInto(object dest, object source)
  189. {
  190. ReparseInto(dest, source);
  191. }
  192. // Reparse an object by writing to a stream and re-reading (possibly
  193. // as a different type).
  194. public static object Reparse(Type type, object source)
  195. {
  196. if (source == null)
  197. return null;
  198. var ms = new MemoryStream();
  199. try
  200. {
  201. // Write
  202. var w = new StreamWriter(ms);
  203. Json.Write(w, source);
  204. w.Flush();
  205. // Read
  206. ms.Seek(0, SeekOrigin.Begin);
  207. var r = new StreamReader(ms);
  208. return Json.Parse(r, type);
  209. }
  210. finally
  211. {
  212. ms.Dispose();
  213. }
  214. }
  215. // Typed version of above
  216. public static T Reparse<T>(object source)
  217. {
  218. return (T)Reparse(typeof(T), source);
  219. }
  220. // Reparse one object into another object
  221. public static void ReparseInto(object dest, object source)
  222. {
  223. var ms = new MemoryStream();
  224. try
  225. {
  226. // Write
  227. var w = new StreamWriter(ms);
  228. Json.Write(w, source);
  229. w.Flush();
  230. // Read
  231. ms.Seek(0, SeekOrigin.Begin);
  232. var r = new StreamReader(ms);
  233. Json.ParseInto(r, dest);
  234. }
  235. finally
  236. {
  237. ms.Dispose();
  238. }
  239. }
  240. // Register a callback that can format a value of a particular type into json
  241. public static void RegisterFormatter(Type type, Action<IJsonWriter, object> formatter)
  242. {
  243. Internal.Writer._formatters[type] = formatter;
  244. }
  245. // Typed version of above
  246. public static void RegisterFormatter<T>(Action<IJsonWriter, T> formatter)
  247. {
  248. RegisterFormatter(typeof(T), (w, o) => formatter(w, (T)o));
  249. }
  250. // Register a parser for a specified type
  251. public static void RegisterParser(Type type, Func<IJsonReader, Type, object> parser)
  252. {
  253. Internal.Reader._parsers.Set(type, parser);
  254. }
  255. // Register a typed parser
  256. public static void RegisterParser<T>(Func<IJsonReader, Type, T> parser)
  257. {
  258. RegisterParser(typeof(T), (r, t) => parser(r, t));
  259. }
  260. // Simpler version for simple types
  261. public static void RegisterParser(Type type, Func<object, object> parser)
  262. {
  263. RegisterParser(type, (r, t) => r.ReadLiteral(parser));
  264. }
  265. // Simpler and typesafe parser for simple types
  266. public static void RegisterParser<T>(Func<object, T> parser)
  267. {
  268. RegisterParser(typeof(T), literal => parser(literal));
  269. }
  270. // Register an into parser
  271. public static void RegisterIntoParser(Type type, Action<IJsonReader, object> parser)
  272. {
  273. Internal.Reader._intoParsers.Set(type, parser);
  274. }
  275. // Register an into parser
  276. public static void RegisterIntoParser<T>(Action<IJsonReader, object> parser)
  277. {
  278. RegisterIntoParser(typeof(T), parser);
  279. }
  280. // Register a factory for instantiating objects (typically abstract classes)
  281. // Callback will be invoked for each key in the dictionary until it returns an object
  282. // instance and which point it will switch to serialization using reflection
  283. public static void RegisterTypeFactory(Type type, Func<IJsonReader, string, object> factory)
  284. {
  285. Internal.Reader._typeFactories.Set(type, factory);
  286. }
  287. // Register a callback to provide a formatter for a newly encountered type
  288. public static void SetFormatterResolver(Func<Type, Action<IJsonWriter, object>> resolver)
  289. {
  290. Internal.Writer._formatterResolver = resolver;
  291. }
  292. // Register a callback to provide a parser for a newly encountered value type
  293. public static void SetParserResolver(Func<Type, Func<IJsonReader, Type, object>> resolver)
  294. {
  295. Internal.Reader._parserResolver = resolver;
  296. }
  297. // Register a callback to provide a parser for a newly encountered reference type
  298. public static void SetIntoParserResolver(Func<Type, Action<IJsonReader, object>> resolver)
  299. {
  300. Internal.Reader._intoParserResolver = resolver;
  301. }
  302. public static bool WalkPath(this IDictionary<string, object> This, string Path, bool create, Func<IDictionary<string,object>,string, bool> leafCallback)
  303. {
  304. // Walk the path
  305. var parts = Path.Split('.');
  306. for (int i = 0; i < parts.Length-1; i++)
  307. {
  308. object val;
  309. if (!This.TryGetValue(parts[i], out val))
  310. {
  311. if (!create)
  312. return false;
  313. val = new Dictionary<string, object>();
  314. This[parts[i]] = val;
  315. }
  316. This = (IDictionary<string,object>)val;
  317. }
  318. // Process the leaf
  319. return leafCallback(This, parts[parts.Length-1]);
  320. }
  321. public static bool PathExists(this IDictionary<string, object> This, string Path)
  322. {
  323. return This.WalkPath(Path, false, (dict, key) => dict.ContainsKey(key));
  324. }
  325. public static object GetPath(this IDictionary<string, object> This, Type type, string Path, object def)
  326. {
  327. This.WalkPath(Path, false, (dict, key) =>
  328. {
  329. object val;
  330. if (dict.TryGetValue(key, out val))
  331. {
  332. if (val == null)
  333. def = val;
  334. else if (type.IsAssignableFrom(val.GetType()))
  335. def = val;
  336. else
  337. def = Reparse(type, val);
  338. }
  339. return true;
  340. });
  341. return def;
  342. }
  343. // Ensure there's an object of type T at specified path
  344. public static T GetObjectAtPath<T>(this IDictionary<string, object> This, string Path) where T:class,new()
  345. {
  346. T retVal = null;
  347. This.WalkPath(Path, true, (dict, key) =>
  348. {
  349. object val;
  350. dict.TryGetValue(key, out val);
  351. retVal = val as T;
  352. if (retVal == null)
  353. {
  354. retVal = val == null ? new T() : Reparse<T>(val);
  355. dict[key] = retVal;
  356. }
  357. return true;
  358. });
  359. return retVal;
  360. }
  361. public static T GetPath<T>(this IDictionary<string, object> This, string Path, T def = default(T))
  362. {
  363. return (T)This.GetPath(typeof(T), Path, def);
  364. }
  365. public static void SetPath(this IDictionary<string, object> This, string Path, object value)
  366. {
  367. This.WalkPath(Path, true, (dict, key) => { dict[key] = value; return true; });
  368. }
  369. // Resolve passed options
  370. static JsonOptions ResolveOptions(JsonOptions options)
  371. {
  372. JsonOptions resolved = JsonOptions.None;
  373. if ((options & (JsonOptions.WriteWhitespace|JsonOptions.DontWriteWhitespace))!=0)
  374. resolved |= options & (JsonOptions.WriteWhitespace | JsonOptions.DontWriteWhitespace);
  375. else
  376. resolved |= WriteWhitespaceDefault ? JsonOptions.WriteWhitespace : JsonOptions.DontWriteWhitespace;
  377. if ((options & (JsonOptions.StrictParser | JsonOptions.NonStrictParser)) != 0)
  378. resolved |= options & (JsonOptions.StrictParser | JsonOptions.NonStrictParser);
  379. else
  380. resolved |= StrictParserDefault ? JsonOptions.StrictParser : JsonOptions.NonStrictParser;
  381. return resolved;
  382. }
  383. }
  384. // Called before loading via reflection
  385. [Obfuscation(Exclude = true, ApplyToMembers = true)]
  386. public interface IJsonLoading
  387. {
  388. void OnJsonLoading(IJsonReader r);
  389. }
  390. // Called after loading via reflection
  391. [Obfuscation(Exclude = true, ApplyToMembers = true)]
  392. public interface IJsonLoaded
  393. {
  394. void OnJsonLoaded(IJsonReader r);
  395. }
  396. // Called for each field while loading from reflection
  397. // Return true if handled
  398. [Obfuscation(Exclude = true, ApplyToMembers = true)]
  399. public interface IJsonLoadField
  400. {
  401. bool OnJsonField(IJsonReader r, string key);
  402. }
  403. // Called when about to write using reflection
  404. [Obfuscation(Exclude = true, ApplyToMembers = true)]
  405. public interface IJsonWriting
  406. {
  407. void OnJsonWriting(IJsonWriter w);
  408. }
  409. // Called after written using reflection
  410. [Obfuscation(Exclude = true, ApplyToMembers = true)]
  411. public interface IJsonWritten
  412. {
  413. void OnJsonWritten(IJsonWriter w);
  414. }
  415. // Describes the current literal in the json stream
  416. public enum LiteralKind
  417. {
  418. None,
  419. String,
  420. Null,
  421. True,
  422. False,
  423. SignedInteger,
  424. UnsignedInteger,
  425. FloatingPoint,
  426. }
  427. // Passed to registered parsers
  428. [Obfuscation(Exclude=true, ApplyToMembers=true)]
  429. public interface IJsonReader
  430. {
  431. object Parse(Type type);
  432. T Parse<T>();
  433. void ParseInto(object into);
  434. object ReadLiteral(Func<object, object> converter);
  435. void ParseDictionary(Action<string> callback);
  436. void ParseArray(Action callback);
  437. LiteralKind GetLiteralKind();
  438. string GetLiteralString();
  439. void NextToken();
  440. }
  441. // Passed to registered formatters
  442. [Obfuscation(Exclude = true, ApplyToMembers = true)]
  443. public interface IJsonWriter
  444. {
  445. void WriteStringLiteral(string str);
  446. void WriteRaw(string str);
  447. void WriteArray(Action callback);
  448. void WriteDictionary(Action callback);
  449. void WriteValue(object value);
  450. void WriteElement();
  451. void WriteKey(string key);
  452. void WriteKeyNoEscaping(string key);
  453. }
  454. // Exception thrown for any parse error
  455. public class JsonParseException : Exception
  456. {
  457. public JsonParseException(Exception inner, string context, JsonLineOffset position) :
  458. base(string.Format("JSON parse error at {0}{1} - {2}", position, string.IsNullOrEmpty(context) ? "" : string.Format(", context {0}", context), inner.Message), inner)
  459. {
  460. Position = position;
  461. Context = context;
  462. }
  463. public JsonLineOffset Position;
  464. public string Context;
  465. }
  466. // Represents a line and character offset position in the source Json
  467. public struct JsonLineOffset
  468. {
  469. public int Line;
  470. public int Offset;
  471. public override string ToString()
  472. {
  473. return string.Format("line {0}, character {1}", Line + 1, Offset + 1);
  474. }
  475. }
  476. // Used to decorate fields and properties that should be serialized
  477. //
  478. // - [Json] on class or struct causes all public fields and properties to be serialized
  479. // - [Json] on a public or non-public field or property causes that member to be serialized
  480. // - [JsonExclude] on a field or property causes that field to be not serialized
  481. // - A class or struct with no [Json] attribute has all public fields/properties serialized
  482. // - A class or struct with no [Json] attribute but a [Json] attribute on one or more members only serializes those members
  483. //
  484. // Use [Json("keyname")] to explicitly specify the key to be used
  485. // [Json] without the keyname will be serialized using the name of the member with the first letter lowercased.
  486. //
  487. // [Json(KeepInstance=true)] causes container/subobject types to be serialized into the existing member instance (if not null)
  488. //
  489. // You can also use the system supplied DataContract and DataMember attributes. They'll only be used if there
  490. // are no PetaJson attributes on the class or it's members. You must specify DataContract on the type and
  491. // DataMember on any fields/properties that require serialization. There's no need for exclude attribute.
  492. // When using DataMember, the name of the field or property is used as is - the first letter is left in upper case
  493. //
  494. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Property | AttributeTargets.Field)]
  495. public class JsonAttribute : Attribute
  496. {
  497. public JsonAttribute()
  498. {
  499. _key = null;
  500. }
  501. public JsonAttribute(string key)
  502. {
  503. _key = key;
  504. }
  505. // Key used to save this field/property
  506. string _key;
  507. public string Key
  508. {
  509. get { return _key; }
  510. }
  511. // If true uses ParseInto to parse into the existing object instance
  512. // If false, creates a new instance as assigns it to the property
  513. public bool KeepInstance
  514. {
  515. get;
  516. set;
  517. }
  518. // If true, the property will be loaded, but not saved
  519. // Use to upgrade deprecated persisted settings, but not
  520. // write them back out again
  521. public bool Deprecated
  522. {
  523. get;
  524. set;
  525. }
  526. }
  527. // See comments for JsonAttribute above
  528. [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
  529. public class JsonExcludeAttribute : Attribute
  530. {
  531. public JsonExcludeAttribute()
  532. {
  533. }
  534. }
  535. // Apply to enum values to specify which enum value to select
  536. // if the supplied json value doesn't match any.
  537. // If not found throws an exception
  538. // eg, any unknown values in the json will be mapped to Fruit.unknown
  539. //
  540. // [JsonUnknown(Fruit.unknown)]
  541. // enum Fruit
  542. // {
  543. // unknown,
  544. // Apple,
  545. // Pear,
  546. // }
  547. [AttributeUsage(AttributeTargets.Enum)]
  548. public class JsonUnknownAttribute : Attribute
  549. {
  550. public JsonUnknownAttribute(object unknownValue)
  551. {
  552. UnknownValue = unknownValue;
  553. }
  554. public object UnknownValue
  555. {
  556. get;
  557. private set;
  558. }
  559. }
  560. namespace Internal
  561. {
  562. [Obfuscation(Exclude = true, ApplyToMembers = true)]
  563. public enum Token
  564. {
  565. EOF,
  566. Identifier,
  567. Literal,
  568. OpenBrace,
  569. CloseBrace,
  570. OpenSquare,
  571. CloseSquare,
  572. Equal,
  573. Colon,
  574. SemiColon,
  575. Comma,
  576. }
  577. // Helper to create instances but include the type name in the thrown exception
  578. public static class DecoratingActivator
  579. {
  580. public static object CreateInstance(Type t)
  581. {
  582. try
  583. {
  584. return Activator.CreateInstance(t);
  585. }
  586. catch (Exception x)
  587. {
  588. throw new InvalidOperationException(string.Format("Failed to create instance of type '{0}'", t.FullName), x);
  589. }
  590. }
  591. }
  592. public class Reader : IJsonReader
  593. {
  594. static Reader()
  595. {
  596. // Setup default resolvers
  597. _parserResolver = ResolveParser;
  598. _intoParserResolver = ResolveIntoParser;
  599. Func<IJsonReader, Type, object> simpleConverter = (reader, type) =>
  600. {
  601. return reader.ReadLiteral(literal => Convert.ChangeType(literal, type, CultureInfo.InvariantCulture));
  602. };
  603. Func<IJsonReader, Type, object> numberConverter = (reader, type) =>
  604. {
  605. switch (reader.GetLiteralKind())
  606. {
  607. case LiteralKind.SignedInteger:
  608. case LiteralKind.UnsignedInteger:
  609. case LiteralKind.FloatingPoint:
  610. object val = Convert.ChangeType(reader.GetLiteralString(), type, CultureInfo.InvariantCulture);
  611. reader.NextToken();
  612. return val;
  613. }
  614. throw new InvalidDataException("expected a numeric literal");
  615. };
  616. // Default type handlers
  617. _parsers.Set(typeof(string), simpleConverter);
  618. _parsers.Set(typeof(char), simpleConverter);
  619. _parsers.Set(typeof(bool), simpleConverter);
  620. _parsers.Set(typeof(byte), numberConverter);
  621. _parsers.Set(typeof(sbyte), numberConverter);
  622. _parsers.Set(typeof(short), numberConverter);
  623. _parsers.Set(typeof(ushort), numberConverter);
  624. _parsers.Set(typeof(int), numberConverter);
  625. _parsers.Set(typeof(uint), numberConverter);
  626. _parsers.Set(typeof(long), numberConverter);
  627. _parsers.Set(typeof(ulong), numberConverter);
  628. _parsers.Set(typeof(decimal), numberConverter);
  629. _parsers.Set(typeof(float), numberConverter);
  630. _parsers.Set(typeof(double), numberConverter);
  631. _parsers.Set(typeof(DateTime), (reader, type) =>
  632. {
  633. return reader.ReadLiteral(literal => Utils.FromUnixMilliseconds((long)Convert.ChangeType(literal, typeof(long), CultureInfo.InvariantCulture)));
  634. });
  635. _parsers.Set(typeof(byte[]), (reader, type) =>
  636. {
  637. return reader.ReadLiteral(literal => Convert.FromBase64String((string)Convert.ChangeType(literal, typeof(string), CultureInfo.InvariantCulture)));
  638. });
  639. }
  640. public Reader(TextReader r, JsonOptions options)
  641. {
  642. _tokenizer = new Tokenizer(r, options);
  643. _options = options;
  644. }
  645. Tokenizer _tokenizer;
  646. JsonOptions _options;
  647. List<string> _contextStack = new List<string>();
  648. public string Context
  649. {
  650. get
  651. {
  652. return string.Join(".", _contextStack);
  653. }
  654. }
  655. static Action<IJsonReader, object> ResolveIntoParser(Type type)
  656. {
  657. var ri = ReflectionInfo.GetReflectionInfo(type);
  658. if (ri != null)
  659. return ri.ParseInto;
  660. else
  661. return null;
  662. }
  663. static Func<IJsonReader, Type, object> ResolveParser(Type type)
  664. {
  665. // See if the Type has a static parser method - T ParseJson(IJsonReader)
  666. var parseJson = ReflectionInfo.FindParseJson(type);
  667. if (parseJson != null)
  668. {
  669. if (parseJson.GetParameters()[0].ParameterType == typeof(IJsonReader))
  670. {
  671. return (r, t) => parseJson.Invoke(null, new Object[] { r });
  672. }
  673. else
  674. {
  675. return (r, t) =>
  676. {
  677. if (r.GetLiteralKind() == LiteralKind.String)
  678. {
  679. var o = parseJson.Invoke(null, new Object[] { r.GetLiteralString() });
  680. r.NextToken();
  681. return o;
  682. }
  683. throw new InvalidDataException(string.Format("Expected string literal for type {0}", type.FullName));
  684. };
  685. }
  686. }
  687. return (r, t) =>
  688. {
  689. var into = DecoratingActivator.CreateInstance(type);
  690. r.ParseInto(into);
  691. return into;
  692. };
  693. }
  694. public JsonLineOffset CurrentTokenPosition
  695. {
  696. get { return _tokenizer.CurrentTokenPosition; }
  697. }
  698. // ReadLiteral is implemented with a converter callback so that any
  699. // errors on converting to the target type are thrown before the tokenizer
  700. // is advanced to the next token. This ensures error location is reported
  701. // at the start of the literal, not the following token.
  702. public object ReadLiteral(Func<object, object> converter)
  703. {
  704. _tokenizer.Check(Token.Literal);
  705. var retv = converter(_tokenizer.LiteralValue);
  706. _tokenizer.NextToken();
  707. return retv;
  708. }
  709. public void CheckEOF()
  710. {
  711. _tokenizer.Check(Token.EOF);
  712. }
  713. public object Parse(Type type)
  714. {
  715. // Null?
  716. if (_tokenizer.CurrentToken == Token.Literal && _tokenizer.LiteralKind == LiteralKind.Null)
  717. {
  718. _tokenizer.NextToken();
  719. return null;
  720. }
  721. // Handle nullable types
  722. var typeUnderlying = Nullable.GetUnderlyingType(type);
  723. if (typeUnderlying != null)
  724. type = typeUnderlying;
  725. // See if we have a reader
  726. Func<IJsonReader, Type, object> parser;
  727. if (Reader._parsers.TryGetValue(type, out parser))
  728. {
  729. return parser(this, type);
  730. }
  731. // See if we have factory
  732. Func<IJsonReader, string, object> factory;
  733. if (Reader._typeFactories.TryGetValue(type, out factory))
  734. {
  735. // Try first without passing dictionary keys
  736. object into = factory(this, null);
  737. if (into == null)
  738. {
  739. // This is a awkward situation. The factory requires a value from the dictionary
  740. // in order to create the target object (typically an abstract class with the class
  741. // kind recorded in the Json). Since there's no guarantee of order in a json dictionary
  742. // we can't assume the required key is first.
  743. // So, create a bookmark on the tokenizer, read keys until the factory returns an
  744. // object instance and then rewind the tokenizer and continue
  745. // Create a bookmark so we can rewind
  746. _tokenizer.CreateBookmark();
  747. // Skip the opening brace
  748. _tokenizer.Skip(Token.OpenBrace);
  749. // First pass to work out type
  750. ParseDictionaryKeys(key =>
  751. {
  752. // Try to instantiate the object
  753. into = factory(this, key);
  754. return into == null;
  755. });
  756. // Move back to start of the dictionary
  757. _tokenizer.RewindToBookmark();
  758. // Quit if still didn't get an object from the factory
  759. if (into == null)
  760. throw new InvalidOperationException("Factory didn't create object instance (probably due to a missing key in the Json)");
  761. }
  762. // Second pass
  763. ParseInto(into);
  764. // Done
  765. return into;
  766. }
  767. // Do we already have an into parser?
  768. Action<IJsonReader, object> intoParser;
  769. if (Reader._intoParsers.TryGetValue(type, out intoParser))
  770. {
  771. var into = DecoratingActivator.CreateInstance(type);
  772. ParseInto(into);
  773. return into;
  774. }
  775. // Enumerated type?
  776. if (type.IsEnum)
  777. {
  778. if (type.GetCustomAttributes(typeof(FlagsAttribute), false).Any())
  779. return ReadLiteral(literal => {
  780. try
  781. {
  782. return Enum.Parse(type, (string)literal);
  783. }
  784. catch
  785. {
  786. return Enum.ToObject(type, literal);
  787. }
  788. });
  789. else
  790. return ReadLiteral(literal => {
  791. try
  792. {
  793. return Enum.Parse(type, (string)literal);
  794. }
  795. catch (Exception)
  796. {
  797. var attr = type.GetCustomAttributes(typeof(JsonUnknownAttribute), false).FirstOrDefault();
  798. if (attr==null)
  799. throw;
  800. return ((JsonUnknownAttribute)attr).UnknownValue;
  801. }
  802. });
  803. }
  804. // Array?
  805. if (type.IsArray && type.GetArrayRank() == 1)
  806. {
  807. // First parse as a List<>
  808. var listType = typeof(List<>).MakeGenericType(type.GetElementType());
  809. var list = DecoratingActivator.CreateInstance(listType);
  810. ParseInto(list);
  811. return listType.GetMethod("ToArray").Invoke(list, null);
  812. }
  813. // Convert interfaces to concrete types
  814. if (type.IsInterface)
  815. type = Utils.ResolveInterfaceToClass(type);
  816. // Untyped dictionary?
  817. if (_tokenizer.CurrentToken == Token.OpenBrace && (type.IsAssignableFrom(typeof(IDictionary<string, object>))))
  818. {
  819. #if !PETAJSON_NO_DYNAMIC
  820. var container = (new ExpandoObject()) as IDictionary<string, object>;
  821. #else
  822. var container = new Dictionary<string, object>();
  823. #endif
  824. ParseDictionary(key =>
  825. {
  826. container[key] = Parse(typeof(Object));
  827. });
  828. return container;
  829. }
  830. // Untyped list?
  831. if (_tokenizer.CurrentToken == Token.OpenSquare && (type.IsAssignableFrom(typeof(List<object>))))
  832. {
  833. var container = new List<object>();
  834. ParseArray(() =>
  835. {
  836. container.Add(Parse(typeof(Object)));
  837. });
  838. return container;
  839. }
  840. // Untyped literal?
  841. if (_tokenizer.CurrentToken == Token.Literal && type.IsAssignableFrom(_tokenizer.LiteralType))
  842. {
  843. var lit = _tokenizer.LiteralValue;
  844. _tokenizer.NextToken();
  845. return lit;
  846. }
  847. // Call value type resolver
  848. if (type.IsValueType)
  849. {
  850. var tp = _parsers.Get(type, () => _parserResolver(type));
  851. if (tp != null)
  852. {
  853. return tp(this, type);
  854. }
  855. }
  856. // Call reference type resolver
  857. if (type.IsClass && type != typeof(object))
  858. {
  859. var into = DecoratingActivator.CreateInstance(type);
  860. ParseInto(into);
  861. return into;
  862. }
  863. // Give up
  864. throw new InvalidDataException(string.Format("syntax error, unexpected token {0}", _tokenizer.CurrentToken));
  865. }
  866. // Parse into an existing object instance
  867. public void ParseInto(object into)
  868. {
  869. if (into == null)
  870. return;
  871. if (_tokenizer.CurrentToken == Token.Literal && _tokenizer.LiteralKind == LiteralKind.Null)
  872. {
  873. throw new InvalidOperationException("can't parse null into existing instance");
  874. //return;
  875. }
  876. var type = into.GetType();
  877. // Existing parse into handler?
  878. Action<IJsonReader,object> parseInto;
  879. if (_intoParsers.TryGetValue(type, out parseInto))
  880. {
  881. parseInto(this, into);
  882. return;
  883. }
  884. // Generic dictionary?
  885. var dictType = Utils.FindGenericInterface(type, typeof(IDictionary<,>));
  886. if (dictType!=null)
  887. {
  888. // Get the key and value types
  889. var typeKey = dictType.GetGenericArguments()[0];
  890. var typeValue = dictType.GetGenericArguments()[1];
  891. // Parse it
  892. IDictionary dict = (IDictionary)into;
  893. dict.Clear();
  894. ParseDictionary(key =>
  895. {
  896. dict.Add(Convert.ChangeType(key, typeKey), Parse(typeValue));
  897. });
  898. return;
  899. }
  900. // Generic list
  901. var listType = Utils.FindGenericInterface(type, typeof(IList<>));
  902. if (listType!=null)
  903. {
  904. // Get element type
  905. var typeElement = listType.GetGenericArguments()[0];
  906. // Parse it
  907. IList list = (IList)into;
  908. list.Clear();
  909. ParseArray(() =>
  910. {
  911. list.Add(Parse(typeElement));
  912. });
  913. return;
  914. }
  915. // Untyped dictionary
  916. var objDict = into as IDictionary;
  917. if (objDict != null)
  918. {
  919. objDict.Clear();
  920. ParseDictionary(key =>
  921. {
  922. objDict[key] = Parse(typeof(Object));
  923. });
  924. return;
  925. }
  926. // Untyped list
  927. var objList = into as IList;
  928. if (objList!=null)
  929. {
  930. objList.Clear();
  931. ParseArray(() =>
  932. {
  933. objList.Add(Parse(typeof(Object)));
  934. });
  935. return;
  936. }
  937. // Try to resolve a parser
  938. var intoParser = _intoParsers.Get(type, () => _intoParserResolver(type));
  939. if (intoParser != null)
  940. {
  941. intoParser(this, into);
  942. return;
  943. }
  944. throw new InvalidOperationException(string.Format("Don't know how to parse into type '{0}'", type.FullName));
  945. }
  946. public T Parse<T>()
  947. {
  948. return (T)Parse(typeof(T));
  949. }
  950. public LiteralKind GetLiteralKind()
  951. {
  952. return _tokenizer.LiteralKind;
  953. }
  954. public string GetLiteralString()
  955. {
  956. return _tokenizer.String;
  957. }
  958. public void NextToken()
  959. {
  960. _tokenizer.NextToken();
  961. }
  962. // Parse a dictionary
  963. public void ParseDictionary(Action<string> callback)
  964. {
  965. _tokenizer.Skip(Token.OpenBrace);
  966. ParseDictionaryKeys(key => { callback(key); return true; });
  967. _tokenizer.Skip(Token.CloseBrace);
  968. }
  969. // Parse dictionary keys, calling callback for each one. Continues until end of input
  970. // or when callback returns false
  971. private void ParseDictionaryKeys(Func<string, bool> callback)
  972. {
  973. // End?
  974. while (_tokenizer.CurrentToken != Token.CloseBrace)
  975. {
  976. // Parse the key
  977. string key = null;
  978. if (_tokenizer.CurrentToken == Token.Identifier && (_options & JsonOptions.StrictParser)==0)
  979. {
  980. key = _tokenizer.String;
  981. }
  982. else if (_tokenizer.CurrentToken == Token.Literal && _tokenizer.LiteralKind == LiteralKind.String)
  983. {
  984. key = (string)_tokenizer.LiteralValue;
  985. }
  986. else
  987. {
  988. throw new InvalidDataException("syntax error, expected string literal or identifier");
  989. }
  990. _tokenizer.NextToken();
  991. _tokenizer.Skip(Token.Colon);
  992. // Remember current position
  993. var pos = _tokenizer.CurrentTokenPosition;
  994. // Call the callback, quit if cancelled
  995. _contextStack.Add(key);
  996. bool doDefaultProcessing = callback(key);
  997. _contextStack.RemoveAt(_contextStack.Count-1);
  998. if (!doDefaultProcessing)
  999. return;
  1000. // If the callback didn't read anything from the tokenizer, then skip it ourself
  1001. if (pos.Line == _tokenizer.CurrentTokenPosition.Line && pos.Offset == _tokenizer.CurrentTokenPosition.Offset)
  1002. {
  1003. Parse(typeof(object));
  1004. }
  1005. // Separating/trailing comma
  1006. if (_tokenizer.SkipIf(Token.Comma))
  1007. {
  1008. if ((_options & JsonOptions.StrictParser) != 0 && _tokenizer.CurrentToken == Token.CloseBrace)
  1009. {
  1010. throw new InvalidDataException("Trailing commas not allowed in strict mode");
  1011. }
  1012. continue;
  1013. }
  1014. // End
  1015. break;
  1016. }
  1017. }
  1018. // Parse an array
  1019. public void ParseArray(Action callback)
  1020. {
  1021. _tokenizer.Skip(Token.OpenSquare);
  1022. int index = 0;
  1023. while (_tokenizer.CurrentToken != Token.CloseSquare)
  1024. {
  1025. _contextStack.Add(string.Format("[{0}]", index));
  1026. callback();
  1027. _contextStack.RemoveAt(_contextStack.Count-1);
  1028. if (_tokenizer.SkipIf(Token.Comma))
  1029. {
  1030. if ((_options & JsonOptions.StrictParser)!=0 && _tokenizer.CurrentToken==Token.CloseSquare)
  1031. {
  1032. throw new InvalidDataException("Trailing commas not allowed in strict mode");
  1033. }
  1034. continue;
  1035. }
  1036. break;
  1037. }
  1038. _tokenizer.Skip(Token.CloseSquare);
  1039. }
  1040. // Yikes!
  1041. public static Func<Type, Action<IJsonReader, object>> _intoParserResolver;
  1042. public static Func<Type, Func<IJsonReader, Type, object>> _parserResolver;
  1043. public static ThreadSafeCache<Type, Func<IJsonReader, Type, object>> _parsers = new ThreadSafeCache<Type, Func<IJsonReader, Type, object>>();
  1044. public static ThreadSafeCache<Type, Action<IJsonReader, object>> _intoParsers = new ThreadSafeCache<Type, Action<IJsonReader, object>>();
  1045. public static ThreadSafeCache<Type, Func<IJsonReader, string, object>> _typeFactories = new ThreadSafeCache<Type, Func<IJsonReader, string, object>>();
  1046. }
  1047. public class Writer : IJsonWriter
  1048. {
  1049. static Writer()
  1050. {
  1051. _formatterResolver = ResolveFormatter;
  1052. // Register standard formatters
  1053. _formatters.Add(typeof(string), (w, o) => w.WriteStringLiteral((string)o));
  1054. _formatters.Add(typeof(char), (w, o) => w.WriteStringLiteral(((char)o).ToString()));
  1055. _formatters.Add(typeof(bool), (w, o) => w.WriteRaw(((bool)o) ? "true" : "false"));
  1056. Action<IJsonWriter, object> convertWriter = (w, o) => w.WriteRaw((string)Convert.ChangeType(o, typeof(string), System.Globalization.CultureInfo.InvariantCulture));
  1057. _formatters.Add(typeof(int), convertWriter);
  1058. _formatters.Add(typeof(uint), convertWriter);
  1059. _formatters.Add(typeof(long), convertWriter);
  1060. _formatters.Add(typeof(ulong), convertWriter);
  1061. _formatters.Add(typeof(short), convertWriter);
  1062. _formatters.Add(typeof(ushort), convertWriter);
  1063. _formatters.Add(typeof(decimal), convertWriter);
  1064. _formatters.Add(typeof(byte), convertWriter);
  1065. _formatters.Add(typeof(sbyte), convertWriter);
  1066. _formatters.Add(typeof(DateTime), (w, o) => convertWriter(w, Utils.ToUnixMilliseconds((DateTime)o)));
  1067. _formatters.Add(typeof(float), (w, o) => w.WriteRaw(((float)o).ToString("R", System.Globalization.CultureInfo.InvariantCulture)));
  1068. _formatters.Add(typeof(double), (w, o) => w.WriteRaw(((double)o).ToString("R", System.Globalization.CultureInfo.InvariantCulture)));
  1069. _formatters.Add(typeof(byte[]), (w, o) =>
  1070. {
  1071. w.WriteRaw("\"");
  1072. w.WriteRaw(Convert.ToBase64String((byte[])o));
  1073. w.WriteRaw("\"");
  1074. });
  1075. }
  1076. public static Func<Type, Action<IJsonWriter, object>> _formatterResolver;
  1077. public static Dictionary<Type, Action<IJsonWriter, object>> _formatters = new Dictionary<Type, Action<IJsonWriter, object>>();
  1078. static Action<IJsonWriter, object> ResolveFormatter(Type type)
  1079. {
  1080. // Try `void FormatJson(IJsonWriter)`
  1081. var formatJson = ReflectionInfo.FindFormatJson(type);
  1082. if (formatJson != null)
  1083. {
  1084. if (formatJson.ReturnType==typeof(void))
  1085. return (w, obj) => formatJson.Invoke(obj, new Object[] { w });
  1086. if (formatJson.ReturnType == typeof(string))
  1087. return (w, obj) => w.WriteStringLiteral((string)formatJson.Invoke(obj, new Object[] { }));
  1088. }
  1089. var ri = ReflectionInfo.GetReflectionInfo(type);
  1090. if (ri != null)
  1091. return ri.Write;
  1092. else
  1093. return null;
  1094. }
  1095. public Writer(TextWriter w, JsonOptions options)
  1096. {
  1097. _writer = w;
  1098. _atStartOfLine = true;
  1099. _needElementSeparator = false;
  1100. _options = options;
  1101. }
  1102. private TextWriter _writer;
  1103. private int IndentLevel;
  1104. private bool _atStartOfLine;
  1105. private bool _needElementSeparator = false;
  1106. private JsonOptions _options;
  1107. private char _currentBlockKind = '\0';
  1108. // Move to the next line
  1109. public void NextLine()
  1110. {
  1111. if (_atStartOfLine)
  1112. return;
  1113. if ((_options & JsonOptions.WriteWhitespace)!=0)
  1114. {
  1115. WriteRaw("\n");
  1116. WriteRaw(new string('\t', IndentLevel));
  1117. }
  1118. _atStartOfLine = true;
  1119. }
  1120. // Start the next element, writing separators and white space
  1121. void NextElement()
  1122. {
  1123. if (_needElementSeparator)
  1124. {
  1125. WriteRaw(",");
  1126. NextLine();
  1127. }
  1128. else
  1129. {
  1130. NextLine();
  1131. IndentLevel++;
  1132. WriteRaw(_currentBlockKind.ToString());
  1133. NextLine();
  1134. }
  1135. _needElementSeparator = true;
  1136. }
  1137. // Write next array element
  1138. public void WriteElement()
  1139. {
  1140. if (_currentBlockKind != '[')
  1141. throw new InvalidOperationException("Attempt to write array element when not in array block");
  1142. NextElement();
  1143. }
  1144. // Write next dictionary key
  1145. public void WriteKey(string key)
  1146. {
  1147. if (_currentBlockKind != '{')
  1148. throw new InvalidOperationException("Attempt to write dictionary element when not in dictionary block");
  1149. NextElement();
  1150. WriteStringLiteral(key);
  1151. WriteRaw(((_options & JsonOptions.WriteWhitespace) != 0) ? ": " : ":");
  1152. }
  1153. // Write an already escaped dictionary key
  1154. public void WriteKeyNoEscaping(string key)
  1155. {
  1156. if (_currentBlockKind != '{')
  1157. throw new InvalidOperationException("Attempt to write dictionary element when not in dictionary block");
  1158. NextElement();
  1159. WriteRaw("\"");
  1160. WriteRaw(key);
  1161. WriteRaw("\"");
  1162. WriteRaw(((_options & JsonOptions.WriteWhitespace) != 0) ? ": " : ":");
  1163. }
  1164. // Write anything
  1165. public void WriteRaw(string str)
  1166. {
  1167. _atStartOfLine = false;
  1168. _writer.Write(str);
  1169. }
  1170. static int IndexOfEscapeableChar(string str, int pos)
  1171. {
  1172. int length = str.Length;
  1173. while (pos < length)
  1174. {
  1175. var ch = str[pos];
  1176. if (ch == '\\' || ch == '/' || ch == '\"' || (ch>=0 && ch <= 0x1f) || (ch >= 0x7f && ch <=0x9f) || ch==0x2028 || ch== 0x2029)
  1177. return pos;
  1178. pos++;
  1179. }
  1180. return -1;
  1181. }
  1182. public void WriteStringLiteral(string str)
  1183. {
  1184. _atStartOfLine = false;
  1185. if (str == null)
  1186. {
  1187. _writer.Write("null");
  1188. return;
  1189. }
  1190. _writer.Write("\"");
  1191. int pos = 0;
  1192. int escapePos;
  1193. while ((escapePos = IndexOfEscapeableChar(str, pos)) >= 0)
  1194. {
  1195. if (escapePos > pos)
  1196. _writer.Write(str.Substring(pos, escapePos - pos));
  1197. switch (str[escapePos])
  1198. {
  1199. case '\"': _writer.Write("\\\""); break;
  1200. case '\\': _writer.Write("\\\\"); break;
  1201. case '/': _writer.Write("\\/"); break;
  1202. case '\b': _writer.Write("\\b"); break;
  1203. case '\f': _writer.Write("\\f"); break;
  1204. case '\n': _writer.Write("\\n"); break;
  1205. case '\r': _writer.Write("\\r"); break;
  1206. case '\t': _writer.Write("\\t"); break;
  1207. default:
  1208. _writer.Write(string.Format("\\u{0:x4}", (int)str[escapePos]));
  1209. break;
  1210. }
  1211. pos = escapePos + 1;
  1212. }
  1213. if (str.Length > pos)
  1214. _writer.Write(str.Substring(pos));
  1215. _writer.Write("\"");
  1216. }
  1217. // Write an array or dictionary block
  1218. private void WriteBlock(string open, string close, Action callback)
  1219. {
  1220. var prevBlockKind = _currentBlockKind;
  1221. _currentBlockKind = open[0];
  1222. var didNeedElementSeparator = _needElementSeparator;
  1223. _needElementSeparator = false;
  1224. callback();
  1225. if (_needElementSeparator)
  1226. {
  1227. IndentLevel--;
  1228. NextLine();
  1229. }
  1230. else
  1231. {
  1232. WriteRaw(open);
  1233. }
  1234. WriteRaw(close);
  1235. _needElementSeparator = didNeedElementSeparator;
  1236. _currentBlockKind = prevBlockKind;
  1237. }
  1238. // Write an array
  1239. public void WriteArray(Action callback)
  1240. {
  1241. WriteBlock("[", "]", callback);
  1242. }
  1243. // Write a dictionary
  1244. public void WriteDictionary(Action callback)
  1245. {
  1246. WriteBlock("{", "}", callback);
  1247. }
  1248. // Write any value
  1249. public void WriteValue(object value)
  1250. {
  1251. _atStartOfLine = false;
  1252. // Special handling for null
  1253. if (value == null)
  1254. {
  1255. _writer.Write("null");
  1256. return;
  1257. }
  1258. var type = value.GetType();
  1259. // Handle nullable types
  1260. var typeUnderlying = Nullable.GetUnderlyingType(type);
  1261. if (typeUnderlying != null)
  1262. type = typeUnderlying;
  1263. // Look up type writer
  1264. Action<IJsonWriter, object> typeWriter;
  1265. if (_formatters.TryGetValue(type, out typeWriter))
  1266. {
  1267. // Write it
  1268. typeWriter(this, value);
  1269. return;
  1270. }
  1271. // Enumerated type?
  1272. if (type.IsEnum)
  1273. {
  1274. if (type.GetCustomAttributes(typeof(FlagsAttribute), false).Any())
  1275. WriteRaw(Convert.ToUInt32(value).ToString(CultureInfo.InvariantCulture));
  1276. else
  1277. WriteStringLiteral(value.ToString());
  1278. return;
  1279. }
  1280. // Dictionary?
  1281. var d = value as System.Collections.IDictionary;
  1282. if (d != null)
  1283. {
  1284. WriteDictionary(() =>
  1285. {
  1286. foreach (var key in d.Keys)
  1287. {
  1288. WriteKey(key.ToString());
  1289. WriteValue(d[key]);
  1290. }
  1291. });
  1292. return;
  1293. }
  1294. // Dictionary?
  1295. var dso = value as IDictionary<string,object>;
  1296. if (dso != null)
  1297. {
  1298. WriteDictionary(() =>
  1299. {
  1300. foreach (var key in dso.Keys)
  1301. {
  1302. WriteKey(key.ToString());
  1303. WriteValue(dso[key]);
  1304. }
  1305. });
  1306. return;
  1307. }
  1308. // Array?
  1309. var e = value as System.Collections.IEnumerable;
  1310. if (e != null)
  1311. {
  1312. WriteArray(() =>
  1313. {
  1314. foreach (var i in e)
  1315. {
  1316. WriteElement();
  1317. WriteValue(i);
  1318. }
  1319. });
  1320. return;
  1321. }
  1322. // Resolve a formatter
  1323. var formatter = _formatterResolver(type);
  1324. if (formatter != null)
  1325. {
  1326. _formatters[type] = formatter;
  1327. formatter(this, value);
  1328. return;
  1329. }
  1330. // Give up
  1331. throw new InvalidDataException(string.Format("Don't know how to write '{0}' to json", value.GetType()));
  1332. }
  1333. }
  1334. // Information about a field or property found through reflection
  1335. public class JsonMemberInfo
  1336. {
  1337. // The Json key for this member
  1338. public string JsonKey;
  1339. // True if should keep existing instance (reference types only)
  1340. public bool KeepInstance;
  1341. // True if deprecated
  1342. public bool Deprecated;
  1343. // Reflected member info
  1344. MemberInfo _mi;
  1345. public MemberInfo Member
  1346. {
  1347. get { return _mi; }
  1348. set
  1349. {
  1350. // Store it
  1351. _mi = value;
  1352. // Also create getters and setters
  1353. if (_mi is PropertyInfo)
  1354. {
  1355. GetValue = (obj) => ((PropertyInfo)_mi).GetValue(obj, null);
  1356. SetValue = (obj, val) => ((PropertyInfo)_mi).SetValue(obj, val, null);
  1357. }
  1358. else
  1359. {
  1360. GetValue = ((FieldInfo)_mi).GetValue;
  1361. SetValue = ((FieldInfo)_mi).SetValue;
  1362. }
  1363. }
  1364. }
  1365. // Member type
  1366. public Type MemberType
  1367. {
  1368. get
  1369. {
  1370. if (Member is PropertyInfo)
  1371. {
  1372. return ((PropertyInfo)Member).PropertyType;
  1373. }
  1374. else
  1375. {
  1376. return ((FieldInfo)Member).FieldType;
  1377. }
  1378. }
  1379. }
  1380. // Get/set helpers
  1381. public Action<object, object> SetValue;
  1382. public Func<object, object> GetValue;
  1383. }
  1384. // Stores reflection info about a type
  1385. public class ReflectionInfo
  1386. {
  1387. // List of members to be serialized
  1388. public List<JsonMemberInfo> Members;
  1389. // Cache of these ReflectionInfos's
  1390. static ThreadSafeCache<Type, ReflectionInfo> _cache = new ThreadSafeCache<Type, ReflectionInfo>();
  1391. public static MethodInfo FindFormatJson(Type type)
  1392. {
  1393. if (type.IsValueType)
  1394. {
  1395. // Try `void FormatJson(IJsonWriter)`
  1396. var formatJson = type.GetMethod("FormatJson", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(IJsonWriter) }, null);
  1397. if (formatJson != null && formatJson.ReturnType == typeof(void))
  1398. return formatJson;
  1399. // Try `string FormatJson()`
  1400. formatJson = type.GetMethod("FormatJson", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { }, null);
  1401. if (formatJson != null && formatJson.ReturnType == typeof(string))
  1402. return formatJson;
  1403. }
  1404. return null;
  1405. }
  1406. public static MethodInfo FindParseJson(Type type)
  1407. {
  1408. // Try `T ParseJson(IJsonReader)`
  1409. var parseJson = type.GetMethod("ParseJson", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static, null, new Type[] { typeof(IJsonReader) }, null);
  1410. if (parseJson != null && parseJson.ReturnType == type)
  1411. return parseJson;
  1412. // Try `T ParseJson(string)`
  1413. parseJson = type.GetMethod("ParseJson", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static, null, new Type[] { typeof(string) }, null);
  1414. if (parseJson != null && parseJson.ReturnType == type)
  1415. return parseJson;
  1416. return null;
  1417. }
  1418. // Write one of these types
  1419. public void Write(IJsonWriter w, object val)
  1420. {
  1421. w.WriteDictionary(() =>
  1422. {
  1423. var writing = val as IJsonWriting;
  1424. if (writing != null)
  1425. writing.OnJsonWriting(w);
  1426. foreach (var jmi in Members.Where(x=>!x.Deprecated))
  1427. {
  1428. w.WriteKeyNoEscaping(jmi.JsonKey);
  1429. w.WriteValue(jmi.GetValue(val));
  1430. }
  1431. var written = val as IJsonWritten;
  1432. if (written != null)
  1433. written.OnJsonWritten(w);
  1434. });
  1435. }
  1436. // Read one of these types.
  1437. // NB: Although PetaJson.JsonParseInto only works on reference type, when using reflection
  1438. // it also works for value types so we use the one method for both
  1439. public void ParseInto(IJsonReader r, object into)
  1440. {
  1441. var loading = into as IJsonLoading;
  1442. if (loading != null)
  1443. loading.OnJsonLoading(r);
  1444. r.ParseDictionary(key =>
  1445. {
  1446. ParseFieldOrProperty(r, into, key);
  1447. });
  1448. var loaded = into as IJsonLoaded;
  1449. if (loaded != null)
  1450. loaded.OnJsonLoaded(r);
  1451. }
  1452. // The member info is stored in a list (as opposed to a dictionary) so that
  1453. // the json is written in the same order as the fields/properties are defined
  1454. // On loading, we assume the fields will be in the same order, but need to
  1455. // handle if they're not. This function performs a linear search, but
  1456. // starts after the last found item as an optimization that should work
  1457. // most of the time.
  1458. int _lastFoundIndex = 0;
  1459. bool FindMemberInfo(string name, out JsonMemberInfo found)
  1460. {
  1461. for (int i = 0; i < Members.Count; i++)
  1462. {
  1463. int index = (i + _lastFoundIndex) % Members.Count;
  1464. var jmi = Members[index];
  1465. if (jmi.JsonKey == name)
  1466. {
  1467. _lastFoundIndex = index;
  1468. found = jmi;
  1469. return true;
  1470. }
  1471. }
  1472. found = null;
  1473. return false;
  1474. }
  1475. // Parse a value from IJsonReader into an object instance
  1476. public void ParseFieldOrProperty(IJsonReader r, object into, string key)
  1477. {
  1478. // IJsonLoadField
  1479. var lf = into as IJsonLoadField;
  1480. if (lf != null && lf.OnJsonField(r, key))
  1481. return;
  1482. // Find member
  1483. JsonMemberInfo jmi;
  1484. if (FindMemberInfo(key, out jmi))
  1485. {
  1486. // Try to keep existing instance
  1487. if (jmi.KeepInstance)
  1488. {
  1489. var subInto = jmi.GetValue(into);
  1490. if (subInto != null)
  1491. {
  1492. r.ParseInto(subInto);
  1493. return;
  1494. }
  1495. }
  1496. // Parse and set
  1497. var val = r.Parse(jmi.MemberType);
  1498. jmi.SetValue(into, val);
  1499. return;
  1500. }
  1501. }
  1502. // Get the reflection info for a specified type
  1503. public static ReflectionInfo GetReflectionInfo(Type type)
  1504. {
  1505. // Check cache
  1506. return _cache.Get(type, () =>
  1507. {
  1508. var allMembers = Utils.GetAllFieldsAndProperties(type);
  1509. // Does type have a [Json] attribute
  1510. bool typeMarked = type.GetCustomAttributes(typeof(JsonAttribute), true).OfType<JsonAttribute>().Any();
  1511. // Do any members have a [Json] attribute
  1512. bool anyFieldsMarked = allMembers.Any(x => x.GetCustomAttributes(typeof(JsonAttribute), false).OfType<JsonAttribute>().Any());
  1513. #if !PETAJSON_NO_DATACONTRACT
  1514. // Try with DataContract and friends
  1515. if (!typeMarked && !anyFieldsMarked && type.GetCustomAttributes(typeof(DataContractAttribute), true).OfType<DataContractAttribute>().Any())
  1516. {
  1517. var ri = CreateReflectionInfo(type, mi =>
  1518. {
  1519. // Get attributes
  1520. var attr = mi.GetCustomAttributes(typeof(DataMemberAttribute), false).OfType<DataMemberAttribute>().FirstOrDefault();
  1521. if (attr != null)
  1522. {
  1523. return new JsonMemberInfo()
  1524. {
  1525. Member = mi,
  1526. JsonKey = attr.Name ?? mi.Name, // No lower case first letter if using DataContract/Member
  1527. };
  1528. }
  1529. return null;
  1530. });
  1531. ri.Members.Sort((a, b) => String.CompareOrdinal(a.JsonKey, b.JsonKey)); // Match DataContractJsonSerializer
  1532. return ri;
  1533. }
  1534. #endif
  1535. {
  1536. // Should we serialize all public methods?
  1537. bool serializeAllPublics = typeMarked || !anyFieldsMarked;
  1538. // Build
  1539. var ri = CreateReflectionInfo(type, mi =>
  1540. {
  1541. // Explicitly excluded?
  1542. if (mi.GetCustomAttributes(typeof(JsonExcludeAttribute), false).Any())
  1543. return null;
  1544. // Get attributes
  1545. var attr = mi.GetCustomAttributes(typeof(JsonAttribute), false).OfType<JsonAttribute>().FirstOrDefault();
  1546. if (attr != null)
  1547. {
  1548. return new JsonMemberInfo()
  1549. {
  1550. Member = mi,
  1551. JsonKey = attr.Key ?? mi.Name.Substring(0, 1).ToLower() + mi.Name.Substring(1),
  1552. KeepInstance = attr.KeepInstance,
  1553. Deprecated = attr.Deprecated,
  1554. };
  1555. }
  1556. // Serialize all publics?
  1557. if (serializeAllPublics && Utils.IsPublic(mi))
  1558. {
  1559. return new JsonMemberInfo()
  1560. {
  1561. Member = mi,
  1562. JsonKey = mi.Name.Substring(0, 1).ToLower() + mi.Name.Substring(1),
  1563. };
  1564. }
  1565. return null;
  1566. });
  1567. return ri;
  1568. }
  1569. });
  1570. }
  1571. public static ReflectionInfo CreateReflectionInfo(Type type, Func<MemberInfo, JsonMemberInfo> callback)
  1572. {
  1573. // Work out properties and fields
  1574. var members = Utils.GetAllFieldsAndProperties(type).Select(x => callback(x)).Where(x => x != null).ToList();
  1575. // Anything with KeepInstance must be a reference type
  1576. var invalid = members.FirstOrDefault(x => x.KeepInstance && x.MemberType.IsValueType);
  1577. if (invalid!=null)
  1578. {
  1579. throw new InvalidOperationException(string.Format("KeepInstance=true can only be applied to reference types ({0}.{1})", type.FullName, invalid.Member));
  1580. }
  1581. // Must have some members
  1582. if (!members.Any())
  1583. return null;
  1584. // Create reflection info
  1585. return new ReflectionInfo() { Members = members };
  1586. }
  1587. }
  1588. public class ThreadSafeCache<TKey, TValue>
  1589. {
  1590. public ThreadSafeCache()
  1591. {
  1592. }
  1593. public TValue Get(TKey key, Func<TValue> createIt)
  1594. {
  1595. // Check if already exists
  1596. _lock.EnterReadLock();
  1597. try
  1598. {
  1599. TValue val;
  1600. if (_map.TryGetValue(key, out val))
  1601. return val;
  1602. }
  1603. finally
  1604. {
  1605. _lock.ExitReadLock();
  1606. }
  1607. // Nope, take lock and try again
  1608. _lock.EnterWriteLock();
  1609. try
  1610. {
  1611. // Check again before creating it
  1612. TValue val;
  1613. if (!_map.TryGetValue(key, out val))
  1614. {
  1615. // Store the new one
  1616. val = createIt();
  1617. _map[key] = val;
  1618. }
  1619. return val;
  1620. }
  1621. finally
  1622. {
  1623. _lock.ExitWriteLock();
  1624. }
  1625. }
  1626. public bool TryGetValue(TKey key, out TValue val)
  1627. {
  1628. _lock.EnterReadLock();
  1629. try
  1630. {
  1631. return _map.TryGetValue(key, out val);
  1632. }
  1633. finally
  1634. {
  1635. _lock.ExitReadLock();
  1636. }
  1637. }
  1638. public void Set(TKey key, TValue value)
  1639. {
  1640. _lock.EnterWriteLock();
  1641. try
  1642. {
  1643. _map[key] = value;
  1644. }
  1645. finally
  1646. {
  1647. _lock.ExitWriteLock();
  1648. }
  1649. }
  1650. Dictionary<TKey, TValue> _map = new Dictionary<TKey,TValue>();
  1651. ReaderWriterLockSlim _lock = new ReaderWriterLockSlim();
  1652. }
  1653. internal static class Utils
  1654. {
  1655. // Get all fields and properties of a type
  1656. public static IEnumerable<MemberInfo> GetAllFieldsAndProperties(Type t)
  1657. {
  1658. if (t == null)
  1659. return Enumerable.Empty<FieldInfo>();
  1660. BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly;
  1661. return t.GetMembers(flags).Where(x => x is FieldInfo || x is PropertyInfo).Concat(GetAllFieldsAndProperties(t.BaseType));
  1662. }
  1663. public static Type FindGenericInterface(Type type, Type tItf)
  1664. {
  1665. foreach (var t in type.GetInterfaces())
  1666. {
  1667. // Is this a generic list?
  1668. if (t.IsGenericType && t.GetGenericTypeDefinition() == tItf)
  1669. return t;
  1670. }
  1671. return null;
  1672. }
  1673. public static bool IsPublic(MemberInfo mi)
  1674. {
  1675. // Public field
  1676. var fi = mi as FieldInfo;
  1677. if (fi != null)
  1678. return fi.IsPublic;
  1679. // Public property
  1680. // (We only check the get method so we can work with anonymous types)
  1681. var pi = mi as PropertyInfo;
  1682. if (pi != null)
  1683. {
  1684. var gm = pi.GetGetMethod(true);
  1685. return (gm != null && gm.IsPublic);
  1686. }
  1687. return false;
  1688. }
  1689. public static Type ResolveInterfaceToClass(Type tItf)
  1690. {
  1691. // Generic type
  1692. if (tItf.IsGenericType)
  1693. {
  1694. var genDef = tItf.GetGenericTypeDefinition();
  1695. // IList<> -> List<>
  1696. if (genDef == typeof(IList<>))
  1697. {
  1698. return typeof(List<>).MakeGenericType(tItf.GetGenericArguments());
  1699. }
  1700. // IDictionary<string,> -> Dictionary<string,>
  1701. if (genDef == typeof(IDictionary<,>) && tItf.GetGenericArguments()[0] == typeof(string))
  1702. {
  1703. return typeof(Dictionary<,>).MakeGenericType(tItf.GetGenericArguments());
  1704. }
  1705. }
  1706. // IEnumerable -> List<object>
  1707. if (tItf == typeof(IEnumerable))
  1708. return typeof(List<object>);
  1709. // IDicitonary -> Dictionary<string,object>
  1710. if (tItf == typeof(IDictionary))
  1711. return typeof(Dictionary<string, object>);
  1712. return tItf;
  1713. }
  1714. public static long ToUnixMilliseconds(DateTime This)
  1715. {
  1716. return (long)This.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
  1717. }
  1718. public static DateTime FromUnixMilliseconds(long timeStamp)
  1719. {
  1720. return new DateTime(1970, 1, 1).AddMilliseconds(timeStamp);
  1721. }
  1722. }
  1723. public class Tokenizer
  1724. {
  1725. public Tokenizer(TextReader r, JsonOptions options)
  1726. {
  1727. _underlying = r;
  1728. _options = options;
  1729. FillBuffer();
  1730. NextChar();
  1731. NextToken();
  1732. }
  1733. private JsonOptions _options;
  1734. private StringBuilder _sb = new StringBuilder();
  1735. private TextReader _underlying;
  1736. private char[] _buf = new char[4096];
  1737. private int _pos;
  1738. private int _bufUsed;
  1739. private StringBuilder _rewindBuffer;
  1740. private int _rewindBufferPos;
  1741. private JsonLineOffset _currentCharPos;
  1742. private char _currentChar;
  1743. private Stack<ReaderState> _bookmarks = new Stack<ReaderState>();
  1744. public JsonLineOffset CurrentTokenPosition;
  1745. public Token CurrentToken;
  1746. public LiteralKind LiteralKind;
  1747. public string String;
  1748. public object LiteralValue
  1749. {
  1750. get
  1751. {
  1752. if (CurrentToken != Token.Literal)
  1753. throw new InvalidOperationException("token is not a literal");
  1754. switch (LiteralKind)
  1755. {
  1756. case LiteralKind.Null: return null;
  1757. case LiteralKind.False: return false;
  1758. case LiteralKind.True: return true;
  1759. case LiteralKind.String: return String;
  1760. case LiteralKind.SignedInteger: return long.Parse(String, CultureInfo.InvariantCulture);
  1761. case LiteralKind.UnsignedInteger: return ulong.Parse(String, CultureInfo.InvariantCulture);
  1762. case LiteralKind.FloatingPoint: return double.Parse(String, CultureInfo.InvariantCulture);
  1763. }
  1764. return null;
  1765. }
  1766. }
  1767. public Type LiteralType
  1768. {
  1769. get
  1770. {
  1771. if (CurrentToken != Token.Literal)
  1772. throw new InvalidOperationException("token is not a literal");
  1773. switch (LiteralKind)
  1774. {
  1775. case LiteralKind.Null: return typeof(Object);
  1776. case LiteralKind.False: return typeof(Boolean);
  1777. case LiteralKind.True: return typeof(Boolean);
  1778. case LiteralKind.String: return typeof(string);
  1779. case LiteralKind.SignedInteger: return typeof(long);
  1780. case LiteralKind.UnsignedInteger: return typeof(ulong);
  1781. case LiteralKind.FloatingPoint: return typeof(double);
  1782. }
  1783. return null;
  1784. }
  1785. }
  1786. // This object represents the entire state of the reader and is used for rewind
  1787. struct ReaderState
  1788. {
  1789. public ReaderState(Tokenizer tokenizer)
  1790. {
  1791. _currentCharPos = tokenizer._currentCharPos;
  1792. _currentChar = tokenizer._currentChar;
  1793. _string = tokenizer.String;
  1794. _literalKind = tokenizer.LiteralKind;
  1795. _rewindBufferPos = tokenizer._rewindBufferPos;
  1796. _currentTokenPos = tokenizer.CurrentTokenPosition;
  1797. _currentToken = tokenizer.CurrentToken;
  1798. }
  1799. public void Apply(Tokenizer tokenizer)
  1800. {
  1801. tokenizer._currentCharPos = _currentCharPos;
  1802. tokenizer._currentChar = _currentChar;
  1803. tokenizer._rewindBufferPos = _rewindBufferPos;
  1804. tokenizer.CurrentToken = _currentToken;
  1805. tokenizer.CurrentTokenPosition = _currentTokenPos;
  1806. tokenizer.String = _string;
  1807. tokenizer.LiteralKind = _literalKind;
  1808. }
  1809. private JsonLineOffset _currentCharPos;
  1810. private JsonLineOffset _currentTokenPos;
  1811. private char _currentChar;
  1812. private Token _currentToken;
  1813. private LiteralKind _literalKind;
  1814. private string _string;
  1815. private int _rewindBufferPos;
  1816. }
  1817. // Create a rewind bookmark
  1818. public void CreateBookmark()
  1819. {
  1820. _bookmarks.Push(new ReaderState(this));
  1821. if (_rewindBuffer == null)
  1822. {
  1823. _rewindBuffer = new StringBuilder();
  1824. _rewindBufferPos = 0;
  1825. }
  1826. }
  1827. // Discard bookmark
  1828. public void DiscardBookmark()
  1829. {
  1830. _bookmarks.Pop();
  1831. if (_bookmarks.Count == 0)
  1832. {
  1833. _rewindBuffer = null;
  1834. _rewindBufferPos = 0;
  1835. }
  1836. }
  1837. // Rewind to a bookmark
  1838. public void RewindToBookmark()
  1839. {
  1840. _bookmarks.Pop().Apply(this);
  1841. }
  1842. // Fill buffer by reading from underlying TextReader
  1843. void FillBuffer()
  1844. {
  1845. _bufUsed = _underlying.Read(_buf, 0, _buf.Length);
  1846. _pos = 0;
  1847. }
  1848. // Get the next character from the input stream
  1849. // (this function could be extracted into a few different methods, but is mostly inlined
  1850. // for performance - yes it makes a difference)
  1851. public char NextChar()
  1852. {
  1853. if (_rewindBuffer == null)
  1854. {
  1855. if (_pos >= _bufUsed)
  1856. {
  1857. if (_bufUsed > 0)
  1858. {
  1859. FillBuffer();
  1860. }
  1861. if (_bufUsed == 0)
  1862. {
  1863. return _currentChar = '\0';
  1864. }
  1865. }
  1866. // Next
  1867. _currentCharPos.Offset++;
  1868. return _currentChar = _buf[_pos++];
  1869. }
  1870. if (_rewindBufferPos < _rewindBuffer.Length)
  1871. {
  1872. _currentCharPos.Offset++;
  1873. return _currentChar = _rewindBuffer[_rewindBufferPos++];
  1874. }
  1875. else
  1876. {
  1877. if (_pos >= _bufUsed && _bufUsed > 0)
  1878. FillBuffer();
  1879. _currentChar = _bufUsed == 0 ? '\0' : _buf[_pos++];
  1880. _rewindBuffer.Append(_currentChar);
  1881. _rewindBufferPos++;
  1882. _currentCharPos.Offset++;
  1883. return _currentChar;
  1884. }
  1885. }
  1886. // Read the next token from the input stream
  1887. // (Mostly inline for performance)
  1888. public void NextToken()
  1889. {
  1890. while (true)
  1891. {
  1892. // Skip whitespace and handle line numbers
  1893. while (true)
  1894. {
  1895. if (_currentChar == '\r')
  1896. {
  1897. if (NextChar() == '\n')
  1898. {
  1899. NextChar();
  1900. }
  1901. _currentCharPos.Line++;
  1902. _currentCharPos.Offset = 0;
  1903. }
  1904. else if (_currentChar == '\n')
  1905. {
  1906. if (NextChar() == '\r')
  1907. {
  1908. NextChar();
  1909. }
  1910. _currentCharPos.Line++;
  1911. _currentCharPos.Offset = 0;
  1912. }
  1913. else if (_currentChar == ' ')
  1914. {
  1915. NextChar();
  1916. }
  1917. else if (_currentChar == '\t')
  1918. {
  1919. NextChar();
  1920. }
  1921. else
  1922. break;
  1923. }
  1924. // Remember position of token
  1925. CurrentTokenPosition = _currentCharPos;
  1926. // Handle common characters first
  1927. switch (_currentChar)
  1928. {
  1929. case '/':
  1930. // Comments not support in strict mode
  1931. if ((_options & JsonOptions.StrictParser) != 0)
  1932. {
  1933. throw new InvalidDataException(string.Format("syntax error, unexpected character '{0}'", _currentChar));
  1934. }
  1935. // Process comment
  1936. NextChar();
  1937. switch (_currentChar)
  1938. {
  1939. case '/':
  1940. NextChar();
  1941. while (_currentChar!='\0' && _currentChar != '\r' && _currentChar != '\n')
  1942. {
  1943. NextChar();
  1944. }
  1945. break;
  1946. case '*':
  1947. bool endFound = false;
  1948. while (!endFound && _currentChar!='\0')
  1949. {
  1950. if (_currentChar == '*')
  1951. {
  1952. NextChar();
  1953. if (_currentChar == '/')
  1954. {
  1955. endFound = true;
  1956. }
  1957. }
  1958. NextChar();
  1959. }
  1960. break;
  1961. default:
  1962. throw new InvalidDataException("syntax error, unexpected character after slash");
  1963. }
  1964. continue;
  1965. case '\"':
  1966. case '\'':
  1967. {
  1968. _sb.Length = 0;
  1969. var quoteKind = _currentChar;
  1970. NextChar();
  1971. while (_currentChar!='\0')
  1972. {
  1973. if (_currentChar == '\\')
  1974. {
  1975. NextChar();
  1976. var escape = _currentChar;
  1977. switch (escape)
  1978. {
  1979. case '\"': _sb.Append('\"'); break;
  1980. case '\\': _sb.Append('\\'); break;
  1981. case '/': _sb.Append('/'); break;
  1982. case 'b': _sb.Append('\b'); break;
  1983. case 'f': _sb.Append('\f'); break;
  1984. case 'n': _sb.Append('\n'); break;
  1985. case 'r': _sb.Append('\r'); break;
  1986. case 't': _sb.Append('\t'); break;
  1987. case 'u':
  1988. var sbHex = new StringBuilder();
  1989. for (int i = 0; i < 4; i++)
  1990. {
  1991. NextChar();
  1992. sbHex.Append(_currentChar);
  1993. }
  1994. _sb.Append((char)Convert.ToUInt16(sbHex.ToString(), 16));
  1995. break;
  1996. default:
  1997. throw new InvalidDataException(string.Format("Invalid escape sequence in string literal: '\\{0}'", _currentChar));
  1998. }
  1999. }
  2000. else if (_currentChar == quoteKind)
  2001. {
  2002. String = _sb.ToString();
  2003. CurrentToken = Token.Literal;
  2004. LiteralKind = LiteralKind.String;
  2005. NextChar();
  2006. return;
  2007. }
  2008. else
  2009. {
  2010. _sb.Append(_currentChar);
  2011. }
  2012. NextChar();
  2013. }
  2014. throw new InvalidDataException("syntax error, unterminated string literal");
  2015. }
  2016. case '{': CurrentToken = Token.OpenBrace; NextChar(); return;
  2017. case '}': CurrentToken = Token.CloseBrace; NextChar(); return;
  2018. case '[': CurrentToken = Token.OpenSquare; NextChar(); return;
  2019. case ']': CurrentToken = Token.CloseSquare; NextChar(); return;
  2020. case '=': CurrentToken = Token.Equal; NextChar(); return;
  2021. case ':': CurrentToken = Token.Colon; NextChar(); return;
  2022. case ';': CurrentToken = Token.SemiColon; NextChar(); return;
  2023. case ',': CurrentToken = Token.Comma; NextChar(); return;
  2024. case '\0': CurrentToken = Token.EOF; return;
  2025. }
  2026. // Number?
  2027. if (char.IsDigit(_currentChar) || _currentChar == '-')
  2028. {
  2029. TokenizeNumber();
  2030. return;
  2031. }
  2032. // Identifier? (checked for after everything else as identifiers are actually quite rare in valid json)
  2033. if (Char.IsLetter(_currentChar) || _currentChar == '_' || _currentChar == '$')
  2034. {
  2035. // Find end of identifier
  2036. _sb.Length = 0;
  2037. while (Char.IsLetterOrDigit(_currentChar) || _currentChar == '_' || _currentChar == '$')
  2038. {
  2039. _sb.Append(_currentChar);
  2040. NextChar();
  2041. }
  2042. String = _sb.ToString();
  2043. // Handle special identifiers
  2044. switch (String)
  2045. {
  2046. case "true":
  2047. LiteralKind = LiteralKind.True;
  2048. CurrentToken = Token.Literal;
  2049. return;
  2050. case "false":
  2051. LiteralKind = LiteralKind.False;
  2052. CurrentToken = Token.Literal;
  2053. return;
  2054. case "null":
  2055. LiteralKind = LiteralKind.Null;
  2056. CurrentToken = Token.Literal;
  2057. return;
  2058. }
  2059. CurrentToken = Token.Identifier;
  2060. return;
  2061. }
  2062. // What the?
  2063. throw new InvalidDataException(string.Format("syntax error, unexpected character '{0}'", _currentChar));
  2064. }
  2065. }
  2066. // Parse a sequence of characters that could make up a valid number
  2067. // For performance, we don't actually parse it into a number yet. When using PetaJsonEmit we parse
  2068. // later, directly into a value type to avoid boxing
  2069. private void TokenizeNumber()
  2070. {
  2071. _sb.Length = 0;
  2072. // Leading negative sign
  2073. bool signed = false;
  2074. if (_currentChar == '-')
  2075. {
  2076. signed = true;
  2077. _sb.Append(_currentChar);
  2078. NextChar();
  2079. }
  2080. // Hex prefix?
  2081. bool hex = false;
  2082. if (_currentChar == '0' && (_options & JsonOptions.StrictParser)==0)
  2083. {
  2084. _sb.Append(_currentChar);
  2085. NextChar();
  2086. if (_currentChar == 'x' || _currentChar == 'X')
  2087. {
  2088. _sb.Append(_currentChar);
  2089. NextChar();
  2090. hex = true;
  2091. }
  2092. }
  2093. // Process characters, but vaguely figure out what type it is
  2094. bool cont = true;
  2095. bool fp = false;
  2096. while (cont)
  2097. {
  2098. switch (_currentChar)
  2099. {
  2100. case '0':
  2101. case '1':
  2102. case '2':
  2103. case '3':
  2104. case '4':
  2105. case '5':
  2106. case '6':
  2107. case '7':
  2108. case '8':
  2109. case '9':
  2110. _sb.Append(_currentChar);
  2111. NextChar();
  2112. break;
  2113. case 'A':
  2114. case 'a':
  2115. case 'B':
  2116. case 'b':
  2117. case 'C':
  2118. case 'c':
  2119. case 'D':
  2120. case 'd':
  2121. case 'F':
  2122. case 'f':
  2123. if (!hex)
  2124. cont = false;
  2125. else
  2126. {
  2127. _sb.Append(_currentChar);
  2128. NextChar();
  2129. }
  2130. break;
  2131. case '.':
  2132. if (hex)
  2133. {
  2134. cont = false;
  2135. }
  2136. else
  2137. {
  2138. fp = true;
  2139. _sb.Append(_currentChar);
  2140. NextChar();
  2141. }
  2142. break;
  2143. case 'E':
  2144. case 'e':
  2145. if (!hex)
  2146. {
  2147. fp = true;
  2148. _sb.Append(_currentChar);
  2149. NextChar();
  2150. if (_currentChar == '+' || _currentChar == '-')
  2151. {
  2152. _sb.Append(_currentChar);
  2153. NextChar();
  2154. }
  2155. }
  2156. break;
  2157. default:
  2158. cont = false;
  2159. break;
  2160. }
  2161. }
  2162. if (char.IsLetter(_currentChar))
  2163. throw new InvalidDataException(string.Format("syntax error, invalid character following number '{0}'", _sb.ToString()));
  2164. // Setup token
  2165. String = _sb.ToString();
  2166. CurrentToken = Token.Literal;
  2167. // Setup literal kind
  2168. if (fp)
  2169. {
  2170. LiteralKind = LiteralKind.FloatingPoint;
  2171. }
  2172. else if (signed)
  2173. {
  2174. LiteralKind = LiteralKind.SignedInteger;
  2175. }
  2176. else
  2177. {
  2178. LiteralKind = LiteralKind.UnsignedInteger;
  2179. }
  2180. }
  2181. // Check the current token, throw exception if mismatch
  2182. public void Check(Token tokenRequired)
  2183. {
  2184. if (tokenRequired != CurrentToken)
  2185. {
  2186. throw new InvalidDataException(string.Format("syntax error, expected {0} found {1}", tokenRequired, CurrentToken));
  2187. }
  2188. }
  2189. // Skip token which must match
  2190. public void Skip(Token tokenRequired)
  2191. {
  2192. Check(tokenRequired);
  2193. NextToken();
  2194. }
  2195. // Skip token if it matches
  2196. public bool SkipIf(Token tokenRequired)
  2197. {
  2198. if (tokenRequired == CurrentToken)
  2199. {
  2200. NextToken();
  2201. return true;
  2202. }
  2203. return false;
  2204. }
  2205. }
  2206. #if !PETAJSON_NO_EMIT
  2207. static class Emit
  2208. {
  2209. // Generates a function that when passed an object of specified type, renders it to an IJsonReader
  2210. public static Action<IJsonWriter, object> MakeFormatter(Type type)
  2211. {
  2212. var formatJson = ReflectionInfo.FindFormatJson(type);
  2213. if (formatJson != null)
  2214. {
  2215. var method = new DynamicMethod("invoke_formatJson", null, new Type[] { typeof(IJsonWriter), typeof(Object) }, true);
  2216. var il = method.GetILGenerator();
  2217. if (formatJson.ReturnType == typeof(string))
  2218. {
  2219. // w.WriteStringLiteral(o.FormatJson())
  2220. il.Emit(OpCodes.Ldarg_0);
  2221. il.Emit(OpCodes.Ldarg_1);
  2222. il.Emit(OpCodes.Unbox, type);
  2223. il.Emit(OpCodes.Call, formatJson);
  2224. il.Emit(OpCodes.Callvirt, typeof(IJsonWriter).GetMethod("WriteStringLiteral"));
  2225. }
  2226. else
  2227. {
  2228. // o.FormatJson(w);
  2229. il.Emit(OpCodes.Ldarg_1);
  2230. il.Emit(type.IsValueType ? OpCodes.Unbox : OpCodes.Castclass, type);
  2231. il.Emit(OpCodes.Ldarg_0);
  2232. il.Emit(type.IsValueType ? OpCodes.Call : OpCodes.Callvirt, formatJson);
  2233. }
  2234. il.Emit(OpCodes.Ret);
  2235. return (Action<IJsonWriter, object>)method.CreateDelegate(typeof(Action<IJsonWriter, object>));
  2236. }
  2237. else
  2238. {
  2239. // Get the reflection info for this type
  2240. var ri = ReflectionInfo.GetReflectionInfo(type);
  2241. if (ri == null)
  2242. return null;
  2243. // Create a dynamic method that can do the work
  2244. var method = new DynamicMethod("dynamic_formatter", null, new Type[] { typeof(IJsonWriter), typeof(object) }, true);
  2245. var il = method.GetILGenerator();
  2246. // Cast/unbox the target object and store in local variable
  2247. var locTypedObj = il.DeclareLocal(type);
  2248. il.Emit(OpCodes.Ldarg_1);
  2249. il.Emit(type.IsValueType ? OpCodes.Unbox_Any : OpCodes.Castclass, type);
  2250. il.Emit(OpCodes.Stloc, locTypedObj);
  2251. // Get Invariant CultureInfo (since we'll probably be needing this)
  2252. var locInvariant = il.DeclareLocal(typeof(IFormatProvider));
  2253. il.Emit(OpCodes.Call, typeof(CultureInfo).GetProperty("InvariantCulture").GetGetMethod());
  2254. il.Emit(OpCodes.Stloc, locInvariant);
  2255. // These are the types we'll call .ToString(Culture.InvariantCulture) on
  2256. var toStringTypes = new Type[] {
  2257. typeof(int), typeof(uint), typeof(long), typeof(ulong),
  2258. typeof(short), typeof(ushort), typeof(decimal),
  2259. typeof(byte), typeof(sbyte)
  2260. };
  2261. // Theses types we also generate for
  2262. var otherSupportedTypes = new Type[] {
  2263. typeof(double), typeof(float), typeof(string), typeof(char)
  2264. };
  2265. // Call IJsonWriting if implemented
  2266. if (typeof(IJsonWriting).IsAssignableFrom(type))
  2267. {
  2268. if (type.IsValueType)
  2269. {
  2270. il.Emit(OpCodes.Ldloca, locTypedObj);
  2271. il.Emit(OpCodes.Ldarg_0);
  2272. il.Emit(OpCodes.Call, type.GetInterfaceMap(typeof(IJsonWriting)).TargetMethods[0]);
  2273. }
  2274. else
  2275. {
  2276. il.Emit(OpCodes.Ldloc, locTypedObj);
  2277. il.Emit(OpCodes.Castclass, typeof(IJsonWriting));
  2278. il.Emit(OpCodes.Ldarg_0);
  2279. il.Emit(OpCodes.Callvirt, typeof(IJsonWriting).GetMethod("OnJsonWriting", new Type[] { typeof(IJsonWriter) }));
  2280. }
  2281. }
  2282. // Process all members
  2283. foreach (var m in ri.Members)
  2284. {
  2285. // Dont save deprecated properties
  2286. if (m.Deprecated)
  2287. {
  2288. continue;
  2289. }
  2290. // Ignore write only properties
  2291. var pi = m.Member as PropertyInfo;
  2292. if (pi != null && pi.GetGetMethod(true) == null)
  2293. {
  2294. continue;
  2295. }
  2296. // Write the Json key
  2297. il.Emit(OpCodes.Ldarg_0);
  2298. il.Emit(OpCodes.Ldstr, m.JsonKey);
  2299. il.Emit(OpCodes.Callvirt, typeof(IJsonWriter).GetMethod("WriteKeyNoEscaping", new Type[] { typeof(string) }));
  2300. // Load the writer
  2301. il.Emit(OpCodes.Ldarg_0);
  2302. // Get the member type
  2303. var memberType = m.MemberType;
  2304. // Load the target object
  2305. if (type.IsValueType)
  2306. {
  2307. il.Emit(OpCodes.Ldloca, locTypedObj);
  2308. }
  2309. else
  2310. {
  2311. il.Emit(OpCodes.Ldloc, locTypedObj);
  2312. }
  2313. // Work out if we need the value or it's address on the stack
  2314. bool NeedValueAddress = (memberType.IsValueType && (toStringTypes.Contains(memberType) || otherSupportedTypes.Contains(memberType)));
  2315. if (Nullable.GetUnderlyingType(memberType) != null)
  2316. {
  2317. NeedValueAddress = true;
  2318. }
  2319. // Property?
  2320. if (pi != null)
  2321. {
  2322. // Call property's get method
  2323. if (type.IsValueType)
  2324. il.Emit(OpCodes.Call, pi.GetGetMethod(true));
  2325. else
  2326. il.Emit(OpCodes.Callvirt, pi.GetGetMethod(true));
  2327. // If we need the address then store in a local and take it's address
  2328. if (NeedValueAddress)
  2329. {
  2330. var locTemp = il.DeclareLocal(memberType);
  2331. il.Emit(OpCodes.Stloc, locTemp);
  2332. il.Emit(OpCodes.Ldloca, locTemp);
  2333. }
  2334. }
  2335. // Field?
  2336. var fi = m.Member as FieldInfo;
  2337. if (fi != null)
  2338. {
  2339. if (NeedValueAddress)
  2340. {
  2341. il.Emit(OpCodes.Ldflda, fi);
  2342. }
  2343. else
  2344. {
  2345. il.Emit(OpCodes.Ldfld, fi);
  2346. }
  2347. }
  2348. Label? lblFinished = null;
  2349. // Is it a nullable type?
  2350. var typeUnderlying = Nullable.GetUnderlyingType(memberType);
  2351. if (typeUnderlying != null)
  2352. {
  2353. // Duplicate the address so we can call get_HasValue() and then get_Value()
  2354. il.Emit(OpCodes.Dup);
  2355. // Define some labels
  2356. var lblHasValue = il.DefineLabel();
  2357. lblFinished = il.DefineLabel();
  2358. // Call has_Value
  2359. il.Emit(OpCodes.Call, memberType.GetProperty("HasValue").GetGetMethod());
  2360. il.Emit(OpCodes.Brtrue, lblHasValue);
  2361. // No value, write "null:
  2362. il.Emit(OpCodes.Pop);
  2363. il.Emit(OpCodes.Ldstr, "null");
  2364. il.Emit(OpCodes.Callvirt, typeof(IJsonWriter).GetMethod("WriteRaw", new Type[] { typeof(string) }));
  2365. il.Emit(OpCodes.Br_S, lblFinished.Value);
  2366. // Get it's value
  2367. il.MarkLabel(lblHasValue);
  2368. il.Emit(OpCodes.Call, memberType.GetProperty("Value").GetGetMethod());
  2369. // Switch to the underlying type from here on
  2370. memberType = typeUnderlying;
  2371. NeedValueAddress = (memberType.IsValueType && (toStringTypes.Contains(memberType) || otherSupportedTypes.Contains(memberType)));
  2372. // Work out again if we need the address of the value
  2373. if (NeedValueAddress)
  2374. {
  2375. var locTemp = il.DeclareLocal(memberType);
  2376. il.Emit(OpCodes.Stloc, locTemp);
  2377. il.Emit(OpCodes.Ldloca, locTemp);
  2378. }
  2379. }
  2380. // ToString()
  2381. if (toStringTypes.Contains(memberType))
  2382. {
  2383. // Convert to string
  2384. il.Emit(OpCodes.Ldloc, locInvariant);
  2385. il.Emit(OpCodes.Call, memberType.GetMethod("ToString", new Type[] { typeof(IFormatProvider) }));
  2386. il.Emit(OpCodes.Callvirt, typeof(IJsonWriter).GetMethod("WriteRaw", new Type[] { typeof(string) }));
  2387. }
  2388. // ToString("R")
  2389. else if (memberType == typeof(float) || memberType == typeof(double))
  2390. {
  2391. il.Emit(OpCodes.Ldstr, "R");
  2392. il.Emit(OpCodes.Ldloc, locInvariant);
  2393. il.Emit(OpCodes.Call, memberType.GetMethod("ToString", new Type[] { typeof(string), typeof(IFormatProvider) }));
  2394. il.Emit(OpCodes.Callvirt, typeof(IJsonWriter).GetMethod("WriteRaw", new Type[] { typeof(string) }));
  2395. }
  2396. // String?
  2397. else if (memberType == typeof(string))
  2398. {
  2399. il.Emit(OpCodes.Callvirt, typeof(IJsonWriter).GetMethod("WriteStringLiteral", new Type[] { typeof(string) }));
  2400. }
  2401. // Char?
  2402. else if (memberType == typeof(char))
  2403. {
  2404. il.Emit(OpCodes.Call, memberType.GetMethod("ToString", new Type[] { }));
  2405. il.Emit(OpCodes.Callvirt, typeof(IJsonWriter).GetMethod("WriteStringLiteral", new Type[] { typeof(string) }));
  2406. }
  2407. // Bool?
  2408. else if (memberType == typeof(bool))
  2409. {
  2410. var lblTrue = il.DefineLabel();
  2411. var lblCont = il.DefineLabel();
  2412. il.Emit(OpCodes.Brtrue_S, lblTrue);
  2413. il.Emit(OpCodes.Ldstr, "false");
  2414. il.Emit(OpCodes.Br_S, lblCont);
  2415. il.MarkLabel(lblTrue);
  2416. il.Emit(OpCodes.Ldstr, "true");
  2417. il.MarkLabel(lblCont);
  2418. il.Emit(OpCodes.Callvirt, typeof(IJsonWriter).GetMethod("WriteRaw", new Type[] { typeof(string) }));
  2419. }
  2420. // NB: We don't support DateTime as it's format can be changed
  2421. else
  2422. {
  2423. // Unsupported type, pass through
  2424. if (memberType.IsValueType)
  2425. {
  2426. il.Emit(OpCodes.Box, memberType);
  2427. }
  2428. il.Emit(OpCodes.Callvirt, typeof(IJsonWriter).GetMethod("WriteValue", new Type[] { typeof(object) }));
  2429. }
  2430. if (lblFinished.HasValue)
  2431. il.MarkLabel(lblFinished.Value);
  2432. }
  2433. // Call IJsonWritten
  2434. if (typeof(IJsonWritten).IsAssignableFrom(type))
  2435. {
  2436. if (type.IsValueType)
  2437. {
  2438. il.Emit(OpCodes.Ldloca, locTypedObj);
  2439. il.Emit(OpCodes.Ldarg_0);
  2440. il.Emit(OpCodes.Call, type.GetInterfaceMap(typeof(IJsonWritten)).TargetMethods[0]);
  2441. }
  2442. else
  2443. {
  2444. il.Emit(OpCodes.Ldloc, locTypedObj);
  2445. il.Emit(OpCodes.Castclass, typeof(IJsonWriting));
  2446. il.Emit(OpCodes.Ldarg_0);
  2447. il.Emit(OpCodes.Callvirt, typeof(IJsonWriting).GetMethod("OnJsonWritten", new Type[] { typeof(IJsonWriter) }));
  2448. }
  2449. }
  2450. // Done!
  2451. il.Emit(OpCodes.Ret);
  2452. var impl = (Action<IJsonWriter, object>)method.CreateDelegate(typeof(Action<IJsonWriter, object>));
  2453. // Wrap it in a call to WriteDictionary
  2454. return (w, obj) =>
  2455. {
  2456. w.WriteDictionary(() =>
  2457. {
  2458. impl(w, obj);
  2459. });
  2460. };
  2461. }
  2462. }
  2463. // Pseudo box lets us pass a value type by reference. Used during
  2464. // deserialization of value types.
  2465. interface IPseudoBox
  2466. {
  2467. object GetValue();
  2468. }
  2469. [Obfuscation(Exclude = true, ApplyToMembers = true)]
  2470. class PseudoBox<T> : IPseudoBox where T : struct
  2471. {
  2472. public T value = default(T);
  2473. object IPseudoBox.GetValue() { return value; }
  2474. }
  2475. // Make a parser for value types
  2476. public static Func<IJsonReader, Type, object> MakeParser(Type type)
  2477. {
  2478. System.Diagnostics.Debug.Assert(type.IsValueType);
  2479. // ParseJson method?
  2480. var parseJson = ReflectionInfo.FindParseJson(type);
  2481. if (parseJson != null)
  2482. {
  2483. if (parseJson.GetParameters()[0].ParameterType == typeof(IJsonReader))
  2484. {
  2485. var method = new DynamicMethod("invoke_ParseJson", typeof(Object), new Type[] { typeof(IJsonReader), typeof(Type) }, true);
  2486. var il = method.GetILGenerator();
  2487. il.Emit(OpCodes.Ldarg_0);
  2488. il.Emit(OpCodes.Call, parseJson);
  2489. il.Emit(OpCodes.Box, type);
  2490. il.Emit(OpCodes.Ret);
  2491. return (Func<IJsonReader,Type,object>)method.CreateDelegate(typeof(Func<IJsonReader,Type,object>));
  2492. }
  2493. else
  2494. {
  2495. var method = new DynamicMethod("invoke_ParseJson", typeof(Object), new Type[] { typeof(string) }, true);
  2496. var il = method.GetILGenerator();
  2497. il.Emit(OpCodes.Ldarg_0);
  2498. il.Emit(OpCodes.Call, parseJson);
  2499. il.Emit(OpCodes.Box, type);
  2500. il.Emit(OpCodes.Ret);
  2501. var invoke = (Func<string, object>)method.CreateDelegate(typeof(Func<string, object>));
  2502. return (r, t) =>
  2503. {
  2504. if (r.GetLiteralKind() == LiteralKind.String)
  2505. {
  2506. var o = invoke(r.GetLiteralString());
  2507. r.NextToken();
  2508. return o;
  2509. }
  2510. throw new InvalidDataException(string.Format("Expected string literal for type {0}", type.FullName));
  2511. };
  2512. }
  2513. }
  2514. else
  2515. {
  2516. // Get the reflection info for this type
  2517. var ri = ReflectionInfo.GetReflectionInfo(type);
  2518. if (ri == null)
  2519. return null;
  2520. // We'll create setters for each property/field
  2521. var setters = new Dictionary<string, Action<IJsonReader, object>>();
  2522. // Store the value in a pseudo box until it's fully initialized
  2523. var boxType = typeof(PseudoBox<>).MakeGenericType(type);
  2524. // Process all members
  2525. foreach (var m in ri.Members)
  2526. {
  2527. // Ignore write only properties
  2528. var pi = m.Member as PropertyInfo;
  2529. var fi = m.Member as FieldInfo;
  2530. if (pi != null && pi.GetSetMethod(true) == null)
  2531. {
  2532. continue;
  2533. }
  2534. // Create a dynamic method that can do the work
  2535. var method = new DynamicMethod("dynamic_parser", null, new Type[] { typeof(IJsonReader), typeof(object) }, true);
  2536. var il = method.GetILGenerator();
  2537. // Load the target
  2538. il.Emit(OpCodes.Ldarg_1);
  2539. il.Emit(OpCodes.Castclass, boxType);
  2540. il.Emit(OpCodes.Ldflda, boxType.GetField("value"));
  2541. // Get the value
  2542. GenerateGetJsonValue(m, il);
  2543. // Assign it
  2544. if (pi != null)
  2545. il.Emit(OpCodes.Call, pi.GetSetMethod(true));
  2546. if (fi != null)
  2547. il.Emit(OpCodes.Stfld, fi);
  2548. // Done
  2549. il.Emit(OpCodes.Ret);
  2550. // Store in the map of setters
  2551. setters.Add(m.JsonKey, (Action<IJsonReader, object>)method.CreateDelegate(typeof(Action<IJsonReader, object>)));
  2552. }
  2553. // Create helpers to invoke the interfaces (this is painful but avoids having to really box
  2554. // the value in order to call the interface).
  2555. Action<object, IJsonReader> invokeLoading = MakeInterfaceCall(type, typeof(IJsonLoading));
  2556. Action<object, IJsonReader> invokeLoaded = MakeInterfaceCall(type, typeof(IJsonLoaded));
  2557. Func<object, IJsonReader, string, bool> invokeField = MakeLoadFieldCall(type);
  2558. // Create the parser
  2559. Func<IJsonReader, Type, object> parser = (reader, Type) =>
  2560. {
  2561. // Create pseudobox (ie: new PseudoBox<Type>)
  2562. var box = DecoratingActivator.CreateInstance(boxType);
  2563. // Call IJsonLoading
  2564. if (invokeLoading != null)
  2565. invokeLoading(box, reader);
  2566. // Read the dictionary
  2567. reader.ParseDictionary(key =>
  2568. {
  2569. // Call IJsonLoadField
  2570. if (invokeField != null && invokeField(box, reader, key))
  2571. return;
  2572. // Get a setter and invoke it if found
  2573. Action<IJsonReader, object> setter;
  2574. if (setters.TryGetValue(key, out setter))
  2575. {
  2576. setter(reader, box);
  2577. }
  2578. });
  2579. // IJsonLoaded
  2580. if (invokeLoaded != null)
  2581. invokeLoaded(box, reader);
  2582. // Return the value
  2583. return ((IPseudoBox)box).GetValue();
  2584. };
  2585. // Done
  2586. return parser;
  2587. }
  2588. }
  2589. // Helper to make the call to a PsuedoBox value's IJsonLoading or IJsonLoaded
  2590. static Action<object, IJsonReader> MakeInterfaceCall(Type type, Type tItf)
  2591. {
  2592. // Interface supported?
  2593. if (!tItf.IsAssignableFrom(type))
  2594. return null;
  2595. // Resolve the box type
  2596. var boxType = typeof(PseudoBox<>).MakeGenericType(type);
  2597. // Create method
  2598. var method = new DynamicMethod("dynamic_invoke_" + tItf.Name, null, new Type[] { typeof(object), typeof(IJsonReader) }, true);
  2599. var il = method.GetILGenerator();
  2600. // Call interface method
  2601. il.Emit(OpCodes.Ldarg_0);
  2602. il.Emit(OpCodes.Castclass, boxType);
  2603. il.Emit(OpCodes.Ldflda, boxType.GetField("value"));
  2604. il.Emit(OpCodes.Ldarg_1);
  2605. il.Emit(OpCodes.Call, type.GetInterfaceMap(tItf).TargetMethods[0]);
  2606. il.Emit(OpCodes.Ret);
  2607. // Done
  2608. return (Action<object, IJsonReader>)method.CreateDelegate(typeof(Action<object, IJsonReader>));
  2609. }
  2610. // Similar to above but for IJsonLoadField
  2611. static Func<object, IJsonReader, string, bool> MakeLoadFieldCall(Type type)
  2612. {
  2613. // Interface supported?
  2614. var tItf = typeof(IJsonLoadField);
  2615. if (!tItf.IsAssignableFrom(type))
  2616. return null;
  2617. // Resolve the box type
  2618. var boxType = typeof(PseudoBox<>).MakeGenericType(type);
  2619. // Create method
  2620. var method = new DynamicMethod("dynamic_invoke_" + tItf.Name, typeof(bool), new Type[] { typeof(object), typeof(IJsonReader), typeof(string) }, true);
  2621. var il = method.GetILGenerator();
  2622. // Call interface method
  2623. il.Emit(OpCodes.Ldarg_0);
  2624. il.Emit(OpCodes.Castclass, boxType);
  2625. il.Emit(OpCodes.Ldflda, boxType.GetField("value"));
  2626. il.Emit(OpCodes.Ldarg_1);
  2627. il.Emit(OpCodes.Ldarg_2);
  2628. il.Emit(OpCodes.Call, type.GetInterfaceMap(tItf).TargetMethods[0]);
  2629. il.Emit(OpCodes.Ret);
  2630. // Done
  2631. return (Func<object, IJsonReader, string, bool>)method.CreateDelegate(typeof(Func<object, IJsonReader, string, bool>));
  2632. }
  2633. // Create an "into parser" that can parse from IJsonReader into a reference type (ie: a class)
  2634. public static Action<IJsonReader, object> MakeIntoParser(Type type)
  2635. {
  2636. System.Diagnostics.Debug.Assert(!type.IsValueType);
  2637. // Get the reflection info for this type
  2638. var ri = ReflectionInfo.GetReflectionInfo(type);
  2639. if (ri == null)
  2640. return null;
  2641. // We'll create setters for each property/field
  2642. var setters = new Dictionary<string, Action<IJsonReader, object>>();
  2643. // Process all members
  2644. foreach (var m in ri.Members)
  2645. {
  2646. // Ignore write only properties
  2647. var pi = m.Member as PropertyInfo;
  2648. var fi = m.Member as FieldInfo;
  2649. if (pi != null && pi.GetSetMethod(true) == null)
  2650. {
  2651. continue;
  2652. }
  2653. // Ignore read only properties that has KeepInstance attribute
  2654. if (pi != null && pi.GetGetMethod(true) == null && m.KeepInstance)
  2655. {
  2656. continue;
  2657. }
  2658. // Create a dynamic method that can do the work
  2659. var method = new DynamicMethod("dynamic_parser", null, new Type[] { typeof(IJsonReader), typeof(object) }, true);
  2660. var il = method.GetILGenerator();
  2661. // Load the target
  2662. il.Emit(OpCodes.Ldarg_1);
  2663. il.Emit(OpCodes.Castclass, type);
  2664. // Try to keep existing instance?
  2665. if (m.KeepInstance)
  2666. {
  2667. // Get existing existing instance
  2668. il.Emit(OpCodes.Dup);
  2669. if (pi != null)
  2670. il.Emit(OpCodes.Callvirt, pi.GetGetMethod(true));
  2671. else
  2672. il.Emit(OpCodes.Ldfld, fi);
  2673. var existingInstance = il.DeclareLocal(m.MemberType);
  2674. var lblExistingInstanceNull = il.DefineLabel();
  2675. // Keep a copy of the existing instance in a locale
  2676. il.Emit(OpCodes.Dup);
  2677. il.Emit(OpCodes.Stloc, existingInstance);
  2678. // Compare to null
  2679. il.Emit(OpCodes.Ldnull);
  2680. il.Emit(OpCodes.Ceq);
  2681. il.Emit(OpCodes.Brtrue_S, lblExistingInstanceNull);
  2682. il.Emit(OpCodes.Ldarg_0); // reader
  2683. il.Emit(OpCodes.Ldloc, existingInstance); // into
  2684. il.Emit(OpCodes.Callvirt, typeof(IJsonReader).GetMethod("ParseInto", new Type[] { typeof(Object) }));
  2685. il.Emit(OpCodes.Pop); // Clean up target left on stack (1)
  2686. il.Emit(OpCodes.Ret);
  2687. il.MarkLabel(lblExistingInstanceNull);
  2688. }
  2689. // Get the value from IJsonReader
  2690. GenerateGetJsonValue(m, il);
  2691. // Assign it
  2692. if (pi != null)
  2693. il.Emit(OpCodes.Callvirt, pi.GetSetMethod(true));
  2694. if (fi != null)
  2695. il.Emit(OpCodes.Stfld, fi);
  2696. // Done
  2697. il.Emit(OpCodes.Ret);
  2698. // Store the handler in map
  2699. setters.Add(m.JsonKey, (Action<IJsonReader, object>)method.CreateDelegate(typeof(Action<IJsonReader, object>)));
  2700. }
  2701. // Now create the parseInto delegate
  2702. Action<IJsonReader, object> parseInto = (reader, obj) =>
  2703. {
  2704. // Call IJsonLoading
  2705. var loading = obj as IJsonLoading;
  2706. if (loading != null)
  2707. loading.OnJsonLoading(reader);
  2708. // Cache IJsonLoadField
  2709. var lf = obj as IJsonLoadField;
  2710. // Read dictionary keys
  2711. reader.ParseDictionary(key =>
  2712. {
  2713. // Call IJsonLoadField
  2714. if (lf != null && lf.OnJsonField(reader, key))
  2715. return;
  2716. // Call setters
  2717. Action<IJsonReader, object> setter;
  2718. if (setters.TryGetValue(key, out setter))
  2719. {
  2720. setter(reader, obj);
  2721. }
  2722. });
  2723. // Call IJsonLoaded
  2724. var loaded = obj as IJsonLoaded;
  2725. if (loaded != null)
  2726. loaded.OnJsonLoaded(reader);
  2727. };
  2728. // Since we've created the ParseInto handler, we might as well register
  2729. // as a Parse handler too.
  2730. RegisterIntoParser(type, parseInto);
  2731. // Done
  2732. return parseInto;
  2733. }
  2734. // Registers a ParseInto handler as Parse handler that instantiates the object
  2735. // and then parses into it.
  2736. static void RegisterIntoParser(Type type, Action<IJsonReader, object> parseInto)
  2737. {
  2738. // Check type has a parameterless constructor
  2739. var con = type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[0], null);
  2740. if (con == null)
  2741. return;
  2742. // Create a dynamic method that can do the work
  2743. var method = new DynamicMethod("dynamic_factory", typeof(object), new Type[] { typeof(IJsonReader), typeof(Action<IJsonReader, object>) }, true);
  2744. var il = method.GetILGenerator();
  2745. // Create the new object
  2746. var locObj = il.DeclareLocal(typeof(object));
  2747. il.Emit(OpCodes.Newobj, con);
  2748. il.Emit(OpCodes.Dup); // For return value
  2749. il.Emit(OpCodes.Stloc, locObj);
  2750. il.Emit(OpCodes.Ldarg_1); // parseinto delegate
  2751. il.Emit(OpCodes.Ldarg_0); // IJsonReader
  2752. il.Emit(OpCodes.Ldloc, locObj); // new object instance
  2753. il.Emit(OpCodes.Callvirt, typeof(Action<IJsonReader, object>).GetMethod("Invoke"));
  2754. il.Emit(OpCodes.Ret);
  2755. var factory = (Func<IJsonReader, Action<IJsonReader, object>, object>)method.CreateDelegate(typeof(Func<IJsonReader, Action<IJsonReader, object>, object>));
  2756. Json.RegisterParser(type, (reader, type2) =>
  2757. {
  2758. return factory(reader, parseInto);
  2759. });
  2760. }
  2761. // Generate the MSIL to retrieve a value for a particular field or property from a IJsonReader
  2762. private static void GenerateGetJsonValue(JsonMemberInfo m, ILGenerator il)
  2763. {
  2764. Action<string> generateCallToHelper = helperName =>
  2765. {
  2766. // Call the helper
  2767. il.Emit(OpCodes.Ldarg_0);
  2768. il.Emit(OpCodes.Call, typeof(Emit).GetMethod(helperName, new Type[] { typeof(IJsonReader) }));
  2769. // Move to next token
  2770. il.Emit(OpCodes.Ldarg_0);
  2771. il.Emit(OpCodes.Callvirt, typeof(IJsonReader).GetMethod("NextToken", new Type[] { }));
  2772. };
  2773. Type[] numericTypes = new Type[] {
  2774. typeof(int), typeof(uint), typeof(long), typeof(ulong),
  2775. typeof(short), typeof(ushort), typeof(decimal),
  2776. typeof(byte), typeof(sbyte),
  2777. typeof(double), typeof(float)
  2778. };
  2779. if (m.MemberType == typeof(string))
  2780. {
  2781. generateCallToHelper("GetLiteralString");
  2782. }
  2783. else if (m.MemberType == typeof(bool))
  2784. {
  2785. generateCallToHelper("GetLiteralBool");
  2786. }
  2787. else if (m.MemberType == typeof(char))
  2788. {
  2789. generateCallToHelper("GetLiteralChar");
  2790. }
  2791. else if (numericTypes.Contains(m.MemberType))
  2792. {
  2793. // Get raw number string
  2794. il.Emit(OpCodes.Ldarg_0);
  2795. il.Emit(OpCodes.Call, typeof(Emit).GetMethod("GetLiteralNumber", new Type[] { typeof(IJsonReader) }));
  2796. // Convert to a string
  2797. il.Emit(OpCodes.Call, typeof(CultureInfo).GetProperty("InvariantCulture").GetGetMethod());
  2798. il.Emit(OpCodes.Call, m.MemberType.GetMethod("Parse", new Type[] { typeof(string), typeof(IFormatProvider) }));
  2799. //
  2800. il.Emit(OpCodes.Ldarg_0);
  2801. il.Emit(OpCodes.Callvirt, typeof(IJsonReader).GetMethod("NextToken", new Type[] { }));
  2802. }
  2803. else
  2804. {
  2805. il.Emit(OpCodes.Ldarg_0);
  2806. il.Emit(OpCodes.Ldtoken, m.MemberType);
  2807. il.Emit(OpCodes.Call, typeof(Type).GetMethod("GetTypeFromHandle", new Type[] { typeof(RuntimeTypeHandle) }));
  2808. il.Emit(OpCodes.Callvirt, typeof(IJsonReader).GetMethod("Parse", new Type[] { typeof(Type) }));
  2809. il.Emit(m.MemberType.IsValueType ? OpCodes.Unbox_Any : OpCodes.Castclass, m.MemberType);
  2810. }
  2811. }
  2812. // Helper to fetch a literal bool from an IJsonReader
  2813. [Obfuscation(Exclude = true)]
  2814. public static bool GetLiteralBool(IJsonReader r)
  2815. {
  2816. switch (r.GetLiteralKind())
  2817. {
  2818. case LiteralKind.True:
  2819. return true;
  2820. case LiteralKind.False:
  2821. return false;
  2822. default:
  2823. throw new InvalidDataException("expected a boolean value");
  2824. }
  2825. }
  2826. // Helper to fetch a literal character from an IJsonReader
  2827. [Obfuscation(Exclude = true)]
  2828. public static char GetLiteralChar(IJsonReader r)
  2829. {
  2830. if (r.GetLiteralKind() != LiteralKind.String)
  2831. throw new InvalidDataException("expected a single character string literal");
  2832. var str = r.GetLiteralString();
  2833. if (str == null || str.Length != 1)
  2834. throw new InvalidDataException("expected a single character string literal");
  2835. return str[0];
  2836. }
  2837. // Helper to fetch a literal string from an IJsonReader
  2838. [Obfuscation(Exclude = true)]
  2839. public static string GetLiteralString(IJsonReader r)
  2840. {
  2841. switch (r.GetLiteralKind())
  2842. {
  2843. case LiteralKind.Null: return null;
  2844. case LiteralKind.String: return r.GetLiteralString();
  2845. }
  2846. throw new InvalidDataException("expected a string literal");
  2847. }
  2848. // Helper to fetch a literal number from an IJsonReader (returns the raw string)
  2849. [Obfuscation(Exclude = true)]
  2850. public static string GetLiteralNumber(IJsonReader r)
  2851. {
  2852. switch (r.GetLiteralKind())
  2853. {
  2854. case LiteralKind.SignedInteger:
  2855. case LiteralKind.UnsignedInteger:
  2856. case LiteralKind.FloatingPoint:
  2857. return r.GetLiteralString();
  2858. }
  2859. throw new InvalidDataException("expected a numeric literal");
  2860. }
  2861. }
  2862. #endif
  2863. }
  2864. }