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.

452 lines
20 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. using System;
  26. using System.Collections;
  27. using System.Collections.Generic;
  28. using System.Globalization;
  29. using System.Runtime.Serialization.Formatters;
  30. using Newtonsoft.Json.Serialization;
  31. using System.Runtime.Serialization;
  32. namespace Newtonsoft.Json
  33. {
  34. /// <summary>
  35. /// Specifies the settings on a <see cref="JsonSerializer"/> object.
  36. /// </summary>
  37. internal class JsonSerializerSettings
  38. {
  39. internal const ReferenceLoopHandling DefaultReferenceLoopHandling = ReferenceLoopHandling.Error;
  40. internal const MissingMemberHandling DefaultMissingMemberHandling = MissingMemberHandling.Ignore;
  41. internal const NullValueHandling DefaultNullValueHandling = NullValueHandling.Include;
  42. internal const DefaultValueHandling DefaultDefaultValueHandling = DefaultValueHandling.Include;
  43. internal const ObjectCreationHandling DefaultObjectCreationHandling = ObjectCreationHandling.Auto;
  44. internal const PreserveReferencesHandling DefaultPreserveReferencesHandling = PreserveReferencesHandling.None;
  45. internal const ConstructorHandling DefaultConstructorHandling = ConstructorHandling.Default;
  46. internal const TypeNameHandling DefaultTypeNameHandling = TypeNameHandling.None;
  47. internal const MetadataPropertyHandling DefaultMetadataPropertyHandling = MetadataPropertyHandling.Default;
  48. internal static readonly StreamingContext DefaultContext;
  49. internal const Formatting DefaultFormatting = Formatting.None;
  50. internal const DateFormatHandling DefaultDateFormatHandling = DateFormatHandling.IsoDateFormat;
  51. internal const DateTimeZoneHandling DefaultDateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind;
  52. internal const DateParseHandling DefaultDateParseHandling = DateParseHandling.DateTime;
  53. internal const FloatParseHandling DefaultFloatParseHandling = FloatParseHandling.Double;
  54. internal const FloatFormatHandling DefaultFloatFormatHandling = FloatFormatHandling.String;
  55. internal const StringEscapeHandling DefaultStringEscapeHandling = StringEscapeHandling.Default;
  56. internal const TypeNameAssemblyFormatHandling DefaultTypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple;
  57. internal static readonly CultureInfo DefaultCulture;
  58. internal const bool DefaultCheckAdditionalContent = false;
  59. internal const string DefaultDateFormatString = @"yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFFK";
  60. internal Formatting? _formatting;
  61. internal DateFormatHandling? _dateFormatHandling;
  62. internal DateTimeZoneHandling? _dateTimeZoneHandling;
  63. internal DateParseHandling? _dateParseHandling;
  64. internal FloatFormatHandling? _floatFormatHandling;
  65. internal FloatParseHandling? _floatParseHandling;
  66. internal StringEscapeHandling? _stringEscapeHandling;
  67. internal CultureInfo _culture;
  68. internal bool? _checkAdditionalContent;
  69. internal int? _maxDepth;
  70. internal bool _maxDepthSet;
  71. internal string _dateFormatString;
  72. internal bool _dateFormatStringSet;
  73. internal TypeNameAssemblyFormatHandling? _typeNameAssemblyFormatHandling;
  74. internal DefaultValueHandling? _defaultValueHandling;
  75. internal PreserveReferencesHandling? _preserveReferencesHandling;
  76. internal NullValueHandling? _nullValueHandling;
  77. internal ObjectCreationHandling? _objectCreationHandling;
  78. internal MissingMemberHandling? _missingMemberHandling;
  79. internal ReferenceLoopHandling? _referenceLoopHandling;
  80. internal StreamingContext? _context;
  81. internal ConstructorHandling? _constructorHandling;
  82. internal TypeNameHandling? _typeNameHandling;
  83. internal MetadataPropertyHandling? _metadataPropertyHandling;
  84. /// <summary>
  85. /// Gets or sets how reference loops (e.g. a class referencing itself) are handled.
  86. /// The default value is <see cref="Json.ReferenceLoopHandling.Error" />.
  87. /// </summary>
  88. /// <value>Reference loop handling.</value>
  89. public ReferenceLoopHandling ReferenceLoopHandling
  90. {
  91. get => _referenceLoopHandling ?? DefaultReferenceLoopHandling;
  92. set => _referenceLoopHandling = value;
  93. }
  94. /// <summary>
  95. /// Gets or sets how missing members (e.g. JSON contains a property that isn't a member on the object) are handled during deserialization.
  96. /// The default value is <see cref="Json.MissingMemberHandling.Ignore" />.
  97. /// </summary>
  98. /// <value>Missing member handling.</value>
  99. public MissingMemberHandling MissingMemberHandling
  100. {
  101. get => _missingMemberHandling ?? DefaultMissingMemberHandling;
  102. set => _missingMemberHandling = value;
  103. }
  104. /// <summary>
  105. /// Gets or sets how objects are created during deserialization.
  106. /// The default value is <see cref="Json.ObjectCreationHandling.Auto" />.
  107. /// </summary>
  108. /// <value>The object creation handling.</value>
  109. public ObjectCreationHandling ObjectCreationHandling
  110. {
  111. get => _objectCreationHandling ?? DefaultObjectCreationHandling;
  112. set => _objectCreationHandling = value;
  113. }
  114. /// <summary>
  115. /// Gets or sets how null values are handled during serialization and deserialization.
  116. /// The default value is <see cref="Json.NullValueHandling.Include" />.
  117. /// </summary>
  118. /// <value>Null value handling.</value>
  119. public NullValueHandling NullValueHandling
  120. {
  121. get => _nullValueHandling ?? DefaultNullValueHandling;
  122. set => _nullValueHandling = value;
  123. }
  124. /// <summary>
  125. /// Gets or sets how default values are handled during serialization and deserialization.
  126. /// The default value is <see cref="Json.DefaultValueHandling.Include" />.
  127. /// </summary>
  128. /// <value>The default value handling.</value>
  129. public DefaultValueHandling DefaultValueHandling
  130. {
  131. get => _defaultValueHandling ?? DefaultDefaultValueHandling;
  132. set => _defaultValueHandling = value;
  133. }
  134. /// <summary>
  135. /// Gets or sets a <see cref="JsonConverter"/> collection that will be used during serialization.
  136. /// </summary>
  137. /// <value>The converters.</value>
  138. public IList<JsonConverter> Converters { get; set; }
  139. /// <summary>
  140. /// Gets or sets how object references are preserved by the serializer.
  141. /// The default value is <see cref="Json.PreserveReferencesHandling.None" />.
  142. /// </summary>
  143. /// <value>The preserve references handling.</value>
  144. public PreserveReferencesHandling PreserveReferencesHandling
  145. {
  146. get => _preserveReferencesHandling ?? DefaultPreserveReferencesHandling;
  147. set => _preserveReferencesHandling = value;
  148. }
  149. /// <summary>
  150. /// Gets or sets how type name writing and reading is handled by the serializer.
  151. /// The default value is <see cref="Json.TypeNameHandling.None" />.
  152. /// </summary>
  153. /// <remarks>
  154. /// <see cref="JsonSerializerSettings.TypeNameHandling"/> should be used with caution when your application deserializes JSON from an external source.
  155. /// Incoming types should be validated with a custom <see cref="JsonSerializerSettings.SerializationBinder"/>
  156. /// when deserializing with a value other than <see cref="Json.TypeNameHandling.None"/>.
  157. /// </remarks>
  158. /// <value>The type name handling.</value>
  159. public TypeNameHandling TypeNameHandling
  160. {
  161. get => _typeNameHandling ?? DefaultTypeNameHandling;
  162. set => _typeNameHandling = value;
  163. }
  164. /// <summary>
  165. /// Gets or sets how metadata properties are used during deserialization.
  166. /// The default value is <see cref="Json.MetadataPropertyHandling.Default" />.
  167. /// </summary>
  168. /// <value>The metadata properties handling.</value>
  169. public MetadataPropertyHandling MetadataPropertyHandling
  170. {
  171. get => _metadataPropertyHandling ?? DefaultMetadataPropertyHandling;
  172. set => _metadataPropertyHandling = value;
  173. }
  174. /// <summary>
  175. /// Gets or sets how a type name assembly is written and resolved by the serializer.
  176. /// The default value is <see cref="FormatterAssemblyStyle.Simple" />.
  177. /// </summary>
  178. /// <value>The type name assembly format.</value>
  179. [Obsolete("TypeNameAssemblyFormat is obsolete. Use TypeNameAssemblyFormatHandling instead.")]
  180. public FormatterAssemblyStyle TypeNameAssemblyFormat
  181. {
  182. get => (FormatterAssemblyStyle)TypeNameAssemblyFormatHandling;
  183. set => TypeNameAssemblyFormatHandling = (TypeNameAssemblyFormatHandling)value;
  184. }
  185. /// <summary>
  186. /// Gets or sets how a type name assembly is written and resolved by the serializer.
  187. /// The default value is <see cref="Json.TypeNameAssemblyFormatHandling.Simple" />.
  188. /// </summary>
  189. /// <value>The type name assembly format.</value>
  190. public TypeNameAssemblyFormatHandling TypeNameAssemblyFormatHandling
  191. {
  192. get => _typeNameAssemblyFormatHandling ?? DefaultTypeNameAssemblyFormatHandling;
  193. set => _typeNameAssemblyFormatHandling = value;
  194. }
  195. /// <summary>
  196. /// Gets or sets how constructors are used during deserialization.
  197. /// The default value is <see cref="Json.ConstructorHandling.Default" />.
  198. /// </summary>
  199. /// <value>The constructor handling.</value>
  200. public ConstructorHandling ConstructorHandling
  201. {
  202. get => _constructorHandling ?? DefaultConstructorHandling;
  203. set => _constructorHandling = value;
  204. }
  205. /// <summary>
  206. /// Gets or sets the contract resolver used by the serializer when
  207. /// serializing .NET objects to JSON and vice versa.
  208. /// </summary>
  209. /// <value>The contract resolver.</value>
  210. public IContractResolver ContractResolver { get; set; }
  211. /// <summary>
  212. /// Gets or sets the equality comparer used by the serializer when comparing references.
  213. /// </summary>
  214. /// <value>The equality comparer.</value>
  215. public IEqualityComparer EqualityComparer { get; set; }
  216. /// <summary>
  217. /// Gets or sets the <see cref="IReferenceResolver"/> used by the serializer when resolving references.
  218. /// </summary>
  219. /// <value>The reference resolver.</value>
  220. [Obsolete("ReferenceResolver property is obsolete. Use the ReferenceResolverProvider property to set the IReferenceResolver: settings.ReferenceResolverProvider = () => resolver")]
  221. public IReferenceResolver ReferenceResolver
  222. {
  223. get => ReferenceResolverProvider?.Invoke();
  224. set
  225. {
  226. ReferenceResolverProvider = (value != null)
  227. ? () => value
  228. : (Func<IReferenceResolver>)null;
  229. }
  230. }
  231. /// <summary>
  232. /// Gets or sets a function that creates the <see cref="IReferenceResolver"/> used by the serializer when resolving references.
  233. /// </summary>
  234. /// <value>A function that creates the <see cref="IReferenceResolver"/> used by the serializer when resolving references.</value>
  235. public Func<IReferenceResolver> ReferenceResolverProvider { get; set; }
  236. /// <summary>
  237. /// Gets or sets the <see cref="ITraceWriter"/> used by the serializer when writing trace messages.
  238. /// </summary>
  239. /// <value>The trace writer.</value>
  240. public ITraceWriter TraceWriter { get; set; }
  241. /// <summary>
  242. /// Gets or sets the <see cref="SerializationBinder"/> used by the serializer when resolving type names.
  243. /// </summary>
  244. /// <value>The binder.</value>
  245. [Obsolete("Binder is obsolete. Use SerializationBinder instead.")]
  246. public SerializationBinder Binder
  247. {
  248. get
  249. {
  250. if (SerializationBinder == null)
  251. {
  252. return null;
  253. }
  254. if (SerializationBinder is SerializationBinderAdapter adapter)
  255. {
  256. return adapter.SerializationBinder;
  257. }
  258. throw new InvalidOperationException("Cannot get SerializationBinder because an ISerializationBinder was previously set.");
  259. }
  260. set => SerializationBinder = value == null ? null : new SerializationBinderAdapter(value);
  261. }
  262. /// <summary>
  263. /// Gets or sets the <see cref="ISerializationBinder"/> used by the serializer when resolving type names.
  264. /// </summary>
  265. /// <value>The binder.</value>
  266. public ISerializationBinder SerializationBinder { get; set; }
  267. /// <summary>
  268. /// Gets or sets the error handler called during serialization and deserialization.
  269. /// </summary>
  270. /// <value>The error handler called during serialization and deserialization.</value>
  271. public EventHandler<ErrorEventArgs> Error { get; set; }
  272. /// <summary>
  273. /// Gets or sets the <see cref="StreamingContext"/> used by the serializer when invoking serialization callback methods.
  274. /// </summary>
  275. /// <value>The context.</value>
  276. public StreamingContext Context
  277. {
  278. get => _context ?? DefaultContext;
  279. set => _context = value;
  280. }
  281. /// <summary>
  282. /// Gets or sets how <see cref="DateTime"/> and <see cref="DateTimeOffset"/> values are formatted when writing JSON text,
  283. /// and the expected date format when reading JSON text.
  284. /// The default value is <c>"yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFFK"</c>.
  285. /// </summary>
  286. public string DateFormatString
  287. {
  288. get => _dateFormatString ?? DefaultDateFormatString;
  289. set
  290. {
  291. _dateFormatString = value;
  292. _dateFormatStringSet = true;
  293. }
  294. }
  295. /// <summary>
  296. /// Gets or sets the maximum depth allowed when reading JSON. Reading past this depth will throw a <see cref="JsonReaderException"/>.
  297. /// A null value means there is no maximum.
  298. /// The default value is <c>null</c>.
  299. /// </summary>
  300. public int? MaxDepth
  301. {
  302. get => _maxDepth;
  303. set
  304. {
  305. if (value <= 0)
  306. {
  307. throw new ArgumentException("Value must be positive.", nameof(value));
  308. }
  309. _maxDepth = value;
  310. _maxDepthSet = true;
  311. }
  312. }
  313. /// <summary>
  314. /// Indicates how JSON text output is formatted.
  315. /// The default value is <see cref="Json.Formatting.None" />.
  316. /// </summary>
  317. public Formatting Formatting
  318. {
  319. get => _formatting ?? DefaultFormatting;
  320. set => _formatting = value;
  321. }
  322. /// <summary>
  323. /// Gets or sets how dates are written to JSON text.
  324. /// The default value is <see cref="Json.DateFormatHandling.IsoDateFormat" />.
  325. /// </summary>
  326. public DateFormatHandling DateFormatHandling
  327. {
  328. get => _dateFormatHandling ?? DefaultDateFormatHandling;
  329. set => _dateFormatHandling = value;
  330. }
  331. /// <summary>
  332. /// Gets or sets how <see cref="DateTime"/> time zones are handled during serialization and deserialization.
  333. /// The default value is <see cref="Json.DateTimeZoneHandling.RoundtripKind" />.
  334. /// </summary>
  335. public DateTimeZoneHandling DateTimeZoneHandling
  336. {
  337. get => _dateTimeZoneHandling ?? DefaultDateTimeZoneHandling;
  338. set => _dateTimeZoneHandling = value;
  339. }
  340. /// <summary>
  341. /// Gets or sets how date formatted strings, e.g. <c>"\/Date(1198908717056)\/"</c> and <c>"2012-03-21T05:40Z"</c>, are parsed when reading JSON.
  342. /// The default value is <see cref="Json.DateParseHandling.DateTime" />.
  343. /// </summary>
  344. public DateParseHandling DateParseHandling
  345. {
  346. get => _dateParseHandling ?? DefaultDateParseHandling;
  347. set => _dateParseHandling = value;
  348. }
  349. /// <summary>
  350. /// Gets or sets how special floating point numbers, e.g. <see cref="Double.NaN"/>,
  351. /// <see cref="Double.PositiveInfinity"/> and <see cref="Double.NegativeInfinity"/>,
  352. /// are written as JSON.
  353. /// The default value is <see cref="Json.FloatFormatHandling.String" />.
  354. /// </summary>
  355. public FloatFormatHandling FloatFormatHandling
  356. {
  357. get => _floatFormatHandling ?? DefaultFloatFormatHandling;
  358. set => _floatFormatHandling = value;
  359. }
  360. /// <summary>
  361. /// Gets or sets how floating point numbers, e.g. 1.0 and 9.9, are parsed when reading JSON text.
  362. /// The default value is <see cref="Json.FloatParseHandling.Double" />.
  363. /// </summary>
  364. public FloatParseHandling FloatParseHandling
  365. {
  366. get => _floatParseHandling ?? DefaultFloatParseHandling;
  367. set => _floatParseHandling = value;
  368. }
  369. /// <summary>
  370. /// Gets or sets how strings are escaped when writing JSON text.
  371. /// The default value is <see cref="Json.StringEscapeHandling.Default" />.
  372. /// </summary>
  373. public StringEscapeHandling StringEscapeHandling
  374. {
  375. get => _stringEscapeHandling ?? DefaultStringEscapeHandling;
  376. set => _stringEscapeHandling = value;
  377. }
  378. /// <summary>
  379. /// Gets or sets the culture used when reading JSON.
  380. /// The default value is <see cref="CultureInfo.InvariantCulture"/>.
  381. /// </summary>
  382. public CultureInfo Culture
  383. {
  384. get => _culture ?? DefaultCulture;
  385. set => _culture = value;
  386. }
  387. /// <summary>
  388. /// Gets a value indicating whether there will be a check for additional content after deserializing an object.
  389. /// The default value is <c>false</c>.
  390. /// </summary>
  391. /// <value>
  392. /// <c>true</c> if there will be a check for additional content after deserializing an object; otherwise, <c>false</c>.
  393. /// </value>
  394. public bool CheckAdditionalContent
  395. {
  396. get => _checkAdditionalContent ?? DefaultCheckAdditionalContent;
  397. set => _checkAdditionalContent = value;
  398. }
  399. static JsonSerializerSettings()
  400. {
  401. DefaultContext = new StreamingContext();
  402. DefaultCulture = CultureInfo.InvariantCulture;
  403. }
  404. /// <summary>
  405. /// Initializes a new instance of the <see cref="JsonSerializerSettings"/> class.
  406. /// </summary>
  407. public JsonSerializerSettings()
  408. {
  409. Converters = new List<JsonConverter>();
  410. }
  411. }
  412. }