Merge pull request #1235 from DsAekb/main

This commit is contained in:
Claire Novotny 2021-09-04 16:13:50 -04:00 коммит произвёл GitHub
Родитель c0294ede1c 292ac57b8e
Коммит 39553f245b
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
1 изменённых файлов: 16 добавлений и 8 удалений

Просмотреть файл

@ -29,15 +29,8 @@ namespace Refit
/// <summary>
/// Creates a new <see cref="SystemTextJsonContentSerializer"/> instance
/// </summary>
public SystemTextJsonContentSerializer() : this(new JsonSerializerOptions())
public SystemTextJsonContentSerializer() : this(GetDefaultJsonSerializerOptions())
{
// Set some defaults
// Default to case insensitive property name matching as that's likely the behavior most users expect
jsonSerializerOptions.PropertyNameCaseInsensitive = true;
jsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
jsonSerializerOptions.Converters.Add(new ObjectToInferredTypesConverter());
jsonSerializerOptions.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase));
}
/// <summary>
@ -73,6 +66,21 @@ namespace Refit
.Select(a => a.Name)
.FirstOrDefault();
}
/// <summary>
/// Creates new <see cref="JsonSerializerOptions"/> and fills it with default parameters
/// </summary>
public static JsonSerializerOptions GetDefaultJsonSerializerOptions()
{
var jsonSerializerOptions = new JsonSerializerOptions();
// Default to case insensitive property name matching as that's likely the behavior most users expect
jsonSerializerOptions.PropertyNameCaseInsensitive = true;
jsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
jsonSerializerOptions.Converters.Add(new ObjectToInferredTypesConverter());
jsonSerializerOptions.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase));
return jsonSerializerOptions;
}
}
// From https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-converters-how-to?pivots=dotnet-5-0#deserialize-inferred-types-to-object-properties