Skip to content

Commit acedeb2

Browse files
Extend JsonSourceGenerationOptionsAttribute to have feature parity with JsonSerializerOptions. (#88753)
1 parent d029ecd commit acedeb2

18 files changed

+619
-167
lines changed

src/libraries/System.Text.Json/Common/JsonSourceGenerationOptionsAttribute.cs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,58 @@ namespace System.Text.Json.Serialization
1515
#endif
1616
sealed class JsonSourceGenerationOptionsAttribute : JsonAttribute
1717
{
18+
/// <summary>
19+
/// Constructs a new <see cref="JsonSourceGenerationOptionsAttribute"/> instance.
20+
/// </summary>
21+
public JsonSourceGenerationOptionsAttribute() { }
22+
23+
/// <summary>
24+
/// Constructs a new <see cref="JsonSourceGenerationOptionsAttribute"/> instance with a predefined set of options determined by the specified <see cref="JsonSerializerDefaults"/>.
25+
/// </summary>
26+
/// <param name="defaults">The <see cref="JsonSerializerDefaults"/> to reason about.</param>
27+
/// <exception cref="ArgumentOutOfRangeException">Invalid <paramref name="defaults"/> parameter.</exception>
28+
public JsonSourceGenerationOptionsAttribute(JsonSerializerDefaults defaults)
29+
{
30+
// Constructor kept in sync with equivalent overload in JsonSerializerOptions
31+
32+
if (defaults is JsonSerializerDefaults.Web)
33+
{
34+
PropertyNameCaseInsensitive = true;
35+
PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase;
36+
NumberHandling = JsonNumberHandling.AllowReadingFromString;
37+
}
38+
else if (defaults is not JsonSerializerDefaults.General)
39+
{
40+
throw new ArgumentOutOfRangeException(nameof(defaults));
41+
}
42+
}
43+
44+
/// <summary>
45+
/// Defines whether an extra comma at the end of a list of JSON values in an object or array
46+
/// is allowed (and ignored) within the JSON payload being deserialized.
47+
/// </summary>
48+
public bool AllowTrailingCommas { get; set; }
49+
50+
/// <summary>
51+
/// Specifies a list of custom converter types to be used.
52+
/// </summary>
53+
public Type[]? Converters { get; set; }
54+
55+
/// <summary>
56+
/// The default buffer size in bytes used when creating temporary buffers.
57+
/// </summary>
58+
public int DefaultBufferSize { get; set; }
59+
1860
/// <summary>
1961
/// Specifies the default ignore condition.
2062
/// </summary>
2163
public JsonIgnoreCondition DefaultIgnoreCondition { get; set; }
2264

65+
/// <summary>
66+
/// Specifies the policy used to convert a dictionary key to another format, such as camel-casing.
67+
/// </summary>
68+
public JsonKnownNamingPolicy DictionaryKeyPolicy { get; set; }
69+
2370
/// <summary>
2471
/// Specifies whether to ignore read-only fields.
2572
/// </summary>
@@ -35,11 +82,47 @@ sealed class JsonSourceGenerationOptionsAttribute : JsonAttribute
3582
/// </summary>
3683
public bool IncludeFields { get; set; }
3784

85+
/// <summary>
86+
/// Gets or sets the maximum depth allowed when serializing or deserializing JSON, with the default (i.e. 0) indicating a max depth of 64.
87+
/// </summary>
88+
public int MaxDepth { get; set; }
89+
90+
/// <summary>
91+
/// Specifies how number types should be handled when serializing or deserializing.
92+
/// </summary>
93+
public JsonNumberHandling NumberHandling { get; set; }
94+
95+
/// <summary>
96+
/// Specifies preferred object creation handling for properties when deserializing JSON.
97+
/// </summary>
98+
public JsonObjectCreationHandling PreferredObjectCreationHandling { get; set; }
99+
100+
/// <summary>
101+
/// Determines whether a property name uses a case-insensitive comparison during deserialization.
102+
/// </summary>
103+
public bool PropertyNameCaseInsensitive { get; set; }
104+
38105
/// <summary>
39106
/// Specifies a built-in naming polices to convert JSON property names with.
40107
/// </summary>
41108
public JsonKnownNamingPolicy PropertyNamingPolicy { get; set; }
42109

110+
/// <summary>
111+
/// Defines how JSON comments are handled during deserialization.
112+
/// </summary>
113+
public JsonCommentHandling ReadCommentHandling { get; set; }
114+
115+
/// <summary>
116+
/// Defines how deserializing a type declared as an <see cref="object"/> is handled during deserialization.
117+
/// </summary>
118+
public JsonUnknownTypeHandling UnknownTypeHandling { get; set; }
119+
120+
/// <summary>
121+
/// Determines how <see cref="JsonSerializer"/> handles JSON properties that
122+
/// cannot be mapped to a specific .NET member when deserializing object types.
123+
/// </summary>
124+
public JsonUnmappedMemberHandling UnmappedMemberHandling { get; set; }
125+
43126
/// <summary>
44127
/// Specifies whether JSON output should be pretty-printed.
45128
/// </summary>

0 commit comments

Comments
 (0)