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.

159 lines
4.6 KiB

  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. #if HAVE_BINARY_SERIALIZATION
  26. using System;
  27. using System.Globalization;
  28. using System.Runtime.Serialization;
  29. using Newtonsoft.Json.Utilities;
  30. using Newtonsoft.Json.Linq;
  31. namespace Newtonsoft.Json.Serialization
  32. {
  33. internal class JsonFormatterConverter : IFormatterConverter
  34. {
  35. private readonly JsonSerializerInternalReader _reader;
  36. private readonly JsonISerializableContract _contract;
  37. private readonly JsonProperty _member;
  38. public JsonFormatterConverter(JsonSerializerInternalReader reader, JsonISerializableContract contract, JsonProperty member)
  39. {
  40. ValidationUtils.ArgumentNotNull(reader, nameof(reader));
  41. ValidationUtils.ArgumentNotNull(contract, nameof(contract));
  42. _reader = reader;
  43. _contract = contract;
  44. _member = member;
  45. }
  46. private T GetTokenValue<T>(object value)
  47. {
  48. ValidationUtils.ArgumentNotNull(value, nameof(value));
  49. JValue v = (JValue)value;
  50. return (T)System.Convert.ChangeType(v.Value, typeof(T), CultureInfo.InvariantCulture);
  51. }
  52. public object Convert(object value, Type type)
  53. {
  54. ValidationUtils.ArgumentNotNull(value, nameof(value));
  55. if (!(value is JToken token))
  56. {
  57. throw new ArgumentException("Value is not a JToken.", nameof(value));
  58. }
  59. return _reader.CreateISerializableItem(token, type, _contract, _member);
  60. }
  61. public object Convert(object value, TypeCode typeCode)
  62. {
  63. ValidationUtils.ArgumentNotNull(value, nameof(value));
  64. if (value is JValue)
  65. {
  66. value = ((JValue)value).Value;
  67. }
  68. return System.Convert.ChangeType(value, typeCode, CultureInfo.InvariantCulture);
  69. }
  70. public bool ToBoolean(object value)
  71. {
  72. return GetTokenValue<bool>(value);
  73. }
  74. public byte ToByte(object value)
  75. {
  76. return GetTokenValue<byte>(value);
  77. }
  78. public char ToChar(object value)
  79. {
  80. return GetTokenValue<char>(value);
  81. }
  82. public DateTime ToDateTime(object value)
  83. {
  84. return GetTokenValue<DateTime>(value);
  85. }
  86. public decimal ToDecimal(object value)
  87. {
  88. return GetTokenValue<decimal>(value);
  89. }
  90. public double ToDouble(object value)
  91. {
  92. return GetTokenValue<double>(value);
  93. }
  94. public short ToInt16(object value)
  95. {
  96. return GetTokenValue<short>(value);
  97. }
  98. public int ToInt32(object value)
  99. {
  100. return GetTokenValue<int>(value);
  101. }
  102. public long ToInt64(object value)
  103. {
  104. return GetTokenValue<long>(value);
  105. }
  106. public sbyte ToSByte(object value)
  107. {
  108. return GetTokenValue<sbyte>(value);
  109. }
  110. public float ToSingle(object value)
  111. {
  112. return GetTokenValue<float>(value);
  113. }
  114. public string ToString(object value)
  115. {
  116. return GetTokenValue<string>(value);
  117. }
  118. public ushort ToUInt16(object value)
  119. {
  120. return GetTokenValue<ushort>(value);
  121. }
  122. public uint ToUInt32(object value)
  123. {
  124. return GetTokenValue<uint>(value);
  125. }
  126. public ulong ToUInt64(object value)
  127. {
  128. return GetTokenValue<ulong>(value);
  129. }
  130. }
  131. }
  132. #endif