-
Notifications
You must be signed in to change notification settings - Fork 9.8k
/
ValidationProblemDetailsJsonConverter.cs
66 lines (56 loc) · 2.38 KB
/
ValidationProblemDetailsJsonConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Mvc.Core;
using static Microsoft.AspNetCore.Mvc.Infrastructure.ProblemDetailsJsonConverter;
namespace Microsoft.AspNetCore.Mvc.Infrastructure
{
internal class ValidationProblemDetailsJsonConverter : JsonConverter<ValidationProblemDetails>
{
private static readonly JsonEncodedText Errors = JsonEncodedText.Encode("errors");
public override ValidationProblemDetails Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var problemDetails = new ValidationProblemDetails();
if (reader.TokenType != JsonTokenType.StartObject)
{
throw new JsonException(Resources.UnexpectedJsonEnd);
}
while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
{
if (reader.ValueTextEquals(Errors.EncodedUtf8Bytes))
{
var errors = JsonSerializer.Deserialize<Dictionary<string, string[]>>(ref reader, options);
foreach (var item in errors)
{
problemDetails.Errors[item.Key] = item.Value;
}
}
else
{
ReadValue(ref reader, problemDetails, options);
}
}
if (reader.TokenType != JsonTokenType.EndObject)
{
throw new JsonException(Resources.UnexpectedJsonEnd);
}
return problemDetails;
}
public override void Write(Utf8JsonWriter writer, ValidationProblemDetails value, JsonSerializerOptions options)
{
writer.WriteStartObject();
WriteProblemDetails(writer, value, options);
writer.WriteStartObject(Errors);
foreach (var kvp in value.Errors)
{
writer.WritePropertyName(kvp.Key);
JsonSerializer.Serialize(writer, kvp.Value, kvp.Value?.GetType() ?? typeof(object), options);
}
writer.WriteEndObject();
writer.WriteEndObject();
}
}
}