Skip to content

Commit

Permalink
Cleanup before the release
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeyzimarev committed Jul 15, 2021
1 parent ddcb137 commit 922bf07
Show file tree
Hide file tree
Showing 29 changed files with 134 additions and 254 deletions.
3 changes: 2 additions & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
<PublicSign Condition="'$(OS)' != 'Windows_NT'">true</PublicSign>
<UpdateVersionProperties>true</UpdateVersionProperties>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<LangVersion>8</LangVersion>
<LangVersion>9</LangVersion>
<Nullable>enable</Nullable>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>
Expand Down
31 changes: 14 additions & 17 deletions src/RestSharp.Serializers.NewtonsoftJson/JsonNetSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using System;
using System.IO;
using JetBrains.Annotations;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using RestSharp.Serialization;

namespace RestSharp.Serializers.NewtonsoftJson
{
public class JsonNetSerializer : IRestSerializer
{
namespace RestSharp.Serializers.NewtonsoftJson {
public class JsonNetSerializer : IRestSerializer {
/// <summary>
/// Default serialization settings:
/// - Camel-case contract resolver
Expand All @@ -16,8 +15,7 @@ public class JsonNetSerializer : IRestSerializer
/// - Non-indented formatting
/// - Allow using non-public constructors
/// </summary>
public static readonly JsonSerializerSettings DefaultSettings = new JsonSerializerSettings
{
public static readonly JsonSerializerSettings DefaultSettings = new() {
ContractResolver = new CamelCasePropertyNamesContractResolver(),
DefaultValueHandling = DefaultValueHandling.Include,
TypeNameHandling = TypeNameHandling.None,
Expand All @@ -26,7 +24,7 @@ public class JsonNetSerializer : IRestSerializer
ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor
};

[ThreadStatic] static WriterBuffer tWriterBuffer;
[ThreadStatic] static WriterBuffer? tWriterBuffer;

readonly JsonSerializer _serializer;

Expand All @@ -41,31 +39,30 @@ public class JsonNetSerializer : IRestSerializer
/// <param name="settings">Json.Net serializer settings</param>
public JsonNetSerializer(JsonSerializerSettings settings) => _serializer = JsonSerializer.Create(settings);

public string Serialize(object obj)
{
public string? Serialize(object? obj) {
if (obj == null) return null;

using var writerBuffer = tWriterBuffer ??= new WriterBuffer(_serializer);

_serializer.Serialize(writerBuffer.GetJsonTextWriter(), obj, obj.GetType());

return writerBuffer.GetStringWriter().ToString();
}

public string Serialize(Parameter bodyParameter) => Serialize(bodyParameter.Value);
public string? Serialize(Parameter bodyParameter) => Serialize(bodyParameter.Value);

public T Deserialize<T>(IRestResponse response)
{
using var reader = new JsonTextReader(new StringReader(response.Content)) {CloseInput = true};
public T? Deserialize<T>(IRestResponse response) {
using var reader = new JsonTextReader(new StringReader(response.Content)) { CloseInput = true };

return _serializer.Deserialize<T>(reader);
}

public string[] SupportedContentTypes { get; } =
{
public string[] SupportedContentTypes { get; } = {
"application/json", "text/json", "text/x-json", "text/javascript", "*+json"
};

public string ContentType { get; set; } = "application/json";

public DataFormat DataFormat { get; } = DataFormat.Json;
public DataFormat DataFormat => DataFormat.Json;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using JetBrains.Annotations;
using Newtonsoft.Json;

namespace RestSharp.Serializers.NewtonsoftJson
{
public static class RestClientExtensions
{
namespace RestSharp.Serializers.NewtonsoftJson {
[PublicAPI]
public static class RestClientExtensions {
/// <summary>
/// Use Newtonsoft.Json serializer with default settings
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
using JetBrains.Annotations;
using Newtonsoft.Json;

namespace RestSharp.Serializers.NewtonsoftJson
{
public static class RestRequestExtensions
{
namespace RestSharp.Serializers.NewtonsoftJson {
[PublicAPI]
public static class RestRequestExtensions {
/// <summary>
/// Use Newtonsoft.Json serializer for a single request
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public static IRestRequest UseNewtonsoftJson(this IRestRequest request)
{
public static IRestRequest UseNewtonsoftJson(this IRestRequest request) {
request.JsonSerializer = new JsonNetSerializer();
return request;
}

/// <summary>
/// Use Newtonsoft.Json serializer for a single request, with custom settings
/// </summary>
/// <param name="request"></param>
/// <param name="settings">Newtonsoft.Json serializer settings</param>
/// <returns></returns>
public static IRestRequest UseNewtonsoftJson(this IRestRequest request, JsonSerializerSettings settings)
{
public static IRestRequest UseNewtonsoftJson(this IRestRequest request, JsonSerializerSettings settings) {
request.JsonSerializer = new JsonNetSerializer(settings);
return request;
}
Expand Down
20 changes: 9 additions & 11 deletions src/RestSharp.Serializers.NewtonsoftJson/WriterBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,23 @@
using System.Text;
using Newtonsoft.Json;

namespace RestSharp.Serializers.NewtonsoftJson
{
public sealed class WriterBuffer : IDisposable
{
private readonly StringWriter _stringWriter;
private readonly JsonTextWriter _jsonTextWriter;
namespace RestSharp.Serializers.NewtonsoftJson {
sealed class WriterBuffer : IDisposable {
readonly StringWriter _stringWriter;
readonly JsonTextWriter _jsonTextWriter;

public WriterBuffer(JsonSerializer jsonSerializer)
{
public WriterBuffer(JsonSerializer jsonSerializer) {
_stringWriter = new StringWriter(new StringBuilder(256), CultureInfo.InvariantCulture);

_jsonTextWriter = new JsonTextWriter(_stringWriter)
{
_jsonTextWriter = new JsonTextWriter(_stringWriter) {
Formatting = jsonSerializer.Formatting, CloseOutput = false
};
}

public JsonTextWriter GetJsonTextWriter() => _jsonTextWriter;

public StringWriter GetStringWriter() => _stringWriter;

public void Dispose() => _stringWriter.GetStringBuilder().Clear();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System.Text.Json;
using JetBrains.Annotations;

namespace RestSharp.Serializers.SystemTextJson
{
public static class RestClientExtensions
{
namespace RestSharp.Serializers.SystemTextJson {
[PublicAPI]
public static class RestClientExtensions {
/// <summary>
/// Use System.Text.Json serializer with default settings
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
using System.Text.Json;
using JetBrains.Annotations;

namespace RestSharp.Serializers.SystemTextJson
{
public static class RestRequestExtensions
{
namespace RestSharp.Serializers.SystemTextJson {
[PublicAPI]
public static class RestRequestExtensions {
/// <summary>
/// Use System.Text.Json serializer for a single request
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public static IRestRequest UseSystemTextJson(this IRestRequest request)
{
public static IRestRequest UseSystemTextJson(this IRestRequest request) {
request.JsonSerializer = new SystemTextJsonSerializer();
return request;
}

/// <summary>
/// Use System.Text.Json serializer for a single request with custom options
/// </summary>
/// <param name="request"></param>
/// <param name="options">System.Text.Json serializer options</param>
/// <returns></returns>
public static IRestRequest UseSystemTextJson(this IRestRequest request, JsonSerializerOptions options)
{
public static IRestRequest UseSystemTextJson(this IRestRequest request, JsonSerializerOptions options) {
request.JsonSerializer = new SystemTextJsonSerializer(options);
return request;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Text.Json" Version="5.0.*"/>
<PackageReference Include="System.Text.Json" Version="5.0.*" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\RestSharp\RestSharp.csproj"/>
<ProjectReference Include="..\RestSharp\RestSharp.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Json;
using JetBrains.Annotations;
using RestSharp.Serialization;

namespace RestSharp.Serializers.SystemTextJson
Expand All @@ -18,11 +19,11 @@ public class SystemTextJsonSerializer : IRestSerializer
/// <param name="options">Json serializer settings</param>
public SystemTextJsonSerializer(JsonSerializerOptions options) => _options = options;

public string Serialize(object obj) => JsonSerializer.Serialize(obj, _options);
public string? Serialize(object? obj) => obj == null ? null : JsonSerializer.Serialize(obj, _options);

public string Serialize(Parameter bodyParameter) => Serialize(bodyParameter.Value);
public string? Serialize(Parameter bodyParameter) => Serialize(bodyParameter.Value);

public T Deserialize<T>(IRestResponse response) => JsonSerializer.Deserialize<T>(response.Content, _options);
public T? Deserialize<T>(IRestResponse response) => JsonSerializer.Deserialize<T>(response.Content, _options);

public string[] SupportedContentTypes { get; } =
{
Expand All @@ -31,6 +32,6 @@ public class SystemTextJsonSerializer : IRestSerializer

public string ContentType { get; set; } = "application/json";

public DataFormat DataFormat { get; } = DataFormat.Json;
public DataFormat DataFormat => DataFormat.Json;
}
}
9 changes: 4 additions & 5 deletions src/RestSharp.Serializers.Utf8Json/RestClientExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@

using JetBrains.Annotations;
using Utf8Json;

namespace RestSharp.Serializers.Utf8Json
{
public static class RestClientExtensions
{
namespace RestSharp.Serializers.Utf8Json {
[PublicAPI]
public static class RestClientExtensions {
/// <summary>
/// Use Utf8Json serializer with default formatter resolver
/// </summary>
Expand Down
16 changes: 7 additions & 9 deletions src/RestSharp.Serializers.Utf8Json/RestRequestExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
using JetBrains.Annotations;
using Utf8Json;

namespace RestSharp.Serializers.Utf8Json
{
public static class RestRequestExtensions
{
namespace RestSharp.Serializers.Utf8Json {
[PublicAPI]
public static class RestRequestExtensions {
/// <summary>
/// Use Utf8Json serializer for a single request
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public static IRestRequest UseUtf8Json(this IRestRequest request)
{
public static IRestRequest UseUtf8Json(this IRestRequest request) {
request.JsonSerializer = new Utf8JsonSerializer();
return request;
}

/// <summary>
/// Use Utf8Json serializer for a single request
/// </summary>
/// <param name="request"></param>
/// <param name="resolver">JSON formatter resolver instance to provide custom options to Utf8Json</param>
/// <returns></returns>
public static IRestRequest UseUtf8Json(this IRestRequest request, IJsonFormatterResolver resolver)
{
public static IRestRequest UseUtf8Json(this IRestRequest request, IJsonFormatterResolver resolver) {
request.JsonSerializer = new Utf8JsonSerializer(resolver);
return request;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<TargetFrameworks>netstandard2.0;net452</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Utf8Json" Version="1.3.7"/>
<PackageReference Include="Utf8Json" Version="1.3.7" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\RestSharp\RestSharp.csproj"/>
<ProjectReference Include="..\RestSharp\RestSharp.csproj" />
</ItemGroup>
</Project>
19 changes: 8 additions & 11 deletions src/RestSharp.Serializers.Utf8Json/Utf8JsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,24 @@
using Utf8Json;
using Utf8Json.Resolvers;

namespace RestSharp.Serializers.Utf8Json
{
public class Utf8JsonSerializer : IRestSerializer
{
public Utf8JsonSerializer(IJsonFormatterResolver resolver = null) => Resolver = resolver ?? StandardResolver.AllowPrivateExcludeNullCamelCase;
namespace RestSharp.Serializers.Utf8Json {
public class Utf8JsonSerializer : IRestSerializer {
public Utf8JsonSerializer(IJsonFormatterResolver? resolver = null) => Resolver = resolver ?? StandardResolver.AllowPrivateExcludeNullCamelCase;

IJsonFormatterResolver Resolver { get; }

public string Serialize(object obj) => JsonSerializer.NonGeneric.ToJsonString(obj, Resolver);
public string? Serialize(object? obj) => obj == null ? null : JsonSerializer.NonGeneric.ToJsonString(obj, Resolver);

public string Serialize(Parameter parameter) => Serialize(parameter.Value);
public string? Serialize(Parameter parameter) => Serialize(parameter.Value);

public T Deserialize<T>(IRestResponse response) => JsonSerializer.Deserialize<T>(response.RawBytes, Resolver);
public T? Deserialize<T>(IRestResponse response) => JsonSerializer.Deserialize<T?>(response.RawBytes, Resolver);

public string[] SupportedContentTypes { get; } =
{
public string[] SupportedContentTypes { get; } = {
"application/json", "text/json", "text/x-json", "text/javascript", "*+json"
};

public string ContentType { get; set; } = "application/json";

public DataFormat DataFormat { get; } = DataFormat.Json;
public DataFormat DataFormat => DataFormat.Json;
}
}
4 changes: 2 additions & 2 deletions src/RestSharp/Authenticators/OAuth/OAuth1Authenticator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ void AddOAuthData(IRestClient client, IRestRequest request, OAuthWorkflow workfl
// if this change causes trouble we need to introduce a flag indicating the specific OAuth implementation level,
// or implement a separate class for each OAuth version
static bool BaseQuery(Parameter x)
=> x.Type == ParameterType.GetOrPost || x.Type == ParameterType.QueryString || x.Type == ParameterType.QueryStringWithoutEncode;
=> x.Type is ParameterType.GetOrPost or ParameterType.QueryString or ParameterType.QueryStringWithoutEncode;

var query =
request.AlwaysMultipartFormData || request.Files.Count > 0
Expand Down Expand Up @@ -334,6 +334,6 @@ string GetAuthorizationHeader()

static class ParametersExtensions
{
internal static IEnumerable<WebPair> ToWebParameters(this IEnumerable<Parameter> p) => p.Select(x => new WebPair(x.Name, x.Value.ToString(), false));
internal static IEnumerable<WebPair> ToWebParameters(this IEnumerable<Parameter> p) => p.Select(x => new WebPair(x.Name, x.Value.ToString()));
}
}
2 changes: 1 addition & 1 deletion src/RestSharp/Authenticators/OAuth/WebPair.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public WebPair(string name, string value, bool encode = false)
public string WebValue { get; }
public bool Encode { get; }

internal static WebPairComparer Comparer { get; } = new WebPairComparer();
internal static WebPairComparer Comparer { get; } = new();

internal class WebPairComparer : IComparer<WebPair>
{
Expand Down
Loading

0 comments on commit 922bf07

Please sign in to comment.