PosInformatique.Foundations.MediaTypes.Json 1.0.0

Prefix Reserved
There is a newer prerelease version of this package available.
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
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="PosInformatique.Foundations.MediaTypes.Json" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="PosInformatique.Foundations.MediaTypes.Json" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="PosInformatique.Foundations.MediaTypes.Json" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add PosInformatique.Foundations.MediaTypes.Json --version 1.0.0
                    
#r "nuget: PosInformatique.Foundations.MediaTypes.Json, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package PosInformatique.Foundations.MediaTypes.Json@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=PosInformatique.Foundations.MediaTypes.Json&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=PosInformatique.Foundations.MediaTypes.Json&version=1.0.0
                    
Install as a Cake Tool

PosInformatique.Foundations.MediaTypes.Json

NuGet version NuGet downloads

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 JsonException on invalid value).
  • Handles null values correctly when reading JSON.
  • Can be used via attributes ([JsonConverter]) or through a JsonSerializerOptions extension method.
  • Ensures consistency with the base MimeType value object.

Use cases

  • Serialization: Convert MimeType value objects into JSON strings.
  • Validation: Ensure only valid MIME type strings are accepted in JSON payloads.
  • Integration: Plug directly into System.Text.Json configuration.

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."
}
Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.