PetaJson.cs 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Reflection;
  7. using System.Globalization;
  8. using System.Collections;
  9. #if SIMPLEJSON_DYNAMIC
  10. using System.Dynamic;
  11. #endif
  12. namespace PetaJson
  13. {
  14. [Flags]
  15. public enum JsonOptions
  16. {
  17. None = 0,
  18. WriteWhitespace = 0x00000001,
  19. DontWriteWhitespace = 0x00000002,
  20. StrictParser = 0x00000004,
  21. NonStrictParser = 0x00000008,
  22. }
  23. public static class Json
  24. {
  25. static Json()
  26. {
  27. WriteWhitespaceDefault = true;
  28. StrictParserDefault = false;
  29. }
  30. public static bool WriteWhitespaceDefault
  31. {
  32. get;
  33. set;
  34. }
  35. public static bool StrictParserDefault
  36. {
  37. get;
  38. set;
  39. }
  40. static JsonOptions ResolveOptions(JsonOptions options)
  41. {
  42. JsonOptions resolved = JsonOptions.None;
  43. if ((options & (JsonOptions.WriteWhitespace|JsonOptions.DontWriteWhitespace))!=0)
  44. resolved |= options & (JsonOptions.WriteWhitespace | JsonOptions.DontWriteWhitespace);
  45. else
  46. resolved |= WriteWhitespaceDefault ? JsonOptions.WriteWhitespace : JsonOptions.DontWriteWhitespace;
  47. if ((options & (JsonOptions.StrictParser | JsonOptions.NonStrictParser)) != 0)
  48. resolved |= options & (JsonOptions.StrictParser | JsonOptions.NonStrictParser);
  49. else
  50. resolved |= StrictParserDefault ? JsonOptions.StrictParser : JsonOptions.NonStrictParser;
  51. return resolved;
  52. }
  53. // Write an object to a text writer
  54. public static void Write(TextWriter w, object o, JsonOptions options = JsonOptions.None)
  55. {
  56. var writer = new Internal.Writer(w, ResolveOptions(options));
  57. writer.WriteValue(o);
  58. }
  59. // Write an object to a file
  60. public static void WriteFile(string filename, object o, JsonOptions options = JsonOptions.None)
  61. {
  62. using (var w = new StreamWriter(filename))
  63. {
  64. Write(w, o, options);
  65. }
  66. }
  67. // Format an object as a json string
  68. public static string Format(object o, JsonOptions options = JsonOptions.None)
  69. {
  70. var sw = new StringWriter();
  71. var writer = new Internal.Writer(sw, ResolveOptions(options));
  72. writer.WriteValue(o);
  73. return sw.ToString();
  74. }
  75. // Parse an object of specified type from a text reader
  76. public static object Parse(TextReader r, Type type, JsonOptions options = JsonOptions.None)
  77. {
  78. Internal.Reader reader = null;
  79. try
  80. {
  81. reader = new Internal.Reader(r, ResolveOptions(options));
  82. var retv = reader.Parse(type);
  83. reader.CheckEOF();
  84. return retv;
  85. }
  86. catch (Exception x)
  87. {
  88. throw new JsonParseException(x, reader==null ? new JsonLineOffset() : reader.CurrentTokenPosition);
  89. }
  90. }
  91. // Parse an object of specified type from a text reader
  92. public static T Parse<T>(TextReader r, JsonOptions options = JsonOptions.None)
  93. {
  94. return (T)Parse(r, typeof(T), options);
  95. }
  96. // Parse from text reader into an already instantied object
  97. public static void ParseInto(TextReader r, Object into, JsonOptions options = JsonOptions.None)
  98. {
  99. Internal.Reader reader = null;
  100. try
  101. {
  102. reader = new Internal.Reader(r, ResolveOptions(options));
  103. reader.ParseInto(into);
  104. reader.CheckEOF();
  105. }
  106. catch (Exception x)
  107. {
  108. throw new JsonParseException(x, reader==null ? new JsonLineOffset() : reader.CurrentTokenPosition);
  109. }
  110. }
  111. // Parse an object of specified type from a text reader
  112. public static object ParseFile(string filename, Type type, JsonOptions options = JsonOptions.None)
  113. {
  114. using (var r = new StreamReader(filename))
  115. {
  116. return Parse(r, type, options);
  117. }
  118. }
  119. // Parse an object of specified type from a text reader
  120. public static T ParseFile<T>(string filename, JsonOptions options = JsonOptions.None)
  121. {
  122. using (var r = new StreamReader(filename))
  123. {
  124. return Parse<T>(r, options);
  125. }
  126. }
  127. // Parse from text reader into an already instantied object
  128. public static void ParseFileInto(string filename, Object into, JsonOptions options = JsonOptions.None)
  129. {
  130. using (var r = new StreamReader(filename))
  131. {
  132. ParseInto(r, into, options);
  133. }
  134. }
  135. // Parse an object from a string
  136. public static object Parse(string data, Type type, JsonOptions options = JsonOptions.None)
  137. {
  138. return Parse(new StringReader(data), type, options);
  139. }
  140. // Parse an object from a string
  141. public static T Parse<T>(string data, JsonOptions options = JsonOptions.None)
  142. {
  143. return (T)Parse<T>(new StringReader(data), options);
  144. }
  145. // Parse from string into an already instantiated object
  146. public static void ParseInto(string data, Object into, JsonOptions options = JsonOptions.None)
  147. {
  148. ParseInto(new StringReader(data), into, options);
  149. }
  150. public static T Clone<T>(T source)
  151. {
  152. return (T)Clone((object)source);
  153. }
  154. public static object Clone(object source)
  155. {
  156. if (source == null)
  157. return null;
  158. return Parse(Format(source), source.GetType());
  159. }
  160. public static void CloneInto<T>(T dest, T source)
  161. {
  162. ParseInto(Format(source), dest);
  163. }
  164. // Register a callback that can format a value of a particular type into json
  165. public static void RegisterFormatter(Type type, Action<IJsonWriter, object> formatter)
  166. {
  167. Internal.Writer._typeWriters[type] = formatter;
  168. }
  169. // Typed version of above
  170. public static void RegisterFormatter<T>(Action<IJsonWriter, T> formatter)
  171. {
  172. RegisterFormatter(typeof(T), (w, o) => formatter(w, (T)o));
  173. }
  174. // Register a parser for a specified type
  175. public static void RegisterParser(Type type, Func<IJsonReader, Type, object> parser)
  176. {
  177. Internal.Reader._typeReaders[type] = parser;
  178. }
  179. // Register a typed parser
  180. public static void RegisterParser<T>(Func<IJsonReader, Type, T> parser)
  181. {
  182. RegisterParser(typeof(T), (r, t) => parser(r, t));
  183. }
  184. // Simpler version for simple types
  185. public static void RegisterParser(Type type, Func<object, object> parser)
  186. {
  187. RegisterParser(type, (r, t) => r.ReadLiteral(parser));
  188. }
  189. // Simpler and typesafe parser for simple types
  190. public static void RegisterParser<T>(Func<object, T> parser)
  191. {
  192. RegisterParser(typeof(T), literal => parser(literal));
  193. }
  194. // Register a factory for instantiating objects (typically abstract classes)
  195. // Callback will be invoked for each key in the dictionary until it returns an object
  196. // instance and which point it will switch to serialization using reflection
  197. public static void RegisterTypeFactory(Type type, Func<IJsonReader, string, object> factory)
  198. {
  199. Internal.Reader._typeFactories[type] = factory;
  200. }
  201. }
  202. // Called before loading via reflection
  203. public interface IJsonLoading
  204. {
  205. void OnJsonLoading(IJsonReader r);
  206. }
  207. // Called after loading via reflection
  208. public interface IJsonLoaded
  209. {
  210. void OnJsonLoaded(IJsonReader r);
  211. }
  212. // Called for each field while loading from reflection
  213. // Return true if handled
  214. public interface IJsonLoadField
  215. {
  216. bool OnJsonField(IJsonReader r, string key);
  217. }
  218. // Called when about to write using reflection
  219. public interface IJsonWriting
  220. {
  221. void OnJsonWriting(IJsonWriter w);
  222. }
  223. // Called after written using reflection
  224. public interface IJsonWritten
  225. {
  226. void OnJsonWritten(IJsonWriter w);
  227. }
  228. // Passed to registered parsers
  229. public interface IJsonReader
  230. {
  231. object ReadLiteral(Func<object, object> converter);
  232. object Parse(Type type);
  233. T Parse<T>();
  234. void ReadDictionary(Action<string> callback);
  235. void ReadArray(Action callback);
  236. }
  237. // Passed to registered formatters
  238. public interface IJsonWriter
  239. {
  240. void WriteStringLiteral(string str);
  241. void WriteRaw(string str);
  242. void WriteArray(Action callback);
  243. void WriteDictionary(Action callback);
  244. void WriteValue(object value);
  245. void WriteElement();
  246. void WriteKey(string key);
  247. }
  248. public class JsonParseException : Exception
  249. {
  250. public JsonParseException(Exception inner, JsonLineOffset position) :
  251. base(string.Format("Json parse error at {0} - {1}", position, inner.Message), inner)
  252. {
  253. }
  254. }
  255. public struct JsonLineOffset
  256. {
  257. public int Line;
  258. public int Offset;
  259. public override string ToString()
  260. {
  261. return string.Format("line {0}, position {1}", Line + 1, Offset + 1);
  262. }
  263. }
  264. // Used to decorate fields and properties that should be serialized
  265. //
  266. // - [Json] on class or struct causes all public fields and properties to be serialized
  267. // - [Json] on a public or non-public field or property causes that member to be serialized
  268. // - [JsonExclude] on a field or property causes that field to be not serialized
  269. // - A class or struct with no [Json] attribute has all public fields/properties serialized
  270. // - A class or struct with no [Json] attribute but a [Json] attribute on one or more members only serializes those members
  271. //
  272. // Use [Json("keyname")] to explicitly specify the key to be used
  273. // [Json] without the keyname will be serialized using the name of the member with the first letter lowercased.
  274. //
  275. // [Json(KeepInstance=true)] causes container/subobject types to be serialized into the existing member instance (if not null)
  276. //
  277. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Property | AttributeTargets.Field)]
  278. public class JsonAttribute : Attribute
  279. {
  280. public JsonAttribute()
  281. {
  282. _key = null;
  283. }
  284. public JsonAttribute(string key)
  285. {
  286. _key = key;
  287. }
  288. string _key;
  289. public string Key
  290. {
  291. get { return _key; }
  292. }
  293. public bool KeepInstance
  294. {
  295. get;
  296. set;
  297. }
  298. }
  299. // See comments above
  300. [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
  301. public class JsonExcludeAttribute : Attribute
  302. {
  303. public JsonExcludeAttribute()
  304. {
  305. }
  306. }
  307. namespace Internal
  308. {
  309. public enum Token
  310. {
  311. EOF,
  312. Identifier,
  313. Literal,
  314. OpenBrace,
  315. CloseBrace,
  316. OpenSquare,
  317. CloseSquare,
  318. Equal,
  319. Colon,
  320. SemiColon,
  321. Comma,
  322. }
  323. public class Reader : IJsonReader
  324. {
  325. static Reader()
  326. {
  327. Func<IJsonReader, Type, object> simpleConverter = (reader, type) =>
  328. {
  329. return reader.ReadLiteral(literal => Convert.ChangeType(literal, type, CultureInfo.InvariantCulture));
  330. };
  331. _typeReaders.Add(typeof(string), simpleConverter);
  332. _typeReaders.Add(typeof(char), simpleConverter);
  333. _typeReaders.Add(typeof(bool), simpleConverter);
  334. _typeReaders.Add(typeof(byte), simpleConverter);
  335. _typeReaders.Add(typeof(sbyte), simpleConverter);
  336. _typeReaders.Add(typeof(short), simpleConverter);
  337. _typeReaders.Add(typeof(ushort), simpleConverter);
  338. _typeReaders.Add(typeof(int), simpleConverter);
  339. _typeReaders.Add(typeof(uint), simpleConverter);
  340. _typeReaders.Add(typeof(long), simpleConverter);
  341. _typeReaders.Add(typeof(ulong), simpleConverter);
  342. _typeReaders.Add(typeof(decimal), simpleConverter);
  343. _typeReaders.Add(typeof(float), simpleConverter);
  344. _typeReaders.Add(typeof(double), simpleConverter);
  345. _typeReaders.Add(typeof(DateTime), (reader, type) =>
  346. {
  347. return reader.ReadLiteral(literal => Utils.FromUnixMilliseconds((long)Convert.ChangeType(literal, typeof(long), CultureInfo.InvariantCulture)));
  348. });
  349. _typeReaders.Add(typeof(byte[]), (reader, type) =>
  350. {
  351. return reader.ReadLiteral(literal => Convert.FromBase64String((string)Convert.ChangeType(literal, typeof(string), CultureInfo.InvariantCulture)));
  352. });
  353. }
  354. public Reader(TextReader r, JsonOptions options)
  355. {
  356. _tokenizer = new Tokenizer(r, options);
  357. _options = options;
  358. }
  359. Tokenizer _tokenizer;
  360. JsonOptions _options;
  361. public JsonLineOffset CurrentTokenPosition
  362. {
  363. get { return _tokenizer.CurrentTokenPosition; }
  364. }
  365. // ReadLiteral is implemented with a converter callback so that any
  366. // errors on converting to the target type are thrown before the tokenizer
  367. // is advanced to the next token. This ensures error location is reported
  368. // at the start of the literal, not the following token.
  369. public object ReadLiteral(Func<object, object> converter)
  370. {
  371. _tokenizer.Check(Token.Literal);
  372. var retv = converter(_tokenizer.Literal);
  373. _tokenizer.NextToken();
  374. return retv;
  375. }
  376. public void CheckEOF()
  377. {
  378. _tokenizer.Check(Token.EOF);
  379. }
  380. public object Parse(Type type)
  381. {
  382. // Null?
  383. if (_tokenizer.CurrentToken == Token.Literal && _tokenizer.Literal == null)
  384. {
  385. _tokenizer.NextToken();
  386. return null;
  387. }
  388. // Handle nullable types
  389. var typeUnderlying = Nullable.GetUnderlyingType(type);
  390. if (typeUnderlying != null)
  391. type = typeUnderlying;
  392. // See if we have a reader
  393. Func<IJsonReader, Type, object> typeReader;
  394. if (Reader._typeReaders.TryGetValue(type, out typeReader))
  395. {
  396. return typeReader(this, type);
  397. }
  398. // Enumerated type?
  399. if (type.IsEnum)
  400. {
  401. return ReadLiteral(literal => Enum.Parse(type, (string)literal));
  402. }
  403. // See if we have factory
  404. Func<IJsonReader, string, object> typeFactory;
  405. if (Reader._typeFactories.TryGetValue(type, out typeFactory))
  406. {
  407. // Try first without passing dictionary keys
  408. object into = typeFactory(this, null);
  409. if (into == null)
  410. {
  411. // This is a awkward situation. The factory requires a value from the dictionary
  412. // in order to create the target object (typically an abstract class with the class
  413. // kind recorded in the Json). Since there's no guarantee of order in a json dictionary
  414. // we can't assume the required key is first.
  415. // So, create a bookmark on the tokenizer, read keys until the factory returns an
  416. // object instance and then rewind the tokenizer and continue
  417. // Create a bookmark so we can rewind
  418. _tokenizer.CreateBookmark();
  419. // Skip the opening brace
  420. _tokenizer.Skip(Token.OpenBrace);
  421. // First pass to work out type
  422. ReadDictionaryKeys(key =>
  423. {
  424. // Try to instantiate the object
  425. into = typeFactory(this, key);
  426. return into == null;
  427. });
  428. // Move back to start of the dictionary
  429. _tokenizer.RewindToBookmark();
  430. // Quit if still didn't get an object from the factory
  431. if (into == null)
  432. throw new InvalidOperationException("Factory didn't create object instance (probably due to a missing key in the Json)");
  433. }
  434. // Second pass
  435. ParseInto(into);
  436. // Done
  437. return into;
  438. }
  439. // Is it a type we can parse into?
  440. if (CanParseInto(type))
  441. {
  442. var into = Activator.CreateInstance(type);
  443. ParseInto(into);
  444. return into;
  445. }
  446. // Array?
  447. if (type.IsArray && type.GetArrayRank() == 1)
  448. {
  449. // First parse as a List<>
  450. var listType = typeof(List<>).MakeGenericType(type.GetElementType());
  451. var list = Activator.CreateInstance(listType);
  452. ParseInto(list);
  453. return listType.GetMethod("ToArray").Invoke(list, null);
  454. }
  455. // Untyped dictionary?
  456. if (_tokenizer.CurrentToken == Token.OpenBrace && (type.IsAssignableFrom(typeof(Dictionary<string, object>))))
  457. {
  458. #if SIMPLEJSON_DYNAMIC
  459. var container = (new ExpandoObject()) as IDictionary<string, object>;
  460. #else
  461. var container = new Dictionary<string, object>();
  462. #endif
  463. ReadDictionary(key =>
  464. {
  465. container[key] = Parse(typeof(Object));
  466. });
  467. return container;
  468. }
  469. // Untyped list?
  470. if (_tokenizer.CurrentToken == Token.OpenSquare && (type.IsAssignableFrom(typeof(List<object>))))
  471. {
  472. var container = new List<object>();
  473. ReadArray(() =>
  474. {
  475. container.Add(Parse(typeof(Object)));
  476. });
  477. return container;
  478. }
  479. // Untyped literal?
  480. if (_tokenizer.CurrentToken == Token.Literal && type.IsAssignableFrom(_tokenizer.Literal.GetType()))
  481. {
  482. var lit = _tokenizer.Literal;
  483. _tokenizer.NextToken();
  484. return lit;
  485. }
  486. throw new InvalidDataException(string.Format("syntax error - unexpected token {0}", _tokenizer.CurrentToken));
  487. }
  488. public static bool CanParseInto(Type type)
  489. {
  490. /* These two checks are redundant as they're covered by IDictionary/IList below
  491. if (typeof(IDictionary<,>).IsAssignableFrom(type))
  492. return true;
  493. if (typeof(IList<>).IsAssignableFrom(type))
  494. return true;
  495. */
  496. if (type.IsArray)
  497. return false;
  498. if (typeof(IDictionary).IsAssignableFrom(type))
  499. return true;
  500. if (typeof(IList).IsAssignableFrom(type))
  501. return true;
  502. if (ReflectionInfo.GetReflectionInfo(type) != null)
  503. return true;
  504. return false;
  505. }
  506. public void ParseInto(object into)
  507. {
  508. if (TryParseInto(into))
  509. return;
  510. throw new InvalidOperationException(string.Format("Don't know how to load into '{0}'", into.GetType().FullName));
  511. }
  512. public Type FindGenericInterface(Type type, Type tItf)
  513. {
  514. foreach (var t in type.GetInterfaces())
  515. {
  516. // Is this a generic list?
  517. if (t.IsGenericType && t.GetGenericTypeDefinition() == tItf)
  518. return type;
  519. }
  520. return null;
  521. }
  522. public bool TryParseInto(object into)
  523. {
  524. if (into == null)
  525. return false;
  526. var type = into.GetType();
  527. // Generic dictionary?
  528. var dictType = FindGenericInterface(type, typeof(IDictionary<,>));
  529. if (dictType!=null)
  530. {
  531. // Get the key and value types
  532. var typeKey = dictType.GetGenericArguments()[0];
  533. var typeValue = dictType.GetGenericArguments()[1];
  534. // Parse it
  535. IDictionary dict = (IDictionary)into;
  536. dict.Clear();
  537. ReadDictionary(key =>
  538. {
  539. dict.Add(Convert.ChangeType(key, typeKey), Parse(typeValue));
  540. });
  541. return true;
  542. }
  543. // Generic list
  544. var listType = FindGenericInterface(type, typeof(IList<>));
  545. if (listType!=null)
  546. {
  547. // Get element type
  548. var typeElement = listType.GetGenericArguments()[0];
  549. // Parse it
  550. IList list = (IList)into;
  551. list.Clear();
  552. ReadArray(() =>
  553. {
  554. list.Add(Parse(typeElement));
  555. });
  556. return true;
  557. }
  558. // Untyped dictionary
  559. var objDict = into as IDictionary;
  560. if (objDict != null)
  561. {
  562. objDict.Clear();
  563. ReadDictionary(key =>
  564. {
  565. objDict[key] = Parse(typeof(Object));
  566. });
  567. return true;
  568. }
  569. // Untyped list
  570. var objList = into as IList;
  571. if (objList!=null)
  572. {
  573. objList.Clear();
  574. ReadArray(() =>
  575. {
  576. objList.Add(Parse(typeof(Object)));
  577. });
  578. return true;
  579. }
  580. // Use reflection?
  581. var ri = ReflectionInfo.GetReflectionInfo(type);
  582. if (ri != null)
  583. {
  584. ri.ParseInto(this, into);
  585. return true;
  586. }
  587. return false;
  588. }
  589. public T Parse<T>()
  590. {
  591. return (T)Parse(typeof(T));
  592. }
  593. public void ReadDictionary(Action<string> callback)
  594. {
  595. _tokenizer.Skip(Token.OpenBrace);
  596. ReadDictionaryKeys(key => { callback(key); return true; });
  597. _tokenizer.Skip(Token.CloseBrace);
  598. }
  599. private void ReadDictionaryKeys(Func<string, bool> callback)
  600. {
  601. while (_tokenizer.CurrentToken != Token.CloseBrace)
  602. {
  603. // Parse the key
  604. string key = null;
  605. if (_tokenizer.CurrentToken == Token.Identifier && (_options & JsonOptions.StrictParser)==0)
  606. {
  607. key = _tokenizer.String;
  608. }
  609. else if (_tokenizer.CurrentToken == Token.Literal && _tokenizer.Literal is String)
  610. {
  611. key = (string)_tokenizer.Literal;
  612. }
  613. else
  614. {
  615. throw new InvalidDataException("syntax error, expected string literal or identifier");
  616. }
  617. _tokenizer.NextToken();
  618. _tokenizer.Skip(Token.Colon);
  619. _tokenizer.DidMove = false;
  620. if (!callback(key))
  621. return;
  622. if (!_tokenizer.DidMove)
  623. {
  624. // The callback didn't handle the key, so skip the value
  625. Parse(typeof(object));
  626. }
  627. if (_tokenizer.SkipIf(Token.Comma))
  628. {
  629. if ((_options & JsonOptions.StrictParser) != 0 && _tokenizer.CurrentToken == Token.CloseBrace)
  630. {
  631. throw new InvalidDataException("Trailing commas not allowed in strict mode");
  632. }
  633. continue;
  634. }
  635. break;
  636. }
  637. }
  638. public void ReadArray(Action callback)
  639. {
  640. _tokenizer.Skip(Token.OpenSquare);
  641. while (_tokenizer.CurrentToken != Token.CloseSquare)
  642. {
  643. callback();
  644. if (_tokenizer.SkipIf(Token.Comma))
  645. {
  646. if ((_options & JsonOptions.StrictParser)!=0 && _tokenizer.CurrentToken==Token.CloseSquare)
  647. {
  648. throw new InvalidDataException("Trailing commas not allowed in strict mode");
  649. }
  650. continue;
  651. }
  652. break;
  653. }
  654. _tokenizer.Skip(Token.CloseSquare);
  655. }
  656. public static Dictionary<Type, Func<IJsonReader, Type, object>> _typeReaders = new Dictionary<Type, Func<IJsonReader, Type, object>>();
  657. public static Dictionary<Type, Func<IJsonReader, string, object>> _typeFactories = new Dictionary<Type, Func<IJsonReader, string, object>>();
  658. }
  659. public class Writer : IJsonWriter
  660. {
  661. static Writer()
  662. {
  663. // Strings
  664. _typeWriters.Add(typeof(string), (w, o) => w.WriteStringLiteral((string)o));
  665. _typeWriters.Add(typeof(char), (w, o) => w.WriteStringLiteral(((char)o).ToString()));
  666. // Boolean
  667. _typeWriters.Add(typeof(bool), (w, o) => w.WriteRaw(((bool)o) ? "true" : "false"));
  668. // Integer
  669. Action<IJsonWriter, object> convertWriter = (w, o) => w.WriteRaw((string)Convert.ChangeType(o, typeof(string), System.Globalization.CultureInfo.InvariantCulture));
  670. _typeWriters.Add(typeof(int), convertWriter);
  671. _typeWriters.Add(typeof(uint), convertWriter);
  672. _typeWriters.Add(typeof(long), convertWriter);
  673. _typeWriters.Add(typeof(ulong), convertWriter);
  674. _typeWriters.Add(typeof(short), convertWriter);
  675. _typeWriters.Add(typeof(ushort), convertWriter);
  676. _typeWriters.Add(typeof(decimal), convertWriter);
  677. _typeWriters.Add(typeof(byte), convertWriter);
  678. _typeWriters.Add(typeof(sbyte), convertWriter);
  679. // Date
  680. _typeWriters.Add(typeof(DateTime), (w, o) => convertWriter(w, Utils.ToUnixMilliseconds((DateTime)o)));
  681. // Floating point
  682. _typeWriters.Add(typeof(float), (w, o) => w.WriteRaw(((float)o).ToString("R", System.Globalization.CultureInfo.InvariantCulture)));
  683. _typeWriters.Add(typeof(double), (w, o) => w.WriteRaw(((double)o).ToString("R", System.Globalization.CultureInfo.InvariantCulture)));
  684. // Byte array
  685. _typeWriters.Add(typeof(byte[]), (w, o) =>
  686. {
  687. w.WriteRaw("\"");
  688. w.WriteRaw(Convert.ToBase64String((byte[])o));
  689. w.WriteRaw("\"");
  690. });
  691. }
  692. public static Dictionary<Type, Action<IJsonWriter, object>> _typeWriters = new Dictionary<Type, Action<IJsonWriter, object>>();
  693. public Writer(TextWriter w, JsonOptions options)
  694. {
  695. _writer = w;
  696. _atStartOfLine = true;
  697. _needElementSeparator = false;
  698. _options = options;
  699. }
  700. TextWriter _writer;
  701. public int IndentLevel;
  702. bool _atStartOfLine;
  703. bool _needElementSeparator = false;
  704. JsonOptions _options;
  705. char _currentBlockKind = '\0';
  706. public void NextLine()
  707. {
  708. if (_atStartOfLine)
  709. return;
  710. if ((_options & JsonOptions.WriteWhitespace)!=0)
  711. {
  712. WriteRaw("\n");
  713. WriteRaw(new string('\t', IndentLevel));
  714. }
  715. _atStartOfLine = true;
  716. }
  717. void NextElement()
  718. {
  719. if (_needElementSeparator)
  720. {
  721. WriteRaw(",");
  722. NextLine();
  723. }
  724. else
  725. {
  726. NextLine();
  727. IndentLevel++;
  728. WriteRaw(_currentBlockKind.ToString());
  729. NextLine();
  730. }
  731. _needElementSeparator = true;
  732. }
  733. public void WriteElement()
  734. {
  735. if (_currentBlockKind != '[')
  736. throw new InvalidOperationException("Attempt to write array element when not in array block");
  737. NextElement();
  738. }
  739. public void WriteKey(string key)
  740. {
  741. if (_currentBlockKind != '{')
  742. throw new InvalidOperationException("Attempt to write dictionary element when not in dictionary block");
  743. NextElement();
  744. WriteStringLiteral(key);
  745. WriteRaw(((_options & JsonOptions.WriteWhitespace)!=0) ? ": " : ":");
  746. }
  747. public void WriteRaw(string str)
  748. {
  749. _atStartOfLine = false;
  750. _writer.Write(str);
  751. }
  752. public void WriteStringLiteral(string str)
  753. {
  754. _writer.Write("\"");
  755. foreach (var ch in str)
  756. {
  757. switch (ch)
  758. {
  759. case '\"': _writer.Write("\\\""); break;
  760. case '\r': _writer.Write("\\r"); break;
  761. case '\n': _writer.Write("\\n"); break;
  762. case '\t': _writer.Write("\\t"); break;
  763. case '\0': _writer.Write("\\0"); break;
  764. case '\\': _writer.Write("\\\\"); break;
  765. case '\'': _writer.Write("\\'"); break;
  766. default: _writer.Write(ch); break;
  767. }
  768. }
  769. _writer.Write("\"");
  770. }
  771. void WriteBlock(string open, string close, Action callback)
  772. {
  773. var prevBlockKind = _currentBlockKind;
  774. _currentBlockKind = open[0];
  775. var didNeedElementSeparator = _needElementSeparator;
  776. _needElementSeparator = false;
  777. callback();
  778. if (_needElementSeparator)
  779. {
  780. IndentLevel--;
  781. NextLine();
  782. }
  783. else
  784. {
  785. WriteRaw(open);
  786. }
  787. WriteRaw(close);
  788. _needElementSeparator = didNeedElementSeparator;
  789. _currentBlockKind = prevBlockKind;
  790. }
  791. public void WriteArray(Action callback)
  792. {
  793. WriteBlock("[", "]", callback);
  794. }
  795. public void WriteDictionary(Action callback)
  796. {
  797. WriteBlock("{", "}", callback);
  798. }
  799. public void WriteValue(object value)
  800. {
  801. // Special handling for null
  802. if (value == null)
  803. {
  804. _writer.Write("null");
  805. return;
  806. }
  807. var type = value.GetType();
  808. // Handle nullable types
  809. var typeUnderlying = Nullable.GetUnderlyingType(type);
  810. if (typeUnderlying != null)
  811. type = typeUnderlying;
  812. // Look up type writer
  813. Action<IJsonWriter, object> typeWriter;
  814. if (_typeWriters.TryGetValue(type, out typeWriter))
  815. {
  816. // Write it
  817. typeWriter(this, value);
  818. return;
  819. }
  820. // Enumerated type?
  821. if (type.IsEnum)
  822. {
  823. WriteStringLiteral(value.ToString());
  824. return;
  825. }
  826. // Dictionary?
  827. var d = value as System.Collections.IDictionary;
  828. if (d != null)
  829. {
  830. WriteDictionary(() =>
  831. {
  832. foreach (var key in d.Keys)
  833. {
  834. WriteKey(key.ToString());
  835. WriteValue(d[key]);
  836. }
  837. });
  838. return;
  839. }
  840. // Array?
  841. var e = value as System.Collections.IEnumerable;
  842. if (e != null)
  843. {
  844. WriteArray(() =>
  845. {
  846. foreach (var i in e)
  847. {
  848. WriteElement();
  849. WriteValue(i);
  850. }
  851. });
  852. return;
  853. }
  854. // Try using reflection
  855. var ri = ReflectionInfo.GetReflectionInfo(type);
  856. if (ri != null)
  857. {
  858. ri.Write(this, value);
  859. return;
  860. }
  861. // What the?
  862. throw new InvalidDataException(string.Format("Don't know how to write '{0}' to json", value.GetType()));
  863. }
  864. }
  865. class JsonMemberInfo
  866. {
  867. public MemberInfo Member;
  868. public string JsonKey;
  869. public bool KeepInstance;
  870. public Type MemberType
  871. {
  872. get
  873. {
  874. if (Member is PropertyInfo)
  875. {
  876. return ((PropertyInfo)Member).PropertyType;
  877. }
  878. else
  879. {
  880. return ((FieldInfo)Member).FieldType;
  881. }
  882. }
  883. }
  884. public void SetValue(object target, object value)
  885. {
  886. if (Member is PropertyInfo)
  887. {
  888. ((PropertyInfo)Member).SetValue(target, value, null);
  889. }
  890. else
  891. {
  892. ((FieldInfo)Member).SetValue(target, value);
  893. }
  894. }
  895. public object GetValue(object target)
  896. {
  897. if (Member is PropertyInfo)
  898. {
  899. return ((PropertyInfo)Member).GetValue(target, null);
  900. }
  901. else
  902. {
  903. return ((FieldInfo)Member).GetValue(target);
  904. }
  905. }
  906. }
  907. class ReflectionInfo
  908. {
  909. List<JsonMemberInfo> _members;
  910. static Dictionary<Type, ReflectionInfo> _cache = new Dictionary<Type, ReflectionInfo>();
  911. public void Write(Writer w, object val)
  912. {
  913. w.WriteDictionary(() =>
  914. {
  915. var writing = val as IJsonWriting;
  916. if (writing != null)
  917. writing.OnJsonWriting(w);
  918. foreach (var jmi in _members)
  919. {
  920. w.WriteKey(jmi.JsonKey);
  921. w.WriteValue(jmi.GetValue(val));
  922. }
  923. var written = val as IJsonWritten;
  924. if (written != null)
  925. written.OnJsonWritten(w);
  926. });
  927. }
  928. // The member info is stored in a list (as opposed to a dictionary) so that
  929. // the json is written in the same order as the fields/properties are defined
  930. // On loading, we assume the fields will be in the same order, but need to
  931. // handle if they're not. This function performs a linear search, but
  932. // starts after the last found item as an optimization that should work
  933. // most of the time.
  934. int _lastFoundIndex = 0;
  935. bool FindMemberInfo(string name, out JsonMemberInfo found)
  936. {
  937. for (int i = 0; i < _members.Count; i++)
  938. {
  939. int index = (i + _lastFoundIndex) % _members.Count;
  940. var jmi = _members[index];
  941. if (jmi.JsonKey == name)
  942. {
  943. _lastFoundIndex = index;
  944. found = jmi;
  945. return true;
  946. }
  947. }
  948. found = null;
  949. return false;
  950. }
  951. public void ParseFieldOrProperty(Reader r, object into, string key)
  952. {
  953. var lf = into as IJsonLoadField;
  954. if (lf != null)
  955. {
  956. if (lf.OnJsonField(r, key))
  957. return;
  958. }
  959. JsonMemberInfo jmi;
  960. if (FindMemberInfo(key, out jmi))
  961. {
  962. if (jmi.KeepInstance && Reader.CanParseInto(jmi.MemberType))
  963. {
  964. var subInto = jmi.GetValue(into);
  965. if (subInto != null)
  966. {
  967. r.ParseInto(subInto);
  968. return;
  969. }
  970. }
  971. var val = r.Parse(jmi.MemberType);
  972. jmi.SetValue(into, val);
  973. return;
  974. }
  975. }
  976. public void ParseInto(Reader r, object into)
  977. {
  978. var loading = into as IJsonLoading;
  979. r.ReadDictionary(key => {
  980. if (loading != null)
  981. {
  982. loading.OnJsonLoading(r);
  983. loading = null;
  984. }
  985. ParseFieldOrProperty(r, into, key);
  986. });
  987. var loaded = into as IJsonLoaded;
  988. if (loaded != null)
  989. loaded.OnJsonLoaded(r);
  990. }
  991. public static ReflectionInfo GetReflectionInfo(Type type)
  992. {
  993. // Already created?
  994. ReflectionInfo existing;
  995. if (_cache.TryGetValue(type, out existing))
  996. return existing;
  997. // Does type have a [Json] attribute
  998. bool typeMarked = type.GetCustomAttributes(typeof(JsonAttribute), true).OfType<JsonAttribute>().Any();
  999. // Do any members have a [Json] attribute
  1000. bool anyFieldsMarked = GetAllFieldsAndProperties(type).Any(x => x.GetCustomAttributes(typeof(JsonAttribute), false).OfType<JsonAttribute>().Any());
  1001. // Should we serialize all public methods?
  1002. bool serializeAllPublics = typeMarked || !anyFieldsMarked;
  1003. // Build
  1004. var ri = CreateReflectionInfo(type, mi =>
  1005. {
  1006. // Explicitly excluded?
  1007. if (mi.GetCustomAttributes(typeof(JsonExcludeAttribute), false).OfType<JsonExcludeAttribute>().Any())
  1008. return null;
  1009. // Get attributes
  1010. var attr = mi.GetCustomAttributes(typeof(JsonAttribute), false).OfType<JsonAttribute>().FirstOrDefault();
  1011. if (attr != null)
  1012. {
  1013. return new JsonMemberInfo()
  1014. {
  1015. Member = mi,
  1016. JsonKey = attr.Key ?? mi.Name.Substring(0, 1).ToLower() + mi.Name.Substring(1),
  1017. KeepInstance = attr.KeepInstance,
  1018. };
  1019. }
  1020. // Serialize all publics?
  1021. if (serializeAllPublics && IsPublic(mi))
  1022. {
  1023. return new JsonMemberInfo()
  1024. {
  1025. Member = mi,
  1026. JsonKey = mi.Name.Substring(0, 1).ToLower() + mi.Name.Substring(1),
  1027. };
  1028. }
  1029. return null;
  1030. });
  1031. // Cache it
  1032. _cache[type] = ri;
  1033. return ri;
  1034. }
  1035. static bool IsPublic(MemberInfo mi)
  1036. {
  1037. // Public field
  1038. var fi = mi as FieldInfo;
  1039. if (fi!=null)
  1040. return fi.IsPublic;
  1041. // Public property
  1042. // (We only check the get method so we can work with anonymous types)
  1043. var pi = mi as PropertyInfo;
  1044. if (pi != null)
  1045. {
  1046. var gm = pi.GetGetMethod();
  1047. return (gm != null && gm.IsPublic);
  1048. }
  1049. return false;
  1050. }
  1051. public static ReflectionInfo CreateReflectionInfo(Type type, Func<MemberInfo, JsonMemberInfo> callback)
  1052. {
  1053. // Already created?
  1054. ReflectionInfo existing;
  1055. if (_cache.TryGetValue(type, out existing))
  1056. return existing;
  1057. // Work out properties and fields
  1058. var members = GetAllFieldsAndProperties(type).Select(x => callback(x)).Where(x => x != null).ToList();
  1059. // Must have some members
  1060. if (!members.Any())
  1061. return null;
  1062. // Create reflection info
  1063. var ri = new ReflectionInfo()
  1064. {
  1065. _members = members,
  1066. };
  1067. return ri;
  1068. }
  1069. static IEnumerable<MemberInfo> GetAllFieldsAndProperties(Type t)
  1070. {
  1071. if (t == null)
  1072. return Enumerable.Empty<FieldInfo>();
  1073. BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly;
  1074. return t.GetMembers(flags).Where(x => x is FieldInfo || x is PropertyInfo).Concat(GetAllFieldsAndProperties(t.BaseType));
  1075. }
  1076. }
  1077. internal static class Utils
  1078. {
  1079. public static long ToUnixMilliseconds(DateTime This)
  1080. {
  1081. return (long)This.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
  1082. }
  1083. public static DateTime FromUnixMilliseconds(long timeStamp)
  1084. {
  1085. return new DateTime(1970, 1, 1).AddMilliseconds(timeStamp);
  1086. }
  1087. }
  1088. public class RewindableTextReader
  1089. {
  1090. public RewindableTextReader(TextReader underlying)
  1091. {
  1092. _underlying = underlying;
  1093. ReadUnderlying();
  1094. }
  1095. TextReader _underlying;
  1096. char[] _buf = new char[1024];
  1097. int _pos;
  1098. int _bufUsed;
  1099. StringBuilder _rewindBuffer;
  1100. int _rewindBufferPos;
  1101. void ReadUnderlying()
  1102. {
  1103. _bufUsed = _underlying.Read(_buf, 0, _buf.Length);
  1104. _pos = 0;
  1105. }
  1106. char ReadUnderlyingChar()
  1107. {
  1108. if (_pos >= _bufUsed)
  1109. {
  1110. if (_bufUsed > 0)
  1111. {
  1112. ReadUnderlying();
  1113. }
  1114. if (_bufUsed == 0)
  1115. {
  1116. return '\0';
  1117. }
  1118. }
  1119. // Next
  1120. return _buf[_pos++];
  1121. }
  1122. public char ReadChar()
  1123. {
  1124. if (_rewindBuffer == null)
  1125. return ReadUnderlyingChar();
  1126. if (_rewindBufferPos < _rewindBuffer.Length)
  1127. {
  1128. return _rewindBuffer[_rewindBufferPos++];
  1129. }
  1130. else
  1131. {
  1132. char ch = ReadUnderlyingChar();
  1133. _rewindBuffer.Append(ch);
  1134. _rewindBufferPos++;
  1135. return ch;
  1136. }
  1137. }
  1138. Stack<int> _bookmarks = new Stack<int>();
  1139. public void CreateBookmark()
  1140. {
  1141. if (_rewindBuffer == null)
  1142. {
  1143. _rewindBuffer = new StringBuilder();
  1144. _rewindBufferPos = 0;
  1145. }
  1146. _bookmarks.Push(_rewindBufferPos);
  1147. }
  1148. public void RewindToBookmark()
  1149. {
  1150. _rewindBufferPos = _bookmarks.Pop();
  1151. }
  1152. public void DiscardBookmark()
  1153. {
  1154. _bookmarks.Pop();
  1155. if (_bookmarks.Count == 0)
  1156. {
  1157. _rewindBuffer = null;
  1158. _rewindBufferPos = 0;
  1159. }
  1160. }
  1161. }
  1162. public class Tokenizer
  1163. {
  1164. public Tokenizer(TextReader r, JsonOptions options)
  1165. {
  1166. _reader = new RewindableTextReader(r);
  1167. _options = options;
  1168. _state._nextCharPos.Line = 0;
  1169. _state._nextCharPos.Offset = 0;
  1170. _state._currentCharPos = _state._nextCharPos;
  1171. // Load up
  1172. NextChar();
  1173. NextToken();
  1174. }
  1175. RewindableTextReader _reader;
  1176. JsonOptions _options;
  1177. StringBuilder _sb = new StringBuilder();
  1178. ReaderState _state;
  1179. // this object represents the entire state of the reader
  1180. // which when combined with the RewindableTextReader allows
  1181. // use to rewind to an arbitrary point in the token stream
  1182. struct ReaderState
  1183. {
  1184. public JsonLineOffset _nextCharPos;
  1185. public JsonLineOffset _currentCharPos;
  1186. public JsonLineOffset _currentTokenPos;
  1187. public char _currentChar;
  1188. public char _pendingChar;
  1189. public Token _currentToken;
  1190. public string _string;
  1191. public object _literal;
  1192. }
  1193. Stack<ReaderState> _bookmarks = new Stack<ReaderState>();
  1194. public void CreateBookmark()
  1195. {
  1196. _bookmarks.Push(_state);
  1197. _reader.CreateBookmark();
  1198. }
  1199. public void DiscardBookmark()
  1200. {
  1201. _bookmarks.Pop();
  1202. _reader.DiscardBookmark();
  1203. }
  1204. public void RewindToBookmark()
  1205. {
  1206. _state = _bookmarks.Pop();
  1207. _reader.RewindToBookmark();
  1208. }
  1209. char NextChar()
  1210. {
  1211. // Normalize line endings to '\n'
  1212. char ch;
  1213. if (_state._pendingChar != '\0')
  1214. {
  1215. ch = _state._pendingChar;
  1216. }
  1217. else
  1218. {
  1219. ch = _reader.ReadChar();
  1220. if (ch == '\r')
  1221. {
  1222. ch = _reader.ReadChar();
  1223. if (ch != '\n')
  1224. {
  1225. _state._pendingChar = ch;
  1226. ch = '\n';
  1227. }
  1228. }
  1229. }
  1230. _state._currentCharPos = _state._nextCharPos;
  1231. // Update line position counter
  1232. if (ch == '\n')
  1233. {
  1234. _state._nextCharPos.Line++;
  1235. _state._nextCharPos.Offset = 0;
  1236. }
  1237. else
  1238. {
  1239. _state._nextCharPos.Offset++;
  1240. }
  1241. return _state._currentChar = ch;
  1242. }
  1243. public bool DidMove
  1244. {
  1245. get;
  1246. set;
  1247. }
  1248. public void NextToken()
  1249. {
  1250. DidMove = true;
  1251. _sb.Length = 0;
  1252. State state = State.Initial;
  1253. _state._currentTokenPos = _state._currentCharPos;
  1254. while (true)
  1255. {
  1256. switch (state)
  1257. {
  1258. case State.Initial:
  1259. if (IsIdentifierLeadChar(_state._currentChar))
  1260. {
  1261. state = State.Identifier;
  1262. break;
  1263. }
  1264. else if (char.IsDigit(_state._currentChar) || _state._currentChar == '-')
  1265. {
  1266. TokenizeNumber();
  1267. _state._currentToken = Token.Literal;
  1268. return;
  1269. }
  1270. else if (char.IsWhiteSpace(_state._currentChar))
  1271. {
  1272. NextChar();
  1273. _state._currentTokenPos = _state._currentCharPos;
  1274. break;
  1275. }
  1276. switch (_state._currentChar)
  1277. {
  1278. case '/':
  1279. if ((_options & JsonOptions.StrictParser)!=0)
  1280. {
  1281. // Comments not support in strict mode
  1282. throw new InvalidDataException(string.Format("syntax error - unexpected character '{0}'", _state._currentChar));
  1283. }
  1284. else
  1285. {
  1286. NextChar();
  1287. state = State.Slash;
  1288. }
  1289. break;
  1290. case '\"':
  1291. NextChar();
  1292. _sb.Length = 0;
  1293. state = State.QuotedString;
  1294. break;
  1295. case '\'':
  1296. NextChar();
  1297. _sb.Length = 0;
  1298. state = State.QuotedChar;
  1299. break;
  1300. case '\0':
  1301. _state._currentToken = Token.EOF;
  1302. return;
  1303. case '{': _state._currentToken = Token.OpenBrace; NextChar(); return;
  1304. case '}': _state._currentToken = Token.CloseBrace; NextChar(); return;
  1305. case '[': _state._currentToken = Token.OpenSquare; NextChar(); return;
  1306. case ']': _state._currentToken = Token.CloseSquare; NextChar(); return;
  1307. case '=': _state._currentToken = Token.Equal; NextChar(); return;
  1308. case ':': _state._currentToken = Token.Colon; NextChar(); return;
  1309. case ';': _state._currentToken = Token.SemiColon; NextChar(); return;
  1310. case ',': _state._currentToken = Token.Comma; NextChar(); return;
  1311. default:
  1312. throw new InvalidDataException(string.Format("syntax error - unexpected character '{0}'", _state._currentChar));
  1313. }
  1314. break;
  1315. case State.Slash:
  1316. switch (_state._currentChar)
  1317. {
  1318. case '/':
  1319. NextChar();
  1320. state = State.SingleLineComment;
  1321. break;
  1322. case '*':
  1323. NextChar();
  1324. state = State.BlockComment;
  1325. break;
  1326. default:
  1327. throw new InvalidDataException("syntax error - unexpected character after slash");
  1328. }
  1329. break;
  1330. case State.SingleLineComment:
  1331. if (_state._currentChar == '\r' || _state._currentChar == '\n')
  1332. {
  1333. state = State.Initial;
  1334. }
  1335. NextChar();
  1336. break;
  1337. case State.BlockComment:
  1338. if (_state._currentChar == '*')
  1339. {
  1340. NextChar();
  1341. if (_state._currentChar == '/')
  1342. {
  1343. state = State.Initial;
  1344. }
  1345. }
  1346. NextChar();
  1347. break;
  1348. case State.Identifier:
  1349. if (IsIdentifierChar(_state._currentChar))
  1350. {
  1351. _sb.Append(_state._currentChar);
  1352. NextChar();
  1353. }
  1354. else
  1355. {
  1356. _state._string = _sb.ToString();
  1357. switch (this.String)
  1358. {
  1359. case "true":
  1360. _state._literal = true;
  1361. _state._currentToken = Token.Literal;
  1362. break;
  1363. case "false":
  1364. _state._literal = false;
  1365. _state._currentToken = Token.Literal;
  1366. break;
  1367. case "null":
  1368. _state._literal = null;
  1369. _state._currentToken = Token.Literal;
  1370. break;
  1371. default:
  1372. _state._currentToken = Token.Identifier;
  1373. break;
  1374. }
  1375. return;
  1376. }
  1377. break;
  1378. case State.QuotedString:
  1379. if (_state._currentChar == '\\')
  1380. {
  1381. AppendSpecialChar();
  1382. }
  1383. else if (_state._currentChar == '\"')
  1384. {
  1385. _state._literal = _sb.ToString();
  1386. _state._currentToken = Token.Literal;
  1387. NextChar();
  1388. return;
  1389. }
  1390. else
  1391. {
  1392. _sb.Append(_state._currentChar);
  1393. NextChar();
  1394. continue;
  1395. }
  1396. break;
  1397. case State.QuotedChar:
  1398. if (_state._currentChar == '\\')
  1399. {
  1400. AppendSpecialChar();
  1401. }
  1402. else if (_state._currentChar == '\'')
  1403. {
  1404. _state._literal = _sb.ToString();
  1405. _state._currentToken = Token.Literal;
  1406. NextChar();
  1407. return;
  1408. }
  1409. else
  1410. {
  1411. _sb.Append(_state._currentChar);
  1412. NextChar();
  1413. continue;
  1414. }
  1415. break;
  1416. }
  1417. }
  1418. }
  1419. void TokenizeNumber()
  1420. {
  1421. _sb.Length = 0;
  1422. // Leading -
  1423. bool signed = false;
  1424. if (_state._currentChar == '-')
  1425. {
  1426. signed = true;
  1427. _sb.Append(_state._currentChar);
  1428. NextChar();
  1429. if (!Char.IsDigit(_state._currentChar))
  1430. {
  1431. throw new InvalidDataException("syntax error - expected digit to follow negative sign");
  1432. }
  1433. }
  1434. // Parse all digits
  1435. bool fp = false;
  1436. while (char.IsDigit(_state._currentChar) || _state._currentChar == '.' || _state._currentChar == 'e' || _state._currentChar == 'E' || _state._currentChar == 'x' || _state._currentChar == 'X')
  1437. {
  1438. if (_state._currentChar == 'e' || _state._currentChar == 'E')
  1439. {
  1440. fp = true;
  1441. _sb.Append(_state._currentChar);
  1442. NextChar();
  1443. if (_state._currentChar == '-' || _state._currentChar == '+')
  1444. {
  1445. _sb.Append(_state._currentChar);
  1446. NextChar();
  1447. }
  1448. }
  1449. else
  1450. {
  1451. if (_state._currentChar == '.')
  1452. fp = true;
  1453. _sb.Append(_state._currentChar);
  1454. NextChar();
  1455. }
  1456. }
  1457. Type type = fp ? typeof(double) : (signed ? typeof(long) : typeof(ulong));
  1458. if (char.IsLetterOrDigit(_state._currentChar))
  1459. throw new InvalidDataException(string.Format("syntax error - invalid character following number '{0}'", _state._currentChar));
  1460. // Convert type
  1461. try
  1462. {
  1463. _state._literal = Convert.ChangeType(_sb.ToString(), type, System.Globalization.CultureInfo.InvariantCulture);
  1464. }
  1465. catch
  1466. {
  1467. throw new InvalidDataException(string.Format("syntax error - incorrectly formatted number '{0}'", _sb.ToString()));
  1468. }
  1469. }
  1470. public void Check(Token tokenRequired)
  1471. {
  1472. if (tokenRequired != CurrentToken)
  1473. {
  1474. throw new InvalidDataException(string.Format("syntax error - expected {0} found {1}", tokenRequired, CurrentToken));
  1475. }
  1476. }
  1477. public void Skip(Token tokenRequired)
  1478. {
  1479. Check(tokenRequired);
  1480. NextToken();
  1481. }
  1482. public bool SkipIf(Token tokenRequired)
  1483. {
  1484. if (tokenRequired == CurrentToken)
  1485. {
  1486. NextToken();
  1487. return true;
  1488. }
  1489. return false;
  1490. }
  1491. void AppendSpecialChar()
  1492. {
  1493. NextChar();
  1494. var escape = _state._currentChar;
  1495. NextChar();
  1496. switch (escape)
  1497. {
  1498. case '\'': _sb.Append('\''); break;
  1499. case '\"': _sb.Append('\"'); break;
  1500. case '\\': _sb.Append('\\'); break;
  1501. case 'r': _sb.Append('\r'); break;
  1502. case 'n': _sb.Append('\n'); break;
  1503. case 't': _sb.Append('\t'); break;
  1504. case '0': _sb.Append('\0'); break;
  1505. case 'u':
  1506. var sbHex = new StringBuilder();
  1507. for (int i = 0; i < 4; i++)
  1508. {
  1509. sbHex.Append(_state._currentChar);
  1510. NextChar();
  1511. }
  1512. _sb.Append((char)Convert.ToUInt16(sbHex.ToString(), 16));
  1513. break;
  1514. default:
  1515. throw new InvalidDataException(string.Format("Invalid escape sequence in string literal: '\\{0}'", _state._currentChar));
  1516. }
  1517. }
  1518. public Token CurrentToken
  1519. {
  1520. get { return _state._currentToken; }
  1521. }
  1522. public JsonLineOffset CurrentTokenPosition
  1523. {
  1524. get { return _state._currentTokenPos; }
  1525. }
  1526. public string String
  1527. {
  1528. get { return _state._string; }
  1529. }
  1530. public object Literal
  1531. {
  1532. get { return _state._literal; }
  1533. }
  1534. private enum State
  1535. {
  1536. Initial,
  1537. Slash,
  1538. SingleLineComment,
  1539. BlockComment,
  1540. Identifier,
  1541. QuotedString,
  1542. QuotedChar,
  1543. }
  1544. public static bool IsIdentifierChar(char ch)
  1545. {
  1546. return Char.IsLetterOrDigit(ch) || ch == '_' || ch == '$';
  1547. }
  1548. public static bool IsIdentifierLeadChar(char ch)
  1549. {
  1550. return Char.IsLetter(ch) || ch == '_' || ch == '$';
  1551. }
  1552. public static bool IsIdentifier(string str)
  1553. {
  1554. return IsIdentifierLeadChar(str[0]) && str.All(x => IsIdentifierChar(x));
  1555. }
  1556. }
  1557. }
  1558. }