From ee628ed2817ac343b4f994eec187bbcb297d1d41 Mon Sep 17 00:00:00 2001 From: David Eriksson Date: Thu, 29 May 2025 22:43:57 +0200 Subject: [PATCH 1/2] Start of Newtonsoft.Json clean-up --- .../JsonHelper.cs | 17 ----------- .../NewtonsoftJsonDefaults.cs | 20 +++++++++++++ .../NewtonsoftJsonDeserializer.cs | 7 +---- .../NewtonsoftJsonSerializer.cs | 5 +--- ...stClientBuilderNewtonsoftJsonExtensions.cs | 28 ++++++------------- 5 files changed, 31 insertions(+), 46 deletions(-) delete mode 100644 Activout.RestClient.Newtonsoft.Json/JsonHelper.cs create mode 100644 Activout.RestClient.Newtonsoft.Json/NewtonsoftJsonDefaults.cs diff --git a/Activout.RestClient.Newtonsoft.Json/JsonHelper.cs b/Activout.RestClient.Newtonsoft.Json/JsonHelper.cs deleted file mode 100644 index c902b4d..0000000 --- a/Activout.RestClient.Newtonsoft.Json/JsonHelper.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System.Collections.Generic; - -namespace Activout.RestClient.Newtonsoft.Json -{ - public static class JsonHelper - { - static JsonHelper() - { - SupportedMediaTypes = new[] - { - MediaType.ValueOf("application/json") - }; - } - - public static IReadOnlyCollection SupportedMediaTypes { get; } - } -} diff --git a/Activout.RestClient.Newtonsoft.Json/NewtonsoftJsonDefaults.cs b/Activout.RestClient.Newtonsoft.Json/NewtonsoftJsonDefaults.cs new file mode 100644 index 0000000..9fe62b8 --- /dev/null +++ b/Activout.RestClient.Newtonsoft.Json/NewtonsoftJsonDefaults.cs @@ -0,0 +1,20 @@ +using System.Diagnostics.CodeAnalysis; +using Newtonsoft.Json; + +namespace Activout.RestClient.Newtonsoft.Json; + +[SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] +public static class NewtonsoftJsonDefaults +{ + public static readonly MediaType[] SupportedMediaTypes = + [ + MediaType.ValueOf("application/json") + ]; + + public static readonly JsonConverter[] DefaultJsonConverters = [new SimpleValueObjectConverter()]; + + public static readonly JsonSerializerSettings DefaultJsonSerializerSettings = new() + { + Converters = DefaultJsonConverters.ToList() + }; +} \ No newline at end of file diff --git a/Activout.RestClient.Newtonsoft.Json/NewtonsoftJsonDeserializer.cs b/Activout.RestClient.Newtonsoft.Json/NewtonsoftJsonDeserializer.cs index 4ae0bd4..b8c7e5c 100644 --- a/Activout.RestClient.Newtonsoft.Json/NewtonsoftJsonDeserializer.cs +++ b/Activout.RestClient.Newtonsoft.Json/NewtonsoftJsonDeserializer.cs @@ -1,8 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net.Http; -using System.Threading.Tasks; using Activout.RestClient.Serialization; using Newtonsoft.Json; using Newtonsoft.Json.Linq; @@ -13,7 +8,7 @@ public class NewtonsoftJsonDeserializer : IDeserializer { private readonly JsonSerializerSettings _jsonSerializerSettings; - public IReadOnlyCollection SupportedMediaTypes => JsonHelper.SupportedMediaTypes; + public IReadOnlyCollection SupportedMediaTypes => NewtonsoftJsonDefaults.SupportedMediaTypes; public NewtonsoftJsonDeserializer(JsonSerializerSettings jsonSerializerSettings) { diff --git a/Activout.RestClient.Newtonsoft.Json/NewtonsoftJsonSerializer.cs b/Activout.RestClient.Newtonsoft.Json/NewtonsoftJsonSerializer.cs index 0ba6afd..394a911 100644 --- a/Activout.RestClient.Newtonsoft.Json/NewtonsoftJsonSerializer.cs +++ b/Activout.RestClient.Newtonsoft.Json/NewtonsoftJsonSerializer.cs @@ -1,6 +1,3 @@ -using System.Collections.Generic; -using System.Linq; -using System.Net.Http; using System.Text; using Activout.RestClient.Serialization; using Newtonsoft.Json; @@ -11,7 +8,7 @@ public class NewtonsoftJsonSerializer : ISerializer { private readonly JsonSerializerSettings _jsonSerializerSettings; - public IReadOnlyCollection SupportedMediaTypes => JsonHelper.SupportedMediaTypes; + public IReadOnlyCollection SupportedMediaTypes => NewtonsoftJsonDefaults.SupportedMediaTypes; public NewtonsoftJsonSerializer(JsonSerializerSettings jsonSerializerSettings) { diff --git a/Activout.RestClient.Newtonsoft.Json/RestClientBuilderNewtonsoftJsonExtensions.cs b/Activout.RestClient.Newtonsoft.Json/RestClientBuilderNewtonsoftJsonExtensions.cs index 7e99c35..dc8dc5d 100644 --- a/Activout.RestClient.Newtonsoft.Json/RestClientBuilderNewtonsoftJsonExtensions.cs +++ b/Activout.RestClient.Newtonsoft.Json/RestClientBuilderNewtonsoftJsonExtensions.cs @@ -2,28 +2,18 @@ using System.Collections.Immutable; using System.Diagnostics.CodeAnalysis; -namespace Activout.RestClient.Newtonsoft.Json +namespace Activout.RestClient.Newtonsoft.Json; + +public static class RestClientBuilderNewtonsoftJsonExtensions { - [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] - public static class RestClientBuilderNewtonsoftJsonExtensions + public static IRestClientBuilder WithNewtonsoftJson(this IRestClientBuilder builder, + JsonSerializerSettings? jsonSerializerSettings = null) { - public static readonly IReadOnlyCollection DefaultJsonConverters = new List - { new SimpleValueObjectConverter() }.ToImmutableList(); - - public static readonly JsonSerializerSettings DefaultJsonSerializerSettings = new() - { - Converters = DefaultJsonConverters.ToList() - }; - - public static IRestClientBuilder WithNewtonsoftJson(this IRestClientBuilder builder, - JsonSerializerSettings? jsonSerializerSettings = null) - { - var settings = jsonSerializerSettings ?? DefaultJsonSerializerSettings; + var settings = jsonSerializerSettings ?? NewtonsoftJsonDefaults.DefaultJsonSerializerSettings; - builder.With(new NewtonsoftJsonSerializer(settings)); - builder.With(new NewtonsoftJsonDeserializer(settings)); + builder.With(new NewtonsoftJsonSerializer(settings)); + builder.With(new NewtonsoftJsonDeserializer(settings)); - return builder; - } + return builder; } } \ No newline at end of file From ae6f4759f936eca1341765bf309b34e9711728df Mon Sep 17 00:00:00 2001 From: David Eriksson Date: Thu, 29 May 2025 22:55:03 +0200 Subject: [PATCH 2/2] README fixes --- Activout.RestClient.Newtonsoft.Json/README.md | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/Activout.RestClient.Newtonsoft.Json/README.md b/Activout.RestClient.Newtonsoft.Json/README.md index acc10a2..650ad11 100644 --- a/Activout.RestClient.Newtonsoft.Json/README.md +++ b/Activout.RestClient.Newtonsoft.Json/README.md @@ -1,4 +1,4 @@ -# Activout.RestClient.Xml +# Activout.RestClient.Newtonsoft.Json This project is an extension to [Activout.RestClient](https://www.nuget.org/packages/Activout.RestClient/). @@ -6,9 +6,24 @@ It provides support for JSON serialization and deserialization via Newtonsoft.Js See the [Activout.RestClient README](https://github.com/twogood/Activout.RestClient/tree/main) for more information. +## Example usage + +```C# +var restClientFactory = Services.CreateRestClientFactory(); +var movieReviewService = restClientFactory + .CreateBuilder() + .With(_httpClient) + .WithNewtonsoftJson() // Use this package to enable Newtonsoft.Json serialization + .BaseUri(new Uri("https://example.com/movieReviewService")) + .Build(); + +var review = new Review(stars: 3, "This was a delightful comedy, but not terribly realistic."); +await movieReviewService.SubmitReview(movieId, review); +``` + ## Need help implementing Activout.RestClient? -Contact [david@activout.se](mailto:david@activout.se) to order a support package, starting at 20000 SEK or 1800 EUR or 2000 USD. +Contact [david@activout.se](mailto:david@activout.se) to order a support package. ## License