You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1171 lines
40 KiB

4 years ago
  1. #region License
  2. // Copyright (c) 2007 James Newton-King
  3. //
  4. // Permission is hereby granted, free of charge, to any person
  5. // obtaining a copy of this software and associated documentation
  6. // files (the "Software"), to deal in the Software without
  7. // restriction, including without limitation the rights to use,
  8. // copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the
  10. // Software is furnished to do so, subject to the following
  11. // conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be
  14. // included in all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  18. // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  20. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  21. // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23. // OTHER DEALINGS IN THE SOFTWARE.
  24. #endregion
  25. using System;
  26. using System.Collections.Generic;
  27. using System.Diagnostics;
  28. using Newtonsoft.Json.Utilities;
  29. using System.Globalization;
  30. #if !NET20
  31. using System.Dynamic;
  32. using System.Linq.Expressions;
  33. #endif
  34. #if HAVE_BIG_INTEGER
  35. using System.Numerics;
  36. #endif
  37. namespace Newtonsoft.Json.Linq
  38. {
  39. /// <summary>
  40. /// Represents a value in JSON (string, integer, date, etc).
  41. /// </summary>
  42. internal partial class JValue : JToken, IEquatable<JValue>, IFormattable, IComparable, IComparable<JValue>, IConvertible
  43. {
  44. private JTokenType _valueType;
  45. private object _value;
  46. internal JValue(object value, JTokenType type)
  47. {
  48. _value = value;
  49. _valueType = type;
  50. }
  51. /// <summary>
  52. /// Initializes a new instance of the <see cref="JValue"/> class from another <see cref="JValue"/> object.
  53. /// </summary>
  54. /// <param name="other">A <see cref="JValue"/> object to copy from.</param>
  55. public JValue(JValue other)
  56. : this(other.Value, other.Type)
  57. {
  58. }
  59. /// <summary>
  60. /// Initializes a new instance of the <see cref="JValue"/> class with the given value.
  61. /// </summary>
  62. /// <param name="value">The value.</param>
  63. public JValue(long value)
  64. : this(value, JTokenType.Integer)
  65. {
  66. }
  67. /// <summary>
  68. /// Initializes a new instance of the <see cref="JValue"/> class with the given value.
  69. /// </summary>
  70. /// <param name="value">The value.</param>
  71. public JValue(decimal value)
  72. : this(value, JTokenType.Float)
  73. {
  74. }
  75. /// <summary>
  76. /// Initializes a new instance of the <see cref="JValue"/> class with the given value.
  77. /// </summary>
  78. /// <param name="value">The value.</param>
  79. public JValue(char value)
  80. : this(value, JTokenType.String)
  81. {
  82. }
  83. /// <summary>
  84. /// Initializes a new instance of the <see cref="JValue"/> class with the given value.
  85. /// </summary>
  86. /// <param name="value">The value.</param>
  87. public JValue(ulong value)
  88. : this(value, JTokenType.Integer)
  89. {
  90. }
  91. /// <summary>
  92. /// Initializes a new instance of the <see cref="JValue"/> class with the given value.
  93. /// </summary>
  94. /// <param name="value">The value.</param>
  95. public JValue(double value)
  96. : this(value, JTokenType.Float)
  97. {
  98. }
  99. /// <summary>
  100. /// Initializes a new instance of the <see cref="JValue"/> class with the given value.
  101. /// </summary>
  102. /// <param name="value">The value.</param>
  103. public JValue(float value)
  104. : this(value, JTokenType.Float)
  105. {
  106. }
  107. /// <summary>
  108. /// Initializes a new instance of the <see cref="JValue"/> class with the given value.
  109. /// </summary>
  110. /// <param name="value">The value.</param>
  111. public JValue(DateTime value)
  112. : this(value, JTokenType.Date)
  113. {
  114. }
  115. #if !NET20
  116. /// <summary>
  117. /// Initializes a new instance of the <see cref="JValue"/> class with the given value.
  118. /// </summary>
  119. /// <param name="value">The value.</param>
  120. public JValue(DateTimeOffset value)
  121. : this(value, JTokenType.Date)
  122. {
  123. }
  124. #endif
  125. /// <summary>
  126. /// Initializes a new instance of the <see cref="JValue"/> class with the given value.
  127. /// </summary>
  128. /// <param name="value">The value.</param>
  129. public JValue(bool value)
  130. : this(value, JTokenType.Boolean)
  131. {
  132. }
  133. /// <summary>
  134. /// Initializes a new instance of the <see cref="JValue"/> class with the given value.
  135. /// </summary>
  136. /// <param name="value">The value.</param>
  137. public JValue(string value)
  138. : this(value, JTokenType.String)
  139. {
  140. }
  141. /// <summary>
  142. /// Initializes a new instance of the <see cref="JValue"/> class with the given value.
  143. /// </summary>
  144. /// <param name="value">The value.</param>
  145. public JValue(Guid value)
  146. : this(value, JTokenType.Guid)
  147. {
  148. }
  149. /// <summary>
  150. /// Initializes a new instance of the <see cref="JValue"/> class with the given value.
  151. /// </summary>
  152. /// <param name="value">The value.</param>
  153. public JValue(Uri value)
  154. : this(value, (value != null) ? JTokenType.Uri : JTokenType.Null)
  155. {
  156. }
  157. /// <summary>
  158. /// Initializes a new instance of the <see cref="JValue"/> class with the given value.
  159. /// </summary>
  160. /// <param name="value">The value.</param>
  161. public JValue(TimeSpan value)
  162. : this(value, JTokenType.TimeSpan)
  163. {
  164. }
  165. /// <summary>
  166. /// Initializes a new instance of the <see cref="JValue"/> class with the given value.
  167. /// </summary>
  168. /// <param name="value">The value.</param>
  169. public JValue(object value)
  170. : this(value, GetValueType(null, value))
  171. {
  172. }
  173. internal override bool DeepEquals(JToken node)
  174. {
  175. if (!(node is JValue other))
  176. {
  177. return false;
  178. }
  179. if (other == this)
  180. {
  181. return true;
  182. }
  183. return ValuesEquals(this, other);
  184. }
  185. /// <summary>
  186. /// Gets a value indicating whether this token has child tokens.
  187. /// </summary>
  188. /// <value>
  189. /// <c>true</c> if this token has child values; otherwise, <c>false</c>.
  190. /// </value>
  191. public override bool HasValues => false;
  192. #if HAVE_BIG_INTEGER
  193. private static int CompareBigInteger(BigInteger i1, object i2)
  194. {
  195. int result = i1.CompareTo(ConvertUtils.ToBigInteger(i2));
  196. if (result != 0)
  197. {
  198. return result;
  199. }
  200. // converting a fractional number to a BigInteger will lose the fraction
  201. // check for fraction if result is two numbers are equal
  202. if (i2 is decimal d1)
  203. {
  204. return (0m).CompareTo(Math.Abs(d1 - Math.Truncate(d1)));
  205. }
  206. else if (i2 is double || i2 is float)
  207. {
  208. double d = Convert.ToDouble(i2, CultureInfo.InvariantCulture);
  209. return (0d).CompareTo(Math.Abs(d - Math.Truncate(d)));
  210. }
  211. return result;
  212. }
  213. #endif
  214. internal static int Compare(JTokenType valueType, object objA, object objB)
  215. {
  216. if (objA == objB)
  217. {
  218. return 0;
  219. }
  220. if (objB == null)
  221. {
  222. return 1;
  223. }
  224. if (objA == null)
  225. {
  226. return -1;
  227. }
  228. switch (valueType)
  229. {
  230. case JTokenType.Integer:
  231. {
  232. #if HAVE_BIG_INTEGER
  233. if (objA is BigInteger integerA)
  234. {
  235. return CompareBigInteger(integerA, objB);
  236. }
  237. if (objB is BigInteger integerB)
  238. {
  239. return -CompareBigInteger(integerB, objA);
  240. }
  241. #endif
  242. if (objA is ulong || objB is ulong || objA is decimal || objB is decimal)
  243. {
  244. return Convert.ToDecimal(objA, CultureInfo.InvariantCulture).CompareTo(Convert.ToDecimal(objB, CultureInfo.InvariantCulture));
  245. }
  246. else if (objA is float || objB is float || objA is double || objB is double)
  247. {
  248. return CompareFloat(objA, objB);
  249. }
  250. else
  251. {
  252. return Convert.ToInt64(objA, CultureInfo.InvariantCulture).CompareTo(Convert.ToInt64(objB, CultureInfo.InvariantCulture));
  253. }
  254. }
  255. case JTokenType.Float:
  256. {
  257. #if HAVE_BIG_INTEGER
  258. if (objA is BigInteger integerA)
  259. {
  260. return CompareBigInteger(integerA, objB);
  261. }
  262. if (objB is BigInteger integerB)
  263. {
  264. return -CompareBigInteger(integerB, objA);
  265. }
  266. #endif
  267. if (objA is ulong || objB is ulong || objA is decimal || objB is decimal)
  268. {
  269. return Convert.ToDecimal(objA, CultureInfo.InvariantCulture).CompareTo(Convert.ToDecimal(objB, CultureInfo.InvariantCulture));
  270. }
  271. return CompareFloat(objA, objB);
  272. }
  273. case JTokenType.Comment:
  274. case JTokenType.String:
  275. case JTokenType.Raw:
  276. string s1 = Convert.ToString(objA, CultureInfo.InvariantCulture);
  277. string s2 = Convert.ToString(objB, CultureInfo.InvariantCulture);
  278. return string.CompareOrdinal(s1, s2);
  279. case JTokenType.Boolean:
  280. bool b1 = Convert.ToBoolean(objA, CultureInfo.InvariantCulture);
  281. bool b2 = Convert.ToBoolean(objB, CultureInfo.InvariantCulture);
  282. return b1.CompareTo(b2);
  283. case JTokenType.Date:
  284. #if !NET20
  285. if (objA is DateTime dateA)
  286. {
  287. #else
  288. DateTime dateA = (DateTime)objA;
  289. #endif
  290. DateTime dateB;
  291. #if !NET20
  292. if (objB is DateTimeOffset offsetB)
  293. {
  294. dateB = offsetB.DateTime;
  295. }
  296. else
  297. #endif
  298. {
  299. dateB = Convert.ToDateTime(objB, CultureInfo.InvariantCulture);
  300. }
  301. return dateA.CompareTo(dateB);
  302. #if !NET20
  303. }
  304. else
  305. {
  306. DateTimeOffset offsetA = (DateTimeOffset)objA;
  307. if (!(objB is DateTimeOffset offsetB))
  308. {
  309. offsetB = new DateTimeOffset(Convert.ToDateTime(objB, CultureInfo.InvariantCulture));
  310. }
  311. return offsetA.CompareTo(offsetB);
  312. }
  313. #endif
  314. case JTokenType.Bytes:
  315. if (!(objB is byte[] bytesB))
  316. {
  317. throw new ArgumentException("Object must be of type byte[].");
  318. }
  319. byte[] bytesA = objA as byte[];
  320. Debug.Assert(bytesA != null);
  321. return MiscellaneousUtils.ByteArrayCompare(bytesA, bytesB);
  322. case JTokenType.Guid:
  323. if (!(objB is Guid))
  324. {
  325. throw new ArgumentException("Object must be of type Guid.");
  326. }
  327. Guid guid1 = (Guid)objA;
  328. Guid guid2 = (Guid)objB;
  329. return guid1.CompareTo(guid2);
  330. case JTokenType.Uri:
  331. Uri uri2 = objB as Uri;
  332. if (uri2 == null)
  333. {
  334. throw new ArgumentException("Object must be of type Uri.");
  335. }
  336. Uri uri1 = (Uri)objA;
  337. return Comparer<string>.Default.Compare(uri1.ToString(), uri2.ToString());
  338. case JTokenType.TimeSpan:
  339. if (!(objB is TimeSpan))
  340. {
  341. throw new ArgumentException("Object must be of type TimeSpan.");
  342. }
  343. TimeSpan ts1 = (TimeSpan)objA;
  344. TimeSpan ts2 = (TimeSpan)objB;
  345. return ts1.CompareTo(ts2);
  346. default:
  347. throw MiscellaneousUtils.CreateArgumentOutOfRangeException(nameof(valueType), valueType, "Unexpected value type: {0}".FormatWith(CultureInfo.InvariantCulture, valueType));
  348. }
  349. }
  350. private static int CompareFloat(object objA, object objB)
  351. {
  352. double d1 = Convert.ToDouble(objA, CultureInfo.InvariantCulture);
  353. double d2 = Convert.ToDouble(objB, CultureInfo.InvariantCulture);
  354. // take into account possible floating point errors
  355. if (MathUtils.ApproxEquals(d1, d2))
  356. {
  357. return 0;
  358. }
  359. return d1.CompareTo(d2);
  360. }
  361. #if !NET20
  362. private static bool Operation(ExpressionType operation, object objA, object objB, out object result)
  363. {
  364. if (objA is string || objB is string)
  365. {
  366. if (operation == ExpressionType.Add || operation == ExpressionType.AddAssign)
  367. {
  368. result = objA?.ToString() + objB?.ToString();
  369. return true;
  370. }
  371. }
  372. #if HAVE_BIG_INTEGER
  373. if (objA is BigInteger || objB is BigInteger)
  374. {
  375. if (objA == null || objB == null)
  376. {
  377. result = null;
  378. return true;
  379. }
  380. // not that this will lose the fraction
  381. // BigInteger doesn't have operators with non-integer types
  382. BigInteger i1 = ConvertUtils.ToBigInteger(objA);
  383. BigInteger i2 = ConvertUtils.ToBigInteger(objB);
  384. switch (operation)
  385. {
  386. case ExpressionType.Add:
  387. case ExpressionType.AddAssign:
  388. result = i1 + i2;
  389. return true;
  390. case ExpressionType.Subtract:
  391. case ExpressionType.SubtractAssign:
  392. result = i1 - i2;
  393. return true;
  394. case ExpressionType.Multiply:
  395. case ExpressionType.MultiplyAssign:
  396. result = i1 * i2;
  397. return true;
  398. case ExpressionType.Divide:
  399. case ExpressionType.DivideAssign:
  400. result = i1 / i2;
  401. return true;
  402. }
  403. }
  404. else
  405. #endif
  406. if (objA is ulong || objB is ulong || objA is decimal || objB is decimal)
  407. {
  408. if (objA == null || objB == null)
  409. {
  410. result = null;
  411. return true;
  412. }
  413. decimal d1 = Convert.ToDecimal(objA, CultureInfo.InvariantCulture);
  414. decimal d2 = Convert.ToDecimal(objB, CultureInfo.InvariantCulture);
  415. switch (operation)
  416. {
  417. case ExpressionType.Add:
  418. case ExpressionType.AddAssign:
  419. result = d1 + d2;
  420. return true;
  421. case ExpressionType.Subtract:
  422. case ExpressionType.SubtractAssign:
  423. result = d1 - d2;
  424. return true;
  425. case ExpressionType.Multiply:
  426. case ExpressionType.MultiplyAssign:
  427. result = d1 * d2;
  428. return true;
  429. case ExpressionType.Divide:
  430. case ExpressionType.DivideAssign:
  431. result = d1 / d2;
  432. return true;
  433. }
  434. }
  435. else if (objA is float || objB is float || objA is double || objB is double)
  436. {
  437. if (objA == null || objB == null)
  438. {
  439. result = null;
  440. return true;
  441. }
  442. double d1 = Convert.ToDouble(objA, CultureInfo.InvariantCulture);
  443. double d2 = Convert.ToDouble(objB, CultureInfo.InvariantCulture);
  444. switch (operation)
  445. {
  446. case ExpressionType.Add:
  447. case ExpressionType.AddAssign:
  448. result = d1 + d2;
  449. return true;
  450. case ExpressionType.Subtract:
  451. case ExpressionType.SubtractAssign:
  452. result = d1 - d2;
  453. return true;
  454. case ExpressionType.Multiply:
  455. case ExpressionType.MultiplyAssign:
  456. result = d1 * d2;
  457. return true;
  458. case ExpressionType.Divide:
  459. case ExpressionType.DivideAssign:
  460. result = d1 / d2;
  461. return true;
  462. }
  463. }
  464. else if (objA is int || objA is uint || objA is long || objA is short || objA is ushort || objA is sbyte || objA is byte ||
  465. objB is int || objB is uint || objB is long || objB is short || objB is ushort || objB is sbyte || objB is byte)
  466. {
  467. if (objA == null || objB == null)
  468. {
  469. result = null;
  470. return true;
  471. }
  472. long l1 = Convert.ToInt64(objA, CultureInfo.InvariantCulture);
  473. long l2 = Convert.ToInt64(objB, CultureInfo.InvariantCulture);
  474. switch (operation)
  475. {
  476. case ExpressionType.Add:
  477. case ExpressionType.AddAssign:
  478. result = l1 + l2;
  479. return true;
  480. case ExpressionType.Subtract:
  481. case ExpressionType.SubtractAssign:
  482. result = l1 - l2;
  483. return true;
  484. case ExpressionType.Multiply:
  485. case ExpressionType.MultiplyAssign:
  486. result = l1 * l2;
  487. return true;
  488. case ExpressionType.Divide:
  489. case ExpressionType.DivideAssign:
  490. result = l1 / l2;
  491. return true;
  492. }
  493. }
  494. result = null;
  495. return false;
  496. }
  497. #endif
  498. internal override JToken CloneToken()
  499. {
  500. return new JValue(this);
  501. }
  502. /// <summary>
  503. /// Creates a <see cref="JValue"/> comment with the given value.
  504. /// </summary>
  505. /// <param name="value">The value.</param>
  506. /// <returns>A <see cref="JValue"/> comment with the given value.</returns>
  507. public static JValue CreateComment(string value)
  508. {
  509. return new JValue(value, JTokenType.Comment);
  510. }
  511. /// <summary>
  512. /// Creates a <see cref="JValue"/> string with the given value.
  513. /// </summary>
  514. /// <param name="value">The value.</param>
  515. /// <returns>A <see cref="JValue"/> string with the given value.</returns>
  516. public static JValue CreateString(string value)
  517. {
  518. return new JValue(value, JTokenType.String);
  519. }
  520. /// <summary>
  521. /// Creates a <see cref="JValue"/> null value.
  522. /// </summary>
  523. /// <returns>A <see cref="JValue"/> null value.</returns>
  524. public static JValue CreateNull()
  525. {
  526. return new JValue(null, JTokenType.Null);
  527. }
  528. /// <summary>
  529. /// Creates a <see cref="JValue"/> undefined value.
  530. /// </summary>
  531. /// <returns>A <see cref="JValue"/> undefined value.</returns>
  532. public static JValue CreateUndefined()
  533. {
  534. return new JValue(null, JTokenType.Undefined);
  535. }
  536. private static JTokenType GetValueType(JTokenType? current, object value)
  537. {
  538. if (value == null)
  539. {
  540. return JTokenType.Null;
  541. }
  542. #if HAVE_ADO_NET
  543. else if (value == DBNull.Value)
  544. {
  545. return JTokenType.Null;
  546. }
  547. #endif
  548. else if (value is string)
  549. {
  550. return GetStringValueType(current);
  551. }
  552. else if (value is long || value is int || value is short || value is sbyte
  553. || value is ulong || value is uint || value is ushort || value is byte)
  554. {
  555. return JTokenType.Integer;
  556. }
  557. else if (value is Enum)
  558. {
  559. return JTokenType.Integer;
  560. }
  561. #if HAVE_BIG_INTEGER
  562. else if (value is BigInteger)
  563. {
  564. return JTokenType.Integer;
  565. }
  566. #endif
  567. else if (value is double || value is float || value is decimal)
  568. {
  569. return JTokenType.Float;
  570. }
  571. else if (value is DateTime)
  572. {
  573. return JTokenType.Date;
  574. }
  575. #if !NET20
  576. else if (value is DateTimeOffset)
  577. {
  578. return JTokenType.Date;
  579. }
  580. #endif
  581. else if (value is byte[])
  582. {
  583. return JTokenType.Bytes;
  584. }
  585. else if (value is bool)
  586. {
  587. return JTokenType.Boolean;
  588. }
  589. else if (value is Guid)
  590. {
  591. return JTokenType.Guid;
  592. }
  593. else if (value is Uri)
  594. {
  595. return JTokenType.Uri;
  596. }
  597. else if (value is TimeSpan)
  598. {
  599. return JTokenType.TimeSpan;
  600. }
  601. throw new ArgumentException("Could not determine JSON object type for type {0}.".FormatWith(CultureInfo.InvariantCulture, value.GetType()));
  602. }
  603. private static JTokenType GetStringValueType(JTokenType? current)
  604. {
  605. if (current == null)
  606. {
  607. return JTokenType.String;
  608. }
  609. switch (current.GetValueOrDefault())
  610. {
  611. case JTokenType.Comment:
  612. case JTokenType.String:
  613. case JTokenType.Raw:
  614. return current.GetValueOrDefault();
  615. default:
  616. return JTokenType.String;
  617. }
  618. }
  619. /// <summary>
  620. /// Gets the node type for this <see cref="JToken"/>.
  621. /// </summary>
  622. /// <value>The type.</value>
  623. public override JTokenType Type => _valueType;
  624. /// <summary>
  625. /// Gets or sets the underlying token value.
  626. /// </summary>
  627. /// <value>The underlying token value.</value>
  628. public object Value
  629. {
  630. get => _value;
  631. set
  632. {
  633. Type currentType = _value?.GetType();
  634. Type newType = value?.GetType();
  635. if (currentType != newType)
  636. {
  637. _valueType = GetValueType(_valueType, value);
  638. }
  639. _value = value;
  640. }
  641. }
  642. /// <summary>
  643. /// Writes this token to a <see cref="JsonWriter"/>.
  644. /// </summary>
  645. /// <param name="writer">A <see cref="JsonWriter"/> into which this method will write.</param>
  646. /// <param name="converters">A collection of <see cref="JsonConverter"/>s which will be used when writing the token.</param>
  647. public override void WriteTo(JsonWriter writer, params JsonConverter[] converters)
  648. {
  649. if (converters != null && converters.Length > 0 && _value != null)
  650. {
  651. JsonConverter matchingConverter = JsonSerializer.GetMatchingConverter(converters, _value.GetType());
  652. if (matchingConverter != null && matchingConverter.CanWrite)
  653. {
  654. matchingConverter.WriteJson(writer, _value, JsonSerializer.CreateDefault());
  655. return;
  656. }
  657. }
  658. switch (_valueType)
  659. {
  660. case JTokenType.Comment:
  661. writer.WriteComment(_value?.ToString());
  662. return;
  663. case JTokenType.Raw:
  664. writer.WriteRawValue(_value?.ToString());
  665. return;
  666. case JTokenType.Null:
  667. writer.WriteNull();
  668. return;
  669. case JTokenType.Undefined:
  670. writer.WriteUndefined();
  671. return;
  672. case JTokenType.Integer:
  673. if (_value is int i)
  674. {
  675. writer.WriteValue(i);
  676. }
  677. else if (_value is long l)
  678. {
  679. writer.WriteValue(l);
  680. }
  681. else if (_value is ulong ul)
  682. {
  683. writer.WriteValue(ul);
  684. }
  685. #if HAVE_BIG_INTEGER
  686. else if (_value is BigInteger integer)
  687. {
  688. writer.WriteValue(integer);
  689. }
  690. #endif
  691. else
  692. {
  693. writer.WriteValue(Convert.ToInt64(_value, CultureInfo.InvariantCulture));
  694. }
  695. return;
  696. case JTokenType.Float:
  697. if (_value is decimal dec)
  698. {
  699. writer.WriteValue(dec);
  700. }
  701. else if (_value is double d)
  702. {
  703. writer.WriteValue(d);
  704. }
  705. else if (_value is float f)
  706. {
  707. writer.WriteValue(f);
  708. }
  709. else
  710. {
  711. writer.WriteValue(Convert.ToDouble(_value, CultureInfo.InvariantCulture));
  712. }
  713. return;
  714. case JTokenType.String:
  715. writer.WriteValue(_value?.ToString());
  716. return;
  717. case JTokenType.Boolean:
  718. writer.WriteValue(Convert.ToBoolean(_value, CultureInfo.InvariantCulture));
  719. return;
  720. case JTokenType.Date:
  721. #if !NET20
  722. if (_value is DateTimeOffset offset)
  723. {
  724. writer.WriteValue(offset);
  725. }
  726. else
  727. #endif
  728. {
  729. writer.WriteValue(Convert.ToDateTime(_value, CultureInfo.InvariantCulture));
  730. }
  731. return;
  732. case JTokenType.Bytes:
  733. writer.WriteValue((byte[])_value);
  734. return;
  735. case JTokenType.Guid:
  736. writer.WriteValue((_value != null) ? (Guid?)_value : null);
  737. return;
  738. case JTokenType.TimeSpan:
  739. writer.WriteValue((_value != null) ? (TimeSpan?)_value : null);
  740. return;
  741. case JTokenType.Uri:
  742. writer.WriteValue((Uri)_value);
  743. return;
  744. }
  745. throw MiscellaneousUtils.CreateArgumentOutOfRangeException(nameof(Type), _valueType, "Unexpected token type.");
  746. }
  747. internal override int GetDeepHashCode()
  748. {
  749. int valueHashCode = (_value != null) ? _value.GetHashCode() : 0;
  750. // GetHashCode on an enum boxes so cast to int
  751. return ((int)_valueType).GetHashCode() ^ valueHashCode;
  752. }
  753. private static bool ValuesEquals(JValue v1, JValue v2)
  754. {
  755. return (v1 == v2 || (v1._valueType == v2._valueType && Compare(v1._valueType, v1._value, v2._value) == 0));
  756. }
  757. /// <summary>
  758. /// Indicates whether the current object is equal to another object of the same type.
  759. /// </summary>
  760. /// <returns>
  761. /// <c>true</c> if the current object is equal to the <paramref name="other"/> parameter; otherwise, <c>false</c>.
  762. /// </returns>
  763. /// <param name="other">An object to compare with this object.</param>
  764. public bool Equals(JValue other)
  765. {
  766. if (other == null)
  767. {
  768. return false;
  769. }
  770. return ValuesEquals(this, other);
  771. }
  772. /// <summary>
  773. /// Determines whether the specified <see cref="Object"/> is equal to the current <see cref="Object"/>.
  774. /// </summary>
  775. /// <param name="obj">The <see cref="Object"/> to compare with the current <see cref="Object"/>.</param>
  776. /// <returns>
  777. /// <c>true</c> if the specified <see cref="Object"/> is equal to the current <see cref="Object"/>; otherwise, <c>false</c>.
  778. /// </returns>
  779. public override bool Equals(object obj)
  780. {
  781. return Equals(obj as JValue);
  782. }
  783. /// <summary>
  784. /// Serves as a hash function for a particular type.
  785. /// </summary>
  786. /// <returns>
  787. /// A hash code for the current <see cref="Object"/>.
  788. /// </returns>
  789. public override int GetHashCode()
  790. {
  791. if (_value == null)
  792. {
  793. return 0;
  794. }
  795. return _value.GetHashCode();
  796. }
  797. /// <summary>
  798. /// Returns a <see cref="String"/> that represents this instance.
  799. /// </summary>
  800. /// <returns>
  801. /// A <see cref="String"/> that represents this instance.
  802. /// </returns>
  803. public override string ToString()
  804. {
  805. if (_value == null)
  806. {
  807. return string.Empty;
  808. }
  809. return _value.ToString();
  810. }
  811. /// <summary>
  812. /// Returns a <see cref="String"/> that represents this instance.
  813. /// </summary>
  814. /// <param name="format">The format.</param>
  815. /// <returns>
  816. /// A <see cref="String"/> that represents this instance.
  817. /// </returns>
  818. public string ToString(string format)
  819. {
  820. return ToString(format, CultureInfo.CurrentCulture);
  821. }
  822. /// <summary>
  823. /// Returns a <see cref="String"/> that represents this instance.
  824. /// </summary>
  825. /// <param name="formatProvider">The format provider.</param>
  826. /// <returns>
  827. /// A <see cref="String"/> that represents this instance.
  828. /// </returns>
  829. public string ToString(IFormatProvider formatProvider)
  830. {
  831. return ToString(null, formatProvider);
  832. }
  833. /// <summary>
  834. /// Returns a <see cref="String"/> that represents this instance.
  835. /// </summary>
  836. /// <param name="format">The format.</param>
  837. /// <param name="formatProvider">The format provider.</param>
  838. /// <returns>
  839. /// A <see cref="String"/> that represents this instance.
  840. /// </returns>
  841. public string ToString(string format, IFormatProvider formatProvider)
  842. {
  843. if (_value == null)
  844. {
  845. return string.Empty;
  846. }
  847. if (_value is IFormattable formattable)
  848. {
  849. return formattable.ToString(format, formatProvider);
  850. }
  851. else
  852. {
  853. return _value.ToString();
  854. }
  855. }
  856. #if !NET20
  857. /// <summary>
  858. /// Returns the <see cref="DynamicMetaObject"/> responsible for binding operations performed on this object.
  859. /// </summary>
  860. /// <param name="parameter">The expression tree representation of the runtime value.</param>
  861. /// <returns>
  862. /// The <see cref="DynamicMetaObject"/> to bind this object.
  863. /// </returns>
  864. protected override DynamicMetaObject GetMetaObject(Expression parameter)
  865. {
  866. return new DynamicProxyMetaObject<JValue>(parameter, this, new JValueDynamicProxy());
  867. }
  868. private class JValueDynamicProxy : DynamicProxy<JValue>
  869. {
  870. public override bool TryConvert(JValue instance, ConvertBinder binder, out object result)
  871. {
  872. if (binder.Type == typeof(JValue) || binder.Type == typeof(JToken))
  873. {
  874. result = instance;
  875. return true;
  876. }
  877. object value = instance.Value;
  878. if (value == null)
  879. {
  880. result = null;
  881. return ReflectionUtils.IsNullable(binder.Type);
  882. }
  883. result = ConvertUtils.Convert(value, CultureInfo.InvariantCulture, binder.Type);
  884. return true;
  885. }
  886. public override bool TryBinaryOperation(JValue instance, BinaryOperationBinder binder, object arg, out object result)
  887. {
  888. object compareValue = arg is JValue value ? value.Value : arg;
  889. switch (binder.Operation)
  890. {
  891. case ExpressionType.Equal:
  892. result = (Compare(instance.Type, instance.Value, compareValue) == 0);
  893. return true;
  894. case ExpressionType.NotEqual:
  895. result = (Compare(instance.Type, instance.Value, compareValue) != 0);
  896. return true;
  897. case ExpressionType.GreaterThan:
  898. result = (Compare(instance.Type, instance.Value, compareValue) > 0);
  899. return true;
  900. case ExpressionType.GreaterThanOrEqual:
  901. result = (Compare(instance.Type, instance.Value, compareValue) >= 0);
  902. return true;
  903. case ExpressionType.LessThan:
  904. result = (Compare(instance.Type, instance.Value, compareValue) < 0);
  905. return true;
  906. case ExpressionType.LessThanOrEqual:
  907. result = (Compare(instance.Type, instance.Value, compareValue) <= 0);
  908. return true;
  909. case ExpressionType.Add:
  910. case ExpressionType.AddAssign:
  911. case ExpressionType.Subtract:
  912. case ExpressionType.SubtractAssign:
  913. case ExpressionType.Multiply:
  914. case ExpressionType.MultiplyAssign:
  915. case ExpressionType.Divide:
  916. case ExpressionType.DivideAssign:
  917. if (Operation(binder.Operation, instance.Value, compareValue, out result))
  918. {
  919. result = new JValue(result);
  920. return true;
  921. }
  922. break;
  923. }
  924. result = null;
  925. return false;
  926. }
  927. }
  928. #endif
  929. int IComparable.CompareTo(object obj)
  930. {
  931. if (obj == null)
  932. {
  933. return 1;
  934. }
  935. JValue value = obj as JValue;
  936. JTokenType comparisonType;
  937. object otherValue;
  938. if (value != null)
  939. {
  940. otherValue = value.Value;
  941. comparisonType = (_valueType == JTokenType.String && _valueType != value._valueType)
  942. ? value._valueType
  943. : _valueType;
  944. }
  945. else
  946. {
  947. otherValue = obj;
  948. comparisonType = _valueType;
  949. }
  950. return Compare(comparisonType, _value, otherValue);
  951. }
  952. /// <summary>
  953. /// Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
  954. /// </summary>
  955. /// <param name="obj">An object to compare with this instance.</param>
  956. /// <returns>
  957. /// A 32-bit signed integer that indicates the relative order of the objects being compared. The return value has these meanings:
  958. /// Value
  959. /// Meaning
  960. /// Less than zero
  961. /// This instance is less than <paramref name="obj"/>.
  962. /// Zero
  963. /// This instance is equal to <paramref name="obj"/>.
  964. /// Greater than zero
  965. /// This instance is greater than <paramref name="obj"/>.
  966. /// </returns>
  967. /// <exception cref="ArgumentException">
  968. /// <paramref name="obj"/> is not of the same type as this instance.
  969. /// </exception>
  970. public int CompareTo(JValue obj)
  971. {
  972. if (obj == null)
  973. {
  974. return 1;
  975. }
  976. JTokenType comparisonType = (_valueType == JTokenType.String && _valueType != obj._valueType)
  977. ? obj._valueType
  978. : _valueType;
  979. return Compare(comparisonType, _value, obj._value);
  980. }
  981. TypeCode IConvertible.GetTypeCode()
  982. {
  983. if (_value == null)
  984. {
  985. return TypeCode.Empty;
  986. }
  987. if (_value is IConvertible convertable)
  988. {
  989. return convertable.GetTypeCode();
  990. }
  991. return TypeCode.Object;
  992. }
  993. bool IConvertible.ToBoolean(IFormatProvider provider)
  994. {
  995. return (bool)this;
  996. }
  997. char IConvertible.ToChar(IFormatProvider provider)
  998. {
  999. return (char)this;
  1000. }
  1001. sbyte IConvertible.ToSByte(IFormatProvider provider)
  1002. {
  1003. return (sbyte)this;
  1004. }
  1005. byte IConvertible.ToByte(IFormatProvider provider)
  1006. {
  1007. return (byte)this;
  1008. }
  1009. short IConvertible.ToInt16(IFormatProvider provider)
  1010. {
  1011. return (short)this;
  1012. }
  1013. ushort IConvertible.ToUInt16(IFormatProvider provider)
  1014. {
  1015. return (ushort)this;
  1016. }
  1017. int IConvertible.ToInt32(IFormatProvider provider)
  1018. {
  1019. return (int)this;
  1020. }
  1021. uint IConvertible.ToUInt32(IFormatProvider provider)
  1022. {
  1023. return (uint)this;
  1024. }
  1025. long IConvertible.ToInt64(IFormatProvider provider)
  1026. {
  1027. return (long)this;
  1028. }
  1029. ulong IConvertible.ToUInt64(IFormatProvider provider)
  1030. {
  1031. return (ulong)this;
  1032. }
  1033. float IConvertible.ToSingle(IFormatProvider provider)
  1034. {
  1035. return (float)this;
  1036. }
  1037. double IConvertible.ToDouble(IFormatProvider provider)
  1038. {
  1039. return (double)this;
  1040. }
  1041. decimal IConvertible.ToDecimal(IFormatProvider provider)
  1042. {
  1043. return (decimal)this;
  1044. }
  1045. DateTime IConvertible.ToDateTime(IFormatProvider provider)
  1046. {
  1047. return (DateTime)this;
  1048. }
  1049. object IConvertible.ToType(Type conversionType, IFormatProvider provider)
  1050. {
  1051. return ToObject(conversionType);
  1052. }
  1053. }
  1054. }