| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | |
| | | 6 | | namespace System.Text.Json |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// Provides the ability for the user to define custom behavior when parsing JSON to create a <see cref="JsonDocumen |
| | | 10 | | /// </summary> |
| | | 11 | | public struct JsonDocumentOptions |
| | | 12 | | { |
| | | 13 | | internal const int DefaultMaxDepth = 64; |
| | | 14 | | |
| | | 15 | | private int _maxDepth; |
| | | 16 | | private JsonCommentHandling _commentHandling; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Defines how the <see cref="Utf8JsonReader"/> should handle comments when reading through the JSON. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <exception cref="ArgumentOutOfRangeException"> |
| | | 22 | | /// Thrown when the comment handling enum is set to a value that is not supported (or not within the <see cref=" |
| | | 23 | | /// </exception> |
| | | 24 | | /// <remarks> |
| | | 25 | | /// By default <exception cref="JsonException"/> is thrown if a comment is encountered. |
| | | 26 | | /// </remarks> |
| | | 27 | | public JsonCommentHandling CommentHandling |
| | | 28 | | { |
| | 0 | 29 | | readonly get => _commentHandling; |
| | | 30 | | set |
| | 0 | 31 | | { |
| | 0 | 32 | | Debug.Assert(value >= 0); |
| | 0 | 33 | | if (value > JsonCommentHandling.Skip) |
| | 0 | 34 | | throw new ArgumentOutOfRangeException(nameof(value), SR.JsonDocumentDoesNotSupportComments); |
| | | 35 | | |
| | 0 | 36 | | _commentHandling = value; |
| | 0 | 37 | | } |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Gets or sets the maximum depth allowed when reading JSON, with the default (i.e. 0) indicating a max depth o |
| | | 42 | | /// </summary> |
| | | 43 | | /// <exception cref="ArgumentOutOfRangeException"> |
| | | 44 | | /// Thrown when the max depth is set to a negative value. |
| | | 45 | | /// </exception> |
| | | 46 | | /// <remarks> |
| | | 47 | | /// Reading past this depth will throw a <exception cref="JsonException"/>. |
| | | 48 | | /// </remarks> |
| | | 49 | | public int MaxDepth |
| | | 50 | | { |
| | 0 | 51 | | readonly get => _maxDepth; |
| | | 52 | | set |
| | 0 | 53 | | { |
| | 0 | 54 | | if (value < 0) |
| | 0 | 55 | | { |
| | 0 | 56 | | ThrowHelper.ThrowArgumentOutOfRangeException_MaxDepthMustBePositive(nameof(value)); |
| | | 57 | | } |
| | | 58 | | |
| | 0 | 59 | | _maxDepth = value; |
| | 0 | 60 | | } |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Defines whether an extra comma at the end of a list of JSON values in an object or array |
| | | 65 | | /// is allowed (and ignored) within the JSON payload being read. |
| | | 66 | | /// </summary> |
| | | 67 | | /// <remarks> |
| | | 68 | | /// By default, it's set to false, and <exception cref="JsonException"/> is thrown if a trailing comma is encoun |
| | | 69 | | /// </remarks> |
| | 0 | 70 | | public bool AllowTrailingCommas { get; set; } |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Defines whether duplicate property names are allowed when deserializing JSON objects. |
| | | 74 | | /// </summary> |
| | | 75 | | /// <remarks> |
| | | 76 | | /// <para> |
| | | 77 | | /// By default, it's set to true. If set to false, <see cref="JsonException"/> is thrown |
| | | 78 | | /// when a duplicate property name is encountered during deserialization. |
| | | 79 | | /// </para> |
| | | 80 | | /// </remarks> |
| | | 81 | | public bool AllowDuplicateProperties |
| | | 82 | | { |
| | | 83 | | // These are negated because the declaring type is a struct and we want the value to be true |
| | | 84 | | // for the default struct value. |
| | 0 | 85 | | get => !field; |
| | 0 | 86 | | set => field = !value; |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | internal JsonReaderOptions GetReaderOptions() |
| | 0 | 90 | | { |
| | 0 | 91 | | return new JsonReaderOptions |
| | 0 | 92 | | { |
| | 0 | 93 | | AllowTrailingCommas = AllowTrailingCommas, |
| | 0 | 94 | | CommentHandling = CommentHandling, |
| | 0 | 95 | | MaxDepth = MaxDepth |
| | 0 | 96 | | }; |
| | 0 | 97 | | } |
| | | 98 | | } |
| | | 99 | | } |