There are two JSON extensions for Solid.Http.Core. One extension, Solid.Http.Json uses System.Text.Json for serialization/deserialization. The other extension, Solid.Http.NewtonsoftJson, uses Newtonsoft.Json for serialization/deserialization.
Solid.Http.Json uses System.Text.Json internally.
> dotnet add package Solid.Http.Json
If you're using Solid.Http.Core directly, you can add Solid.Http.Json using the builder.
public void ConfigureServices(IServiceCollection services)
{
services.AddSolidHttpCore(builder =>
{
builder.AddJson(options =>
{
options.SerializerOptions = new JsonSerializerOptions
{
// configure your JsonSerializerOptions here
};
});
});
}
If you're using Solid.Http, then the AddJson() method on the builder is called internally. You can configure the JsonSerializerOptions in one of two ways. You can call AddJson() on the builder with no adverse consequences, or you can call ConfigureSolidHttpJson(o => {}) on the IServiceCollection directly.
Included in the package is an extension method that makes it easier to add JSON content to an HTTP request.
public async Task<Post> CreateAsync(Post post)
{
var options = new JsonSerializerOptions
{
// configure your options here.
};
await _client
.PostAsync("posts")
// contentType and options are optional
// If options are omitted, the options that are specified in the SolidHttpJsonOptions are used.
.WithJsonContent(post, contentType: "application/json", options: options)
;
return post;
}
The As methods that are included with Solid.Http.Core will use the configured JsonSerializerOptions to deserialize the received response body.
public async Task<Post> GetAsync(int id)
{
return await _client
.GetAsync("posts/{id}")
.WithNamedParameter("id", id)
.ExpectSuccess()
.As<Post>()
;
}
You can also specify JsonSerializerOptions if a specific API endpoint differs from the default in how it serializes it's entities.
public async Task<Post> GetAsync(int id)
{
var options = new JsonSerializerOptions
{
// configure your options here.
};
return await _client
.GetAsync("posts/{id}")
.WithNamedParameter("id", id)
.ExpectSuccess()
.As<Post>(options)
;
}
Solid.Http.NewtonsoftJson uses Newtonsoft.Json internally.
> dotnet add package Solid.Http.NewtonsoftJson
You can add Solid.Http.NewtonsoftJson using the builder.
public void ConfigureServices(IServiceCollection services)
{
// AddSolidHttpCore could be replaced with AddSolidHttp here
services.AddSolidHttpCore(builder =>
{
builder.AddNewtonsoftJson(options =>
{
options.SerializerSettings = new JsonSerializerSettings
{
// configure your JsonSerializerSettings here
};
});
});
}
If Solid.Http.Json has beed previously added, Solid.Http.NewtonsoftJson will replace it for default deserialization.
Included in the package is an extension method that makes it easier to add JSON content to an HTTP request.
public async Task<Post> CreateAsync(Post post)
{
var settings = new JsonSerializerSettings
{
// configure your settings here.
};
await _client
.PostAsync("posts")
// contentType and settings are optional
// If settings are omitted, the settings that are specified in the SolidHttpNewtonsoftJsonOptions are used.
.WithNewtonsoftJsonContent(post, contentType: "application/json", settings: settings)
;
return post;
}
The As methods that are included with Solid.Http.Core will use the configured JsonSerializerSettings to deserialize the received response body.
public async Task<Post> GetAsync(int id)
{
return await _client
.GetAsync("posts/{id}")
.WithNamedParameter("id", id)
.ExpectSuccess()
.As<Post>()
;
}
You can also specify JsonSerializerSettings if a specific API endpoint differs from the default in how it serializes it's entities.
public async Task<Post> GetAsync(int id)
{
var settings = new JsonSerializerSettings
{
// configure your settings here.
};
return await _client
.GetAsync("posts/{id}")
.WithNamedParameter("id", id)
.ExpectSuccess()
.As<Post>(settings)
;
}