JsonMapper.cs 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  1. #region Header
  2. /**
  3. * JsonMapper.cs
  4. * JSON to .Net object and object to JSON conversions.
  5. *
  6. * The authors disclaim copyright to this source code. For more details, see
  7. * the COPYING file included with this distribution.
  8. **/
  9. #endregion
  10. using System;
  11. using System.Collections;
  12. using System.Collections.Generic;
  13. using System.Globalization;
  14. using System.IO;
  15. using System.Reflection;
  16. using LitJson.Extensions;
  17. namespace LitJson
  18. {
  19. internal struct PropertyMetadata
  20. {
  21. public MemberInfo Info;
  22. public bool IsField;
  23. public Type Type;
  24. }
  25. internal struct ArrayMetadata
  26. {
  27. private Type element_type;
  28. private bool is_array;
  29. private bool is_list;
  30. public Type ElementType
  31. {
  32. get
  33. {
  34. if (element_type == null)
  35. return typeof(JsonData);
  36. return element_type;
  37. }
  38. set { element_type = value; }
  39. }
  40. public bool IsArray
  41. {
  42. get { return is_array; }
  43. set { is_array = value; }
  44. }
  45. public bool IsList
  46. {
  47. get { return is_list; }
  48. set { is_list = value; }
  49. }
  50. }
  51. internal struct ObjectMetadata
  52. {
  53. private Type element_type;
  54. private bool is_dictionary;
  55. private IDictionary<string, PropertyMetadata> properties;
  56. public Type ElementType
  57. {
  58. get
  59. {
  60. if (element_type == null)
  61. return typeof(JsonData);
  62. return element_type;
  63. }
  64. set { element_type = value; }
  65. }
  66. public bool IsDictionary
  67. {
  68. get { return is_dictionary; }
  69. set { is_dictionary = value; }
  70. }
  71. public IDictionary<string, PropertyMetadata> Properties
  72. {
  73. get { return properties; }
  74. set { properties = value; }
  75. }
  76. }
  77. internal delegate void ExporterFunc(object obj, JsonWriter writer);
  78. public delegate void ExporterFunc<T>(T obj, JsonWriter writer);
  79. internal delegate object ImporterFunc(object input);
  80. public delegate TValue ImporterFunc<TJson, TValue>(TJson input);
  81. public delegate IJsonWrapper WrapperFactory();
  82. public class JsonMapper
  83. {
  84. #region Fields
  85. private static readonly int max_nesting_depth;
  86. private static readonly IFormatProvider datetime_format;
  87. private static readonly IDictionary<Type, ExporterFunc> base_exporters_table;
  88. private static readonly IDictionary<Type, ExporterFunc> custom_exporters_table;
  89. private static readonly IDictionary<Type,
  90. IDictionary<Type, ImporterFunc>> base_importers_table;
  91. private static readonly IDictionary<Type,
  92. IDictionary<Type, ImporterFunc>> custom_importers_table;
  93. private static readonly IDictionary<Type, ArrayMetadata> array_metadata;
  94. private static readonly object array_metadata_lock = new Object();
  95. private static readonly IDictionary<Type,
  96. IDictionary<Type, MethodInfo>> conv_ops;
  97. private static readonly object conv_ops_lock = new Object();
  98. private static readonly IDictionary<Type, ObjectMetadata> object_metadata;
  99. private static readonly object object_metadata_lock = new Object();
  100. private static readonly IDictionary<Type,
  101. IList<PropertyMetadata>> type_properties;
  102. private static readonly object type_properties_lock = new Object();
  103. private static readonly JsonWriter static_writer;
  104. private static readonly object static_writer_lock = new Object();
  105. #endregion
  106. #region Constructors
  107. static JsonMapper()
  108. {
  109. max_nesting_depth = 100;
  110. array_metadata = new Dictionary<Type, ArrayMetadata>();
  111. conv_ops = new Dictionary<Type, IDictionary<Type, MethodInfo>>();
  112. object_metadata = new Dictionary<Type, ObjectMetadata>();
  113. type_properties = new Dictionary<Type,
  114. IList<PropertyMetadata>>();
  115. static_writer = new JsonWriter();
  116. datetime_format = DateTimeFormatInfo.InvariantInfo;
  117. base_exporters_table = new Dictionary<Type, ExporterFunc>();
  118. custom_exporters_table = new Dictionary<Type, ExporterFunc>();
  119. base_importers_table = new Dictionary<Type,
  120. IDictionary<Type, ImporterFunc>>();
  121. custom_importers_table = new Dictionary<Type,
  122. IDictionary<Type, ImporterFunc>>();
  123. RegisterBaseExporters();
  124. RegisterBaseImporters();
  125. }
  126. #endregion
  127. #region Private Methods
  128. private static void AddArrayMetadata(Type type)
  129. {
  130. if (array_metadata.ContainsKey(type))
  131. return;
  132. ArrayMetadata data = new ArrayMetadata();
  133. data.IsArray = type.IsArray;
  134. if (type.GetInterface("System.Collections.IList") != null)
  135. data.IsList = true;
  136. foreach (PropertyInfo p_info in type.GetProperties())
  137. {
  138. if (p_info.Name != "Item")
  139. continue;
  140. ParameterInfo[] parameters = p_info.GetIndexParameters();
  141. if (parameters.Length != 1)
  142. continue;
  143. if (parameters[0].ParameterType == typeof(int))
  144. data.ElementType = p_info.PropertyType;
  145. }
  146. lock (array_metadata_lock)
  147. {
  148. try
  149. {
  150. array_metadata.Add(type, data);
  151. }
  152. catch (ArgumentException)
  153. {
  154. return;
  155. }
  156. }
  157. }
  158. private static void AddObjectMetadata(Type type)
  159. {
  160. if (object_metadata.ContainsKey(type))
  161. return;
  162. ObjectMetadata data = new ObjectMetadata();
  163. if (type.GetInterface("System.Collections.IDictionary") != null)
  164. data.IsDictionary = true;
  165. data.Properties = new Dictionary<string, PropertyMetadata>();
  166. foreach (PropertyInfo p_info in type.GetProperties())
  167. {
  168. if (p_info.Name == "Item")
  169. {
  170. ParameterInfo[] parameters = p_info.GetIndexParameters();
  171. if (parameters.Length != 1)
  172. continue;
  173. if (parameters[0].ParameterType == typeof(string))
  174. data.ElementType = p_info.PropertyType;
  175. continue;
  176. }
  177. PropertyMetadata p_data = new PropertyMetadata();
  178. p_data.Info = p_info;
  179. p_data.Type = p_info.PropertyType;
  180. data.Properties.Add(p_info.Name, p_data);
  181. }
  182. foreach (FieldInfo f_info in type.GetFields())
  183. {
  184. PropertyMetadata p_data = new PropertyMetadata();
  185. p_data.Info = f_info;
  186. p_data.IsField = true;
  187. p_data.Type = f_info.FieldType;
  188. data.Properties.Add(f_info.Name, p_data);
  189. }
  190. lock (object_metadata_lock)
  191. {
  192. try
  193. {
  194. object_metadata.Add(type, data);
  195. }
  196. catch (ArgumentException)
  197. {
  198. return;
  199. }
  200. }
  201. }
  202. private static void AddTypeProperties(Type type)
  203. {
  204. if (type_properties.ContainsKey(type))
  205. return;
  206. IList<PropertyMetadata> props = new List<PropertyMetadata>();
  207. foreach (PropertyInfo p_info in type.GetProperties())
  208. {
  209. if (p_info.Name == "Item")
  210. continue;
  211. PropertyMetadata p_data = new PropertyMetadata();
  212. p_data.Info = p_info;
  213. p_data.IsField = false;
  214. props.Add(p_data);
  215. }
  216. foreach (FieldInfo f_info in type.GetFields())
  217. {
  218. PropertyMetadata p_data = new PropertyMetadata();
  219. p_data.Info = f_info;
  220. p_data.IsField = true;
  221. props.Add(p_data);
  222. }
  223. lock (type_properties_lock)
  224. {
  225. try
  226. {
  227. type_properties.Add(type, props);
  228. }
  229. catch (ArgumentException)
  230. {
  231. return;
  232. }
  233. }
  234. }
  235. private static MethodInfo GetConvOp(Type t1, Type t2)
  236. {
  237. lock (conv_ops_lock)
  238. {
  239. if (!conv_ops.ContainsKey(t1))
  240. conv_ops.Add(t1, new Dictionary<Type, MethodInfo>());
  241. }
  242. if (conv_ops[t1].ContainsKey(t2))
  243. return conv_ops[t1][t2];
  244. MethodInfo op = t1.GetMethod(
  245. "op_Implicit", new Type[] { t2 });
  246. lock (conv_ops_lock)
  247. {
  248. try
  249. {
  250. conv_ops[t1].Add(t2, op);
  251. }
  252. catch (ArgumentException)
  253. {
  254. return conv_ops[t1][t2];
  255. }
  256. }
  257. return op;
  258. }
  259. private static object ReadValue(Type inst_type, JsonReader reader)
  260. {
  261. reader.Read();
  262. if (reader.Token == JsonToken.ArrayEnd)
  263. return null;
  264. Type underlying_type = Nullable.GetUnderlyingType(inst_type);
  265. Type value_type = underlying_type ?? inst_type;
  266. if (reader.Token == JsonToken.Null)
  267. {
  268. #if NETSTANDARD1_5
  269. if (inst_type.IsClass() || underlying_type != null) {
  270. return null;
  271. }
  272. #else
  273. if (inst_type.IsClass || underlying_type != null)
  274. {
  275. return null;
  276. }
  277. #endif
  278. throw new JsonException(String.Format(
  279. "Can't assign null to an instance of type {0}",
  280. inst_type));
  281. }
  282. if (reader.Token == JsonToken.Double ||
  283. reader.Token == JsonToken.Int ||
  284. reader.Token == JsonToken.Long ||
  285. reader.Token == JsonToken.String ||
  286. reader.Token == JsonToken.Boolean)
  287. {
  288. Type json_type = reader.Value.GetType();
  289. if (value_type.IsAssignableFrom(json_type))
  290. return reader.Value;
  291. // If there's a custom importer that fits, use it
  292. if (custom_importers_table.ContainsKey(json_type) &&
  293. custom_importers_table[json_type].ContainsKey(
  294. value_type))
  295. {
  296. ImporterFunc importer =
  297. custom_importers_table[json_type][value_type];
  298. return importer(reader.Value);
  299. }
  300. // Maybe there's a base importer that works
  301. if (base_importers_table.ContainsKey(json_type) &&
  302. base_importers_table[json_type].ContainsKey(
  303. value_type))
  304. {
  305. ImporterFunc importer =
  306. base_importers_table[json_type][value_type];
  307. return importer(reader.Value);
  308. }
  309. // Maybe it's an enum
  310. #if NETSTANDARD1_5
  311. if (value_type.IsEnum())
  312. return Enum.ToObject (value_type, reader.Value);
  313. #else
  314. if (value_type.IsEnum)
  315. return Enum.ToObject(value_type, reader.Value);
  316. #endif
  317. // Try using an implicit conversion operator
  318. MethodInfo conv_op = GetConvOp(value_type, json_type);
  319. if (conv_op != null)
  320. return conv_op.Invoke(null,
  321. new object[] { reader.Value });
  322. // No luck
  323. throw new JsonException(String.Format(
  324. "Can't assign value '{0}' (type {1}) to type {2}",
  325. reader.Value, json_type, inst_type));
  326. }
  327. object instance = null;
  328. if (reader.Token == JsonToken.ArrayStart)
  329. {
  330. AddArrayMetadata(inst_type);
  331. ArrayMetadata t_data = array_metadata[inst_type];
  332. if (!t_data.IsArray && !t_data.IsList)
  333. throw new JsonException(String.Format(
  334. "Type {0} can't act as an array",
  335. inst_type));
  336. IList list;
  337. Type elem_type;
  338. if (!t_data.IsArray)
  339. {
  340. list = (IList)Activator.CreateInstance(inst_type);
  341. elem_type = t_data.ElementType;
  342. }
  343. else
  344. {
  345. list = new ArrayList();
  346. elem_type = inst_type.GetElementType();
  347. }
  348. while (true)
  349. {
  350. object item = ReadValue(elem_type, reader);
  351. if (item == null && reader.Token == JsonToken.ArrayEnd)
  352. break;
  353. list.Add(item);
  354. }
  355. if (t_data.IsArray)
  356. {
  357. int n = list.Count;
  358. instance = Array.CreateInstance(elem_type, n);
  359. for (int i = 0; i < n; i++)
  360. ((Array)instance).SetValue(list[i], i);
  361. }
  362. else
  363. instance = list;
  364. }
  365. else if (reader.Token == JsonToken.ObjectStart)
  366. {
  367. AddObjectMetadata(value_type);
  368. ObjectMetadata t_data = object_metadata[value_type];
  369. instance = Activator.CreateInstance(value_type);
  370. while (true)
  371. {
  372. reader.Read();
  373. if (reader.Token == JsonToken.ObjectEnd)
  374. break;
  375. string property = (string)reader.Value;
  376. if (t_data.Properties.ContainsKey(property))
  377. {
  378. PropertyMetadata prop_data =
  379. t_data.Properties[property];
  380. if (prop_data.IsField)
  381. {
  382. ((FieldInfo)prop_data.Info).SetValue(
  383. instance, ReadValue(prop_data.Type, reader));
  384. }
  385. else
  386. {
  387. PropertyInfo p_info =
  388. (PropertyInfo)prop_data.Info;
  389. if (p_info.CanWrite)
  390. p_info.SetValue(
  391. instance,
  392. ReadValue(prop_data.Type, reader),
  393. null);
  394. else
  395. ReadValue(prop_data.Type, reader);
  396. }
  397. }
  398. else
  399. {
  400. if (!t_data.IsDictionary)
  401. {
  402. if (!reader.SkipNonMembers)
  403. {
  404. throw new JsonException(String.Format(
  405. "The type {0} doesn't have the " +
  406. "property '{1}'",
  407. inst_type, property));
  408. }
  409. else
  410. {
  411. ReadSkip(reader);
  412. continue;
  413. }
  414. }
  415. ((IDictionary)instance).Add(
  416. property, ReadValue(
  417. t_data.ElementType, reader));
  418. }
  419. }
  420. }
  421. return instance;
  422. }
  423. private static IJsonWrapper ReadValue(WrapperFactory factory,
  424. JsonReader reader)
  425. {
  426. reader.Read();
  427. if (reader.Token == JsonToken.ArrayEnd ||
  428. reader.Token == JsonToken.Null)
  429. return null;
  430. IJsonWrapper instance = factory();
  431. if (reader.Token == JsonToken.String)
  432. {
  433. instance.SetString((string)reader.Value);
  434. return instance;
  435. }
  436. if (reader.Token == JsonToken.Double)
  437. {
  438. instance.SetDouble((double)reader.Value);
  439. return instance;
  440. }
  441. if (reader.Token == JsonToken.Int)
  442. {
  443. instance.SetInt((int)reader.Value);
  444. return instance;
  445. }
  446. if (reader.Token == JsonToken.Long)
  447. {
  448. instance.SetLong((long)reader.Value);
  449. return instance;
  450. }
  451. if (reader.Token == JsonToken.Boolean)
  452. {
  453. instance.SetBoolean((bool)reader.Value);
  454. return instance;
  455. }
  456. if (reader.Token == JsonToken.ArrayStart)
  457. {
  458. instance.SetJsonType(JsonType.Array);
  459. while (true)
  460. {
  461. IJsonWrapper item = ReadValue(factory, reader);
  462. if (item == null && reader.Token == JsonToken.ArrayEnd)
  463. break;
  464. ((IList)instance).Add(item);
  465. }
  466. }
  467. else if (reader.Token == JsonToken.ObjectStart)
  468. {
  469. instance.SetJsonType(JsonType.Object);
  470. while (true)
  471. {
  472. reader.Read();
  473. if (reader.Token == JsonToken.ObjectEnd)
  474. break;
  475. string property = (string)reader.Value;
  476. ((IDictionary)instance)[property] = ReadValue(
  477. factory, reader);
  478. }
  479. }
  480. return instance;
  481. }
  482. private static void ReadSkip(JsonReader reader)
  483. {
  484. ToWrapper(
  485. delegate { return new JsonMockWrapper(); }, reader);
  486. }
  487. private static void RegisterBaseExporters()
  488. {
  489. base_exporters_table[typeof(byte)] =
  490. delegate (object obj, JsonWriter writer)
  491. {
  492. writer.Write(Convert.ToInt32((byte)obj));
  493. };
  494. base_exporters_table[typeof(char)] =
  495. delegate (object obj, JsonWriter writer)
  496. {
  497. writer.Write(Convert.ToString((char)obj));
  498. };
  499. base_exporters_table[typeof(DateTime)] =
  500. delegate (object obj, JsonWriter writer)
  501. {
  502. writer.Write(Convert.ToString((DateTime)obj,
  503. datetime_format));
  504. };
  505. base_exporters_table[typeof(decimal)] =
  506. delegate (object obj, JsonWriter writer)
  507. {
  508. writer.Write((decimal)obj);
  509. };
  510. base_exporters_table[typeof(sbyte)] =
  511. delegate (object obj, JsonWriter writer)
  512. {
  513. writer.Write(Convert.ToInt32((sbyte)obj));
  514. };
  515. base_exporters_table[typeof(short)] =
  516. delegate (object obj, JsonWriter writer)
  517. {
  518. writer.Write(Convert.ToInt32((short)obj));
  519. };
  520. base_exporters_table[typeof(ushort)] =
  521. delegate (object obj, JsonWriter writer)
  522. {
  523. writer.Write(Convert.ToInt32((ushort)obj));
  524. };
  525. base_exporters_table[typeof(uint)] =
  526. delegate (object obj, JsonWriter writer)
  527. {
  528. writer.Write(Convert.ToUInt64((uint)obj));
  529. };
  530. base_exporters_table[typeof(ulong)] =
  531. delegate (object obj, JsonWriter writer)
  532. {
  533. writer.Write((ulong)obj);
  534. };
  535. base_exporters_table[typeof(DateTimeOffset)] =
  536. delegate (object obj, JsonWriter writer)
  537. {
  538. writer.Write(((DateTimeOffset)obj).ToString("yyyy-MM-ddTHH:mm:ss.fffffffzzz", datetime_format));
  539. };
  540. }
  541. private static void RegisterBaseImporters()
  542. {
  543. ImporterFunc importer;
  544. importer = delegate (object input)
  545. {
  546. return Convert.ToByte((int)input);
  547. };
  548. RegisterImporter(base_importers_table, typeof(int),
  549. typeof(byte), importer);
  550. importer = delegate (object input)
  551. {
  552. return Convert.ToUInt64((int)input);
  553. };
  554. RegisterImporter(base_importers_table, typeof(int),
  555. typeof(ulong), importer);
  556. importer = delegate (object input)
  557. {
  558. return Convert.ToInt64((int)input);
  559. };
  560. RegisterImporter(base_importers_table, typeof(int),
  561. typeof(long), importer);
  562. importer = delegate (object input)
  563. {
  564. return Convert.ToSByte((int)input);
  565. };
  566. RegisterImporter(base_importers_table, typeof(int),
  567. typeof(sbyte), importer);
  568. importer = delegate (object input)
  569. {
  570. return Convert.ToInt16((int)input);
  571. };
  572. RegisterImporter(base_importers_table, typeof(int),
  573. typeof(short), importer);
  574. importer = delegate (object input)
  575. {
  576. return Convert.ToUInt16((int)input);
  577. };
  578. RegisterImporter(base_importers_table, typeof(int),
  579. typeof(ushort), importer);
  580. importer = delegate (object input)
  581. {
  582. return Convert.ToUInt32((int)input);
  583. };
  584. RegisterImporter(base_importers_table, typeof(int),
  585. typeof(uint), importer);
  586. importer = delegate (object input)
  587. {
  588. return Convert.ToSingle((int)input);
  589. };
  590. RegisterImporter(base_importers_table, typeof(int),
  591. typeof(float), importer);
  592. importer = delegate (object input)
  593. {
  594. return Convert.ToDouble((int)input);
  595. };
  596. RegisterImporter(base_importers_table, typeof(int),
  597. typeof(double), importer);
  598. importer = delegate (object input)
  599. {
  600. return Convert.ToDecimal((double)input);
  601. };
  602. RegisterImporter(base_importers_table, typeof(double),
  603. typeof(decimal), importer);
  604. importer = delegate (object input)
  605. {
  606. return Convert.ToSingle((double)input);
  607. };
  608. RegisterImporter(base_importers_table, typeof(double),
  609. typeof(float), importer);
  610. importer = delegate (object input)
  611. {
  612. return Convert.ToUInt32((long)input);
  613. };
  614. RegisterImporter(base_importers_table, typeof(long),
  615. typeof(uint), importer);
  616. importer = delegate (object input)
  617. {
  618. return Convert.ToChar((string)input);
  619. };
  620. RegisterImporter(base_importers_table, typeof(string),
  621. typeof(char), importer);
  622. importer = delegate (object input)
  623. {
  624. return Convert.ToDateTime((string)input, datetime_format);
  625. };
  626. RegisterImporter(base_importers_table, typeof(string),
  627. typeof(DateTime), importer);
  628. importer = delegate (object input)
  629. {
  630. return DateTimeOffset.Parse((string)input, datetime_format);
  631. };
  632. RegisterImporter(base_importers_table, typeof(string),
  633. typeof(DateTimeOffset), importer);
  634. }
  635. private static void RegisterImporter(
  636. IDictionary<Type, IDictionary<Type, ImporterFunc>> table,
  637. Type json_type, Type value_type, ImporterFunc importer)
  638. {
  639. if (!table.ContainsKey(json_type))
  640. table.Add(json_type, new Dictionary<Type, ImporterFunc>());
  641. table[json_type][value_type] = importer;
  642. }
  643. private static void WriteValue(object obj, JsonWriter writer,
  644. bool writer_is_private,
  645. int depth)
  646. {
  647. if (depth > max_nesting_depth)
  648. throw new JsonException(
  649. String.Format("Max allowed object depth reached while " +
  650. "trying to export from type {0}",
  651. obj.GetType()));
  652. if (obj == null)
  653. {
  654. writer.Write(null);
  655. return;
  656. }
  657. if (obj is IJsonWrapper)
  658. {
  659. if (writer_is_private)
  660. writer.TextWriter.Write(((IJsonWrapper)obj).ToJson());
  661. else
  662. ((IJsonWrapper)obj).ToJson(writer);
  663. return;
  664. }
  665. if (obj is String)
  666. {
  667. writer.Write((string)obj);
  668. return;
  669. }
  670. if (obj is Double)
  671. {
  672. writer.Write((double)obj);
  673. return;
  674. }
  675. if (obj is Single)
  676. {
  677. writer.Write((float)obj);
  678. return;
  679. }
  680. if (obj is Int32)
  681. {
  682. writer.Write((int)obj);
  683. return;
  684. }
  685. if (obj is Boolean)
  686. {
  687. writer.Write((bool)obj);
  688. return;
  689. }
  690. if (obj is Int64)
  691. {
  692. writer.Write((long)obj);
  693. return;
  694. }
  695. if (obj is Array)
  696. {
  697. writer.WriteArrayStart();
  698. foreach (object elem in (Array)obj)
  699. WriteValue(elem, writer, writer_is_private, depth + 1);
  700. writer.WriteArrayEnd();
  701. return;
  702. }
  703. if (obj is IList)
  704. {
  705. writer.WriteArrayStart();
  706. foreach (object elem in (IList)obj)
  707. WriteValue(elem, writer, writer_is_private, depth + 1);
  708. writer.WriteArrayEnd();
  709. return;
  710. }
  711. #if UNITY_2018_3_OR_NEWER
  712. if (obj is IDictionary dictionary) {
  713. #else
  714. if (obj is IDictionary)
  715. {
  716. var dictionary = obj as IDictionary;
  717. #endif
  718. writer.WriteObjectStart();
  719. foreach (DictionaryEntry entry in dictionary)
  720. {
  721. #if UNITY_2018_3_OR_NEWER
  722. var propertyName = entry.Key is string key ?
  723. key
  724. : Convert.ToString(entry.Key, CultureInfo.InvariantCulture);
  725. #else
  726. var propertyName = entry.Key is string ? (entry.Key as string) : Convert.ToString(entry.Key, CultureInfo.InvariantCulture);
  727. #endif
  728. writer.WritePropertyName(propertyName);
  729. WriteValue(entry.Value, writer, writer_is_private,
  730. depth + 1);
  731. }
  732. writer.WriteObjectEnd();
  733. return;
  734. }
  735. Type obj_type = obj.GetType();
  736. // See if there's a custom exporter for the object
  737. if (custom_exporters_table.ContainsKey(obj_type))
  738. {
  739. ExporterFunc exporter = custom_exporters_table[obj_type];
  740. exporter(obj, writer);
  741. return;
  742. }
  743. // If not, maybe there's a base exporter
  744. if (base_exporters_table.ContainsKey(obj_type))
  745. {
  746. ExporterFunc exporter = base_exporters_table[obj_type];
  747. exporter(obj, writer);
  748. return;
  749. }
  750. // Last option, let's see if it's an enum
  751. if (obj is Enum)
  752. {
  753. Type e_type = Enum.GetUnderlyingType(obj_type);
  754. if (e_type == typeof(long)
  755. || e_type == typeof(uint)
  756. || e_type == typeof(ulong))
  757. writer.Write((ulong)obj);
  758. else
  759. writer.Write((int)obj);
  760. return;
  761. }
  762. // Okay, so it looks like the input should be exported as an
  763. // object
  764. AddTypeProperties(obj_type);
  765. IList<PropertyMetadata> props = type_properties[obj_type];
  766. writer.WriteObjectStart();
  767. foreach (PropertyMetadata p_data in props)
  768. {
  769. var skipAttributesList = p_data.Info.GetCustomAttributes(typeof(JsonIgnore), true);
  770. var skipAttributes = skipAttributesList as ICollection<Attribute>;
  771. if (skipAttributes.Count > 0)
  772. {
  773. continue;
  774. }
  775. if (p_data.IsField)
  776. {
  777. writer.WritePropertyName(p_data.Info.Name);
  778. WriteValue(((FieldInfo)p_data.Info).GetValue(obj),
  779. writer, writer_is_private, depth + 1);
  780. }
  781. else
  782. {
  783. PropertyInfo p_info = (PropertyInfo)p_data.Info;
  784. if (p_info.CanRead)
  785. {
  786. writer.WritePropertyName(p_data.Info.Name);
  787. WriteValue(p_info.GetValue(obj, null),
  788. writer, writer_is_private, depth + 1);
  789. }
  790. }
  791. }
  792. writer.WriteObjectEnd();
  793. }
  794. #endregion
  795. public static string ToJson(object obj)
  796. {
  797. lock (static_writer_lock)
  798. {
  799. static_writer.Reset();
  800. WriteValue(obj, static_writer, true, 0);
  801. return static_writer.ToString();
  802. }
  803. }
  804. public static void ToJson(object obj, JsonWriter writer)
  805. {
  806. WriteValue(obj, writer, false, 0);
  807. }
  808. public static JsonData ToObject(JsonReader reader)
  809. {
  810. return (JsonData)ToWrapper(
  811. delegate { return new JsonData(); }, reader);
  812. }
  813. public static JsonData ToObject(TextReader reader)
  814. {
  815. JsonReader json_reader = new JsonReader(reader);
  816. return (JsonData)ToWrapper(
  817. delegate { return new JsonData(); }, json_reader);
  818. }
  819. public static JsonData ToObject(string json)
  820. {
  821. return (JsonData)ToWrapper(
  822. delegate { return new JsonData(); }, json);
  823. }
  824. public static T ToObject<T>(JsonReader reader)
  825. {
  826. return (T)ReadValue(typeof(T), reader);
  827. }
  828. public static T ToObject<T>(TextReader reader)
  829. {
  830. JsonReader json_reader = new JsonReader(reader);
  831. return (T)ReadValue(typeof(T), json_reader);
  832. }
  833. public static T ToObject<T>(string json)
  834. {
  835. JsonReader reader = new JsonReader(json);
  836. return (T)ReadValue(typeof(T), reader);
  837. }
  838. public static object ToObject(string json, Type ConvertType)
  839. {
  840. JsonReader reader = new JsonReader(json);
  841. return ReadValue(ConvertType, reader);
  842. }
  843. public static IJsonWrapper ToWrapper(WrapperFactory factory,
  844. JsonReader reader)
  845. {
  846. return ReadValue(factory, reader);
  847. }
  848. public static IJsonWrapper ToWrapper(WrapperFactory factory,
  849. string json)
  850. {
  851. JsonReader reader = new JsonReader(json);
  852. return ReadValue(factory, reader);
  853. }
  854. public static void RegisterExporter<T>(ExporterFunc<T> exporter)
  855. {
  856. ExporterFunc exporter_wrapper =
  857. delegate (object obj, JsonWriter writer)
  858. {
  859. exporter((T)obj, writer);
  860. };
  861. custom_exporters_table[typeof(T)] = exporter_wrapper;
  862. }
  863. public static void RegisterImporter<TJson, TValue>(
  864. ImporterFunc<TJson, TValue> importer)
  865. {
  866. ImporterFunc importer_wrapper =
  867. delegate (object input)
  868. {
  869. return importer((TJson)input);
  870. };
  871. RegisterImporter(custom_importers_table, typeof(TJson),
  872. typeof(TValue), importer_wrapper);
  873. }
  874. public static void UnregisterExporters()
  875. {
  876. custom_exporters_table.Clear();
  877. }
  878. public static void UnregisterImporters()
  879. {
  880. custom_importers_table.Clear();
  881. }
  882. }
  883. }