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

SimpleJSONDotNetTypes.cs 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. #region License and information
  2. /* * * * *
  3. *
  4. * Extension file for the SimpleJSON framework for better support of some common
  5. * .NET types. It does only work together with the SimpleJSON.cs
  6. * It provides direct conversion support for types like decimal, char, byte,
  7. * sbyte, short, ushort, uint, DateTime, TimeSpan and Guid. In addition there
  8. * are conversion helpers for converting an array of number values into a byte[]
  9. * or a List<byte> as well as converting an array of string values into a string[]
  10. * or List<string>.
  11. * Finally there are some additional type conversion operators for some nullable
  12. * types like short?, int?, float?, double?, long? and bool?. They will actually
  13. * assign a JSONNull value when it's null or a JSONNumber when it's not.
  14. *
  15. * The MIT License (MIT)
  16. *
  17. * Copyright (c) 2020 Markus Göbel (Bunny83)
  18. *
  19. * Permission is hereby granted, free of charge, to any person obtaining a copy
  20. * of this software and associated documentation files (the "Software"), to deal
  21. * in the Software without restriction, including without limitation the rights
  22. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  23. * copies of the Software, and to permit persons to whom the Software is
  24. * furnished to do so, subject to the following conditions:
  25. *
  26. * The above copyright notice and this permission notice shall be included in all
  27. * copies or substantial portions of the Software.
  28. *
  29. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  30. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  31. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  32. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  33. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  34. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  35. * SOFTWARE.
  36. *
  37. * * * * */
  38. #endregion License and information
  39. namespace SimpleJSON
  40. {
  41. using System.Globalization;
  42. using System.Collections.Generic;
  43. public partial class JSONNode
  44. {
  45. #region Decimal
  46. public virtual decimal AsDecimal
  47. {
  48. get
  49. {
  50. decimal result;
  51. if (!decimal.TryParse(Value, out result))
  52. result = 0;
  53. return result;
  54. }
  55. set
  56. {
  57. Value = value.ToString();
  58. }
  59. }
  60. public static implicit operator JSONNode(decimal aDecimal)
  61. {
  62. return new JSONString(aDecimal.ToString());
  63. }
  64. public static implicit operator decimal(JSONNode aNode)
  65. {
  66. return aNode.AsDecimal;
  67. }
  68. #endregion Decimal
  69. #region Char
  70. public virtual char AsChar
  71. {
  72. get
  73. {
  74. if (IsString && Value.Length > 0)
  75. return Value[0];
  76. if (IsNumber)
  77. return (char)AsInt;
  78. return '\0';
  79. }
  80. set
  81. {
  82. if (IsString)
  83. Value = value.ToString();
  84. else if (IsNumber)
  85. AsInt = (int)value;
  86. }
  87. }
  88. public static implicit operator JSONNode(char aChar)
  89. {
  90. return new JSONString(aChar.ToString());
  91. }
  92. public static implicit operator char(JSONNode aNode)
  93. {
  94. return aNode.AsChar;
  95. }
  96. #endregion Decimal
  97. #region UInt
  98. public virtual uint AsUInt
  99. {
  100. get
  101. {
  102. return (uint)AsDouble;
  103. }
  104. set
  105. {
  106. AsDouble = value;
  107. }
  108. }
  109. public static implicit operator JSONNode(uint aUInt)
  110. {
  111. return new JSONNumber(aUInt);
  112. }
  113. public static implicit operator uint(JSONNode aNode)
  114. {
  115. return aNode.AsUInt;
  116. }
  117. #endregion UInt
  118. #region Byte
  119. public virtual byte AsByte
  120. {
  121. get
  122. {
  123. return (byte)AsInt;
  124. }
  125. set
  126. {
  127. AsInt = value;
  128. }
  129. }
  130. public static implicit operator JSONNode(byte aByte)
  131. {
  132. return new JSONNumber(aByte);
  133. }
  134. public static implicit operator byte(JSONNode aNode)
  135. {
  136. return aNode.AsByte;
  137. }
  138. #endregion Byte
  139. #region SByte
  140. public virtual sbyte AsSByte
  141. {
  142. get
  143. {
  144. return (sbyte)AsInt;
  145. }
  146. set
  147. {
  148. AsInt = value;
  149. }
  150. }
  151. public static implicit operator JSONNode(sbyte aSByte)
  152. {
  153. return new JSONNumber(aSByte);
  154. }
  155. public static implicit operator sbyte(JSONNode aNode)
  156. {
  157. return aNode.AsSByte;
  158. }
  159. #endregion SByte
  160. #region Short
  161. public virtual short AsShort
  162. {
  163. get
  164. {
  165. return (short)AsInt;
  166. }
  167. set
  168. {
  169. AsInt = value;
  170. }
  171. }
  172. public static implicit operator JSONNode(short aShort)
  173. {
  174. return new JSONNumber(aShort);
  175. }
  176. public static implicit operator short(JSONNode aNode)
  177. {
  178. return aNode.AsShort;
  179. }
  180. #endregion Short
  181. #region UShort
  182. public virtual ushort AsUShort
  183. {
  184. get
  185. {
  186. return (ushort)AsInt;
  187. }
  188. set
  189. {
  190. AsInt = value;
  191. }
  192. }
  193. public static implicit operator JSONNode(ushort aUShort)
  194. {
  195. return new JSONNumber(aUShort);
  196. }
  197. public static implicit operator ushort(JSONNode aNode)
  198. {
  199. return aNode.AsUShort;
  200. }
  201. #endregion UShort
  202. #region DateTime
  203. public virtual System.DateTime AsDateTime
  204. {
  205. get
  206. {
  207. System.DateTime result;
  208. if (!System.DateTime.TryParse(Value, CultureInfo.InvariantCulture, DateTimeStyles.None, out result))
  209. result = new System.DateTime(0);
  210. return result;
  211. }
  212. set
  213. {
  214. Value = value.ToString(CultureInfo.InvariantCulture);
  215. }
  216. }
  217. public static implicit operator JSONNode(System.DateTime aDateTime)
  218. {
  219. return new JSONString(aDateTime.ToString(CultureInfo.InvariantCulture));
  220. }
  221. public static implicit operator System.DateTime(JSONNode aNode)
  222. {
  223. return aNode.AsDateTime;
  224. }
  225. #endregion DateTime
  226. #region TimeSpan
  227. public virtual System.TimeSpan AsTimeSpan
  228. {
  229. get
  230. {
  231. System.TimeSpan result;
  232. if (!System.TimeSpan.TryParse(Value, CultureInfo.InvariantCulture, out result))
  233. result = new System.TimeSpan(0);
  234. return result;
  235. }
  236. set
  237. {
  238. Value = value.ToString();
  239. }
  240. }
  241. public static implicit operator JSONNode(System.TimeSpan aTimeSpan)
  242. {
  243. return new JSONString(aTimeSpan.ToString());
  244. }
  245. public static implicit operator System.TimeSpan(JSONNode aNode)
  246. {
  247. return aNode.AsTimeSpan;
  248. }
  249. #endregion TimeSpan
  250. #region Guid
  251. public virtual System.Guid AsGuid
  252. {
  253. get
  254. {
  255. System.Guid result;
  256. System.Guid.TryParse(Value, out result);
  257. return result;
  258. }
  259. set
  260. {
  261. Value = value.ToString();
  262. }
  263. }
  264. public static implicit operator JSONNode(System.Guid aGuid)
  265. {
  266. return new JSONString(aGuid.ToString());
  267. }
  268. public static implicit operator System.Guid(JSONNode aNode)
  269. {
  270. return aNode.AsGuid;
  271. }
  272. #endregion Guid
  273. #region ByteArray
  274. public virtual byte[] AsByteArray
  275. {
  276. get
  277. {
  278. if (this.IsNull || !this.IsArray)
  279. return null;
  280. int count = Count;
  281. byte[] result = new byte[count];
  282. for (int i = 0; i < count; i++)
  283. result[i] = this[i].AsByte;
  284. return result;
  285. }
  286. set
  287. {
  288. if (!IsArray || value == null)
  289. return;
  290. Clear();
  291. for (int i = 0; i < value.Length; i++)
  292. Add(value[i]);
  293. }
  294. }
  295. public static implicit operator JSONNode(byte[] aByteArray)
  296. {
  297. return new JSONArray { AsByteArray = aByteArray };
  298. }
  299. public static implicit operator byte[](JSONNode aNode)
  300. {
  301. return aNode.AsByteArray;
  302. }
  303. #endregion ByteArray
  304. #region ByteList
  305. public virtual List<byte> AsByteList
  306. {
  307. get
  308. {
  309. if (this.IsNull || !this.IsArray)
  310. return null;
  311. int count = Count;
  312. List<byte> result = new List<byte>(count);
  313. for (int i = 0; i < count; i++)
  314. result.Add(this[i].AsByte);
  315. return result;
  316. }
  317. set
  318. {
  319. if (!IsArray || value == null)
  320. return;
  321. Clear();
  322. for (int i = 0; i < value.Count; i++)
  323. Add(value[i]);
  324. }
  325. }
  326. public static implicit operator JSONNode(List<byte> aByteList)
  327. {
  328. return new JSONArray { AsByteList = aByteList };
  329. }
  330. public static implicit operator List<byte> (JSONNode aNode)
  331. {
  332. return aNode.AsByteList;
  333. }
  334. #endregion ByteList
  335. #region StringArray
  336. public virtual string[] AsStringArray
  337. {
  338. get
  339. {
  340. if (this.IsNull || !this.IsArray)
  341. return null;
  342. int count = Count;
  343. string[] result = new string[count];
  344. for (int i = 0; i < count; i++)
  345. result[i] = this[i].Value;
  346. return result;
  347. }
  348. set
  349. {
  350. if (!IsArray || value == null)
  351. return;
  352. Clear();
  353. for (int i = 0; i < value.Length; i++)
  354. Add(value[i]);
  355. }
  356. }
  357. public static implicit operator JSONNode(string[] aStringArray)
  358. {
  359. return new JSONArray { AsStringArray = aStringArray };
  360. }
  361. public static implicit operator string[] (JSONNode aNode)
  362. {
  363. return aNode.AsStringArray;
  364. }
  365. #endregion StringArray
  366. #region StringList
  367. public virtual List<string> AsStringList
  368. {
  369. get
  370. {
  371. if (this.IsNull || !this.IsArray)
  372. return null;
  373. int count = Count;
  374. List<string> result = new List<string>(count);
  375. for (int i = 0; i < count; i++)
  376. result.Add(this[i].Value);
  377. return result;
  378. }
  379. set
  380. {
  381. if (!IsArray || value == null)
  382. return;
  383. Clear();
  384. for (int i = 0; i < value.Count; i++)
  385. Add(value[i]);
  386. }
  387. }
  388. public static implicit operator JSONNode(List<string> aStringList)
  389. {
  390. return new JSONArray { AsStringList = aStringList };
  391. }
  392. public static implicit operator List<string> (JSONNode aNode)
  393. {
  394. return aNode.AsStringList;
  395. }
  396. #endregion StringList
  397. #region NullableTypes
  398. public static implicit operator JSONNode(int? aValue)
  399. {
  400. if (aValue == null)
  401. return JSONNull.CreateOrGet();
  402. return new JSONNumber((int)aValue);
  403. }
  404. public static implicit operator int?(JSONNode aNode)
  405. {
  406. if (aNode == null || aNode.IsNull)
  407. return null;
  408. return aNode.AsInt;
  409. }
  410. public static implicit operator JSONNode(float? aValue)
  411. {
  412. if (aValue == null)
  413. return JSONNull.CreateOrGet();
  414. return new JSONNumber((float)aValue);
  415. }
  416. public static implicit operator float? (JSONNode aNode)
  417. {
  418. if (aNode == null || aNode.IsNull)
  419. return null;
  420. return aNode.AsFloat;
  421. }
  422. public static implicit operator JSONNode(double? aValue)
  423. {
  424. if (aValue == null)
  425. return JSONNull.CreateOrGet();
  426. return new JSONNumber((double)aValue);
  427. }
  428. public static implicit operator double? (JSONNode aNode)
  429. {
  430. if (aNode == null || aNode.IsNull)
  431. return null;
  432. return aNode.AsDouble;
  433. }
  434. public static implicit operator JSONNode(bool? aValue)
  435. {
  436. if (aValue == null)
  437. return JSONNull.CreateOrGet();
  438. return new JSONBool((bool)aValue);
  439. }
  440. public static implicit operator bool? (JSONNode aNode)
  441. {
  442. if (aNode == null || aNode.IsNull)
  443. return null;
  444. return aNode.AsBool;
  445. }
  446. public static implicit operator JSONNode(long? aValue)
  447. {
  448. if (aValue == null)
  449. return JSONNull.CreateOrGet();
  450. return new JSONNumber((long)aValue);
  451. }
  452. public static implicit operator long? (JSONNode aNode)
  453. {
  454. if (aNode == null || aNode.IsNull)
  455. return null;
  456. return aNode.AsLong;
  457. }
  458. public static implicit operator JSONNode(short? aValue)
  459. {
  460. if (aValue == null)
  461. return JSONNull.CreateOrGet();
  462. return new JSONNumber((short)aValue);
  463. }
  464. public static implicit operator short? (JSONNode aNode)
  465. {
  466. if (aNode == null || aNode.IsNull)
  467. return null;
  468. return aNode.AsShort;
  469. }
  470. #endregion NullableTypes
  471. }
  472. }