1
1
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2
2
3
+ // Ignore Spelling: Serializer
3
4
namespace Asp . Versioning ;
4
5
5
6
using Microsoft . AspNetCore . Http ;
@@ -30,6 +31,19 @@ public partial class ErrorObjectWriter : IProblemDetailsWriter
30
31
public ErrorObjectWriter ( IOptions < JsonOptions > options ) =>
31
32
this . options = ( options ?? throw new ArgumentNullException ( nameof ( options ) ) ) . Value . SerializerOptions ;
32
33
34
+ /// <summary>
35
+ /// Gets the associated, default <see cref="JsonSerializerContext"/>.
36
+ /// </summary>
37
+ /// <value>The associated, default <see cref="JsonSerializerContext"/>.</value>
38
+ public static JsonSerializerContext DefaultJsonSerializerContext => ErrorObjectJsonContext . Default ;
39
+
40
+ /// <summary>
41
+ /// Creates and returns a new <see cref="JsonSerializerContext"/> associated with the writer.
42
+ /// </summary>
43
+ /// <param name="options">The <see cref="JsonSerializerOptions">JSON serializer options</see> to use.</param>
44
+ /// <returns>A new <see cref="JsonSerializerContext"/>.</returns>
45
+ public static JsonSerializerContext NewJsonSerializerContext ( JsonSerializerOptions options ) => new ErrorObjectJsonContext ( options ) ;
46
+
33
47
/// <inheritdoc />
34
48
public virtual bool CanWrite ( ProblemDetailsContext context )
35
49
{
@@ -89,6 +103,7 @@ internal ErrorObject( ProblemDetails problemDetails ) =>
89
103
/// </summary>
90
104
protected internal readonly partial struct ErrorDetail
91
105
{
106
+ private const string CodeProperty = "code" ;
92
107
private readonly ProblemDetails problemDetails ;
93
108
private readonly InnerError ? innerError ;
94
109
private readonly Dictionary < string , object > extensions = [ ] ;
@@ -103,23 +118,21 @@ internal ErrorDetail( ProblemDetails problemDetails )
103
118
/// Gets or sets one of a server-defined set of error codes.
104
119
/// </summary>
105
120
/// <value>A server-defined error code.</value>
106
- [ JsonPropertyName ( "code" ) ]
121
+ [ JsonPropertyName ( CodeProperty ) ]
107
122
[ JsonIgnore ( Condition = WhenWritingNull ) ]
108
123
public string ? Code
109
124
{
110
- get => problemDetails . Extensions . TryGetValue ( "code" , out var value ) &&
111
- value is string code ?
112
- code :
113
- default ;
125
+ get => problemDetails . Extensions . TryGetValue ( CodeProperty , out var value ) &&
126
+ value is string code ? code : default ;
114
127
set
115
128
{
116
129
if ( value is null )
117
130
{
118
- problemDetails . Extensions . Remove ( "code" ) ;
131
+ problemDetails . Extensions . Remove ( CodeProperty ) ;
119
132
}
120
133
else
121
134
{
122
- problemDetails . Extensions [ "code" ] = value ;
135
+ problemDetails . Extensions [ CodeProperty ] = value ;
123
136
}
124
137
}
125
138
}
0 commit comments