Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions Activout.RestClient.Newtonsoft.Json/JsonHelper.cs

This file was deleted.

20 changes: 20 additions & 0 deletions Activout.RestClient.Newtonsoft.Json/NewtonsoftJsonDefaults.cs
Original file line number Diff line number Diff line change
@@ -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()
};
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,7 +8,7 @@ public class NewtonsoftJsonDeserializer : IDeserializer
{
private readonly JsonSerializerSettings _jsonSerializerSettings;

public IReadOnlyCollection<MediaType> SupportedMediaTypes => JsonHelper.SupportedMediaTypes;
public IReadOnlyCollection<MediaType> SupportedMediaTypes => NewtonsoftJsonDefaults.SupportedMediaTypes;

public NewtonsoftJsonDeserializer(JsonSerializerSettings jsonSerializerSettings)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -11,7 +8,7 @@ public class NewtonsoftJsonSerializer : ISerializer
{
private readonly JsonSerializerSettings _jsonSerializerSettings;

public IReadOnlyCollection<MediaType> SupportedMediaTypes => JsonHelper.SupportedMediaTypes;
public IReadOnlyCollection<MediaType> SupportedMediaTypes => NewtonsoftJsonDefaults.SupportedMediaTypes;

public NewtonsoftJsonSerializer(JsonSerializerSettings jsonSerializerSettings)
{
Expand Down
19 changes: 17 additions & 2 deletions Activout.RestClient.Newtonsoft.Json/README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
# Activout.RestClient.Xml
# Activout.RestClient.Newtonsoft.Json

This project is an extension to [Activout.RestClient](https://www.nuget.org/packages/Activout.RestClient/).

It provides support for JSON serialization and deserialization via Newtonsoft.Json.

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<IMovieReviewService>();

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsonConverter> DefaultJsonConverters = new List<JsonConverter>
{ 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;
}
}