PosInformatique.Foundations.MediaTypes.Json
1.0.0
Prefix Reserved
See the version list below for details.
dotnet add package PosInformatique.Foundations.MediaTypes.Json --version 1.0.0
NuGet\Install-Package PosInformatique.Foundations.MediaTypes.Json -Version 1.0.0
<PackageReference Include="PosInformatique.Foundations.MediaTypes.Json" Version="1.0.0" />
<PackageVersion Include="PosInformatique.Foundations.MediaTypes.Json" Version="1.0.0" />
<PackageReference Include="PosInformatique.Foundations.MediaTypes.Json" />
paket add PosInformatique.Foundations.MediaTypes.Json --version 1.0.0
#r "nuget: PosInformatique.Foundations.MediaTypes.Json, 1.0.0"
#:package PosInformatique.Foundations.MediaTypes.Json@1.0.0
#addin nuget:?package=PosInformatique.Foundations.MediaTypes.Json&version=1.0.0
#tool nuget:?package=PosInformatique.Foundations.MediaTypes.Json&version=1.0.0
PosInformatique.Foundations.MediaTypes.Json
Introduction
Provides a System.Text.Json converter for the MimeType value object from
PosInformatique.Foundations.MediaTypes.
Enables seamless serialization and deserialization of MIME types (e.g. application/json, image/png) within JSON documents.
Install
You can install the package from NuGet:
dotnet add package PosInformatique.Foundations.MediaTypes.Json
This package depends on the base package PosInformatique.Foundations.MediaTypes.
Features
- Provides a
JsonConverter<MimeType>for serialization and deserialization. - Validates MIME type strings when deserializing (throws
JsonExceptionon invalid value). - Handles
nullvalues correctly when reading JSON. - Can be used via attributes (
[JsonConverter]) or through aJsonSerializerOptionsextension method. - Ensures consistency with the base
MimeTypevalue object.
Use cases
- Serialization: Convert
MimeTypevalue objects into JSON strings. - Validation: Ensure only valid MIME type strings are accepted in JSON payloads.
- Integration: Plug directly into
System.Text.Jsonconfiguration.
Examples
Example 1: DTO with [JsonConverter] attribute
using System.Text.Json;
using System.Text.Json.Serialization;
using PosInformatique.Foundations.MediaTypes;
using PosInformatique.Foundations.MediaTypes.Json;
public class MediaResourceDto
{
[JsonConverter(typeof(MimeTypeJsonConverter))]
public MimeType? ContentType { get; set; }
}
// Serialization
var dto = new MediaResourceDto { ContentType = MimeType.Parse("application/json") };
var json = JsonSerializer.Serialize(dto);
// Result: {"ContentType":"application/json"}
// Deserialization
var input = "{ \"ContentType\": \"image/png\" }";
var deserialized = JsonSerializer.Deserialize<MediaResourceDto>(input);
Console.WriteLine(deserialized!.ContentType); // "image/png"
Example 2: Use AddMediaTypesConverters() without attributes
The library provides an extension method AddMediaTypesConverters() on JsonSerializerOptions to register the MimeTypeJsonConverter globally.
using System.Text.Json;
using PosInformatique.Foundations.MediaTypes;
using PosInformatique.Foundations.MediaTypes.Json;
public class FileMetadataDto
{
public MimeType? ContentType { get; set; }
}
var options = new JsonSerializerOptions()
.AddMediaTypesConverters(); // Registers MimeTypeJsonConverter
// Serialization
var dto = new FileMetadataDto
{
ContentType = MimeType.Parse("application/pdf")
};
var json = JsonSerializer.Serialize(dto, options);
// Result: {"ContentType":"application/pdf"}
// Deserialization
var input = "{ \"ContentType\": \"text/plain\" }";
var deserialized = JsonSerializer.Deserialize<FileMetadataDto>(input, options);
Console.WriteLine(deserialized!.ContentType); // "text/plain"
Example 3: Null and invalid values
using System.Text.Json;
using PosInformatique.Foundations.MediaTypes;
public class DocumentDto
{
public MimeType? ContentType { get; set; }
}
var options = new JsonSerializerOptions().AddMediaTypesConverters();
// Null value
var jsonWithNull = "{ \"ContentType\": null }";
var docWithNull = JsonSerializer.Deserialize<DocumentDto>(jsonWithNull, options);
// docWithNull.ContentType is null
// Invalid MIME type -> throws JsonException
var invalidJson = "{ \"ContentType\": \"not a mime\" }";
try
{
var invalidDoc = JsonSerializer.Deserialize<DocumentDto>(invalidJson, options);
}
catch (JsonException ex)
{
Console.WriteLine(ex.Message); // "'not a mime' is not a valid MIME type."
}
Links
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- PosInformatique.Foundations.MediaTypes (>= 1.0.0)
-
net9.0
- PosInformatique.Foundations.MediaTypes (>= 1.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.1.0-rc.2 | 49 | 1/26/2026 |
| 1.1.0-rc.1 | 54 | 1/23/2026 |
| 1.0.0 | 428 | 11/19/2025 |
| 1.0.0-rc.4 | 363 | 11/19/2025 |
| 1.0.0-rc.3 | 370 | 11/18/2025 |
| 1.0.0-rc.2 | 375 | 11/18/2025 |
| 1.0.0-rc.1 | 371 | 11/18/2025 |
1.0.0
- Initial release with the support JSON serialization (with System.Text.Json) for MimeType value object.