API Reference ResultJsonConverter - ulfbou/Zentient.Results GitHub Wiki

๐Ÿงฉ API Reference: sealed class โ€“ Zentient.Results.Serialization.ResultJsonConverter

namespace Zentient.Results.Serialization

๐Ÿ“– Summary

Provides a polymorphic System.Text.Json.Serialization.JsonConverterFactory that serializes and deserializes IResult, IResult<T>, Result, and Result<T> instances with full fidelity, preserving status, messages, errors, and encapsulated values.


๐Ÿ“Œ Metadata

Property Value
๐Ÿ“ฆ Assembly Zentient.Results.dll
๐Ÿ‘ Visibility public sealed class
โš™๏ธ Base Type System.Text.Json.Serialization.JsonConverterFactory
๐Ÿงฑ Implements Dynamic converter resolution for IResult and derived types
๐Ÿ—‚ Category JSON Serialization

๐ŸŽฏ Purpose

Enables System.Text.Json to:

  • Serialize and deserialize IResult and IResult<T> preserving complete structure.
  • Customize serialization details based on JsonSerializerOptions.
  • Decouple serialization from concrete implementations such as Result<T>.
  • Support transport-safe, protocol-agnostic JSON representations.

๐Ÿ”ง Usage

Registering the Converter

var options = new JsonSerializerOptions();
options.Converters.Add(new ResultJsonConverter());

string json = JsonSerializer.Serialize(Result<int>.Success(42, "OK"), options);

๐Ÿ“‚ Public API

๐Ÿ”น CanConvert(Type typeToConvert)

public override bool CanConvert(Type typeToConvert)

Determines if the converter supports the specified type.

Parameter Type Description
typeToConvert Type Type to verify for conversion
Returns Description
true If typeToConvert implements IResult

๐Ÿ”น CreateConverter(Type typeToConvert, JsonSerializerOptions options)

public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options)

Creates an appropriate JsonConverter instance for the specified result type.

Parameter Type Description
typeToConvert Type Target type to create converter for
options JsonSerializerOptions Serialization options
Returns Description
JsonConverter Converter instance or null if unsupported

Behavior:

  • Returns ResultGenericJsonConverter<T> for generic types (Result<T>, IResult<T>).
  • Returns ResultNonGenericJsonConverter for non-generic types (Result, IResult).
  • Returns null for unsupported types.

๐Ÿ” Internal Converters Overview

๐Ÿงฑ ResultGenericJsonConverter<T> : JsonConverter<Result<T>>

Supports full serialization/deserialization of Result<T> including fields:

  • value
  • isSuccess
  • isFailure
  • status
  • messages
  • errors
  • errorMessage (optional)

๐Ÿงฑ ResultNonGenericJsonConverter : JsonConverter<Result>

Handles Result serialization excluding value property but including all other common fields.


โš ๏ธ Important Notes

  • Avoid multiple registrations of ResultJsonConverter in different JsonSerializerOptions instances.
  • Honors camelCase naming via configured PropertyNamingPolicy.
  • Gracefully handles partial or unknown fields during deserialization.

๐Ÿงช Example JSON Output

{
  "value": 100,
  "isSuccess": true,
  "status": { "code": 0, "description": "Success" },
  "messages": ["Completed successfully"]
}

โœ… Best Practices

Scenario Recommendation
ASP.NET Core Serialization Add ResultJsonConverter to JsonOptions.Converters
Polymorphic Deserialization Use IResult as the contract/interface type
Custom Naming Policies Configure PropertyNamingPolicy = JsonNamingPolicy.CamelCase
โš ๏ธ **GitHub.com Fallback** โš ๏ธ