ktsu.ToStringJsonConverter 1.2.3-pre.1

Prefix Reserved
This is a prerelease version of ktsu.ToStringJsonConverter.
There is a newer version of this package available.
See the version list below for details.
dotnet add package ktsu.ToStringJsonConverter --version 1.2.3-pre.1
                    
NuGet\Install-Package ktsu.ToStringJsonConverter -Version 1.2.3-pre.1
                    
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="ktsu.ToStringJsonConverter" Version="1.2.3-pre.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ktsu.ToStringJsonConverter" Version="1.2.3-pre.1" />
                    
Directory.Packages.props
<PackageReference Include="ktsu.ToStringJsonConverter" />
                    
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 ktsu.ToStringJsonConverter --version 1.2.3-pre.1
                    
#r "nuget: ktsu.ToStringJsonConverter, 1.2.3-pre.1"
                    
#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 ktsu.ToStringJsonConverter@1.2.3-pre.1
                    
#: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=ktsu.ToStringJsonConverter&version=1.2.3-pre.1&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=ktsu.ToStringJsonConverter&version=1.2.3-pre.1&prerelease
                    
Install as a Cake Tool

ktsu.ToStringJsonConverter

A JSON converter factory that serializes objects using their ToString and FromString methods.

License NuGet NuGet Downloads Build Status GitHub Stars

Introduction

ToStringJsonConverter is a JSON converter factory for System.Text.Json that simplifies serialization and deserialization of custom types by leveraging their ToString and FromString methods. This approach is particularly useful for value types, strong types, and any other types where a string representation makes logical sense.

Features

  • Automatic Type Detection: Automatically identifies types with compatible FromString methods.
  • String-Based Serialization: Converts objects to and from JSON using their string representation.
  • Property Name Support: Works with both JSON values and property names.
  • Reflection Optimization: Uses cached reflection for improved performance.
  • Generic Method Support: Handles both generic and non-generic FromString methods.

Installation

Package Manager Console

Install-Package ktsu.ToStringJsonConverter

.NET CLI

dotnet add package ktsu.ToStringJsonConverter

Package Reference

<PackageReference Include="ktsu.ToStringJsonConverter" Version="x.y.z" />

Usage Examples

Basic Example

using System.Text.Json;
using ktsu.ToStringJsonConverter;

// Configure the converter in your JsonSerializerOptions
var options = new JsonSerializerOptions();
options.Converters.Add(new ToStringJsonConverterFactory());

// Example custom type with ToString and FromString
public class CustomId
{
    public string Value { get; set; }
    
    public static CustomId FromString(string value) => new() { Value = value };
    
    public override string ToString() => Value;
}

// Serialization
var id = new CustomId { Value = "12345" };
string json = JsonSerializer.Serialize(id, options);
// json is now: "12345"

// Deserialization
CustomId deserialized = JsonSerializer.Deserialize<CustomId>(json, options);
// deserialized.Value is now: "12345"

Integration with Other Converters

using System.Text.Json;
using System.Text.Json.Serialization;
using ktsu.ToStringJsonConverter;

var options = new JsonSerializerOptions
{
    WriteIndented = true,
    PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
    Converters =
    {
        new ToStringJsonConverterFactory(),
        new JsonStringEnumConverter()
    }
};

// Now both enum values and custom types with FromString will be handled appropriately

Advanced Usage

Working with Collections of Custom Types

using System.Text.Json;
using ktsu.ToStringJsonConverter;

// Setup serializer options with the converter
var options = new JsonSerializerOptions();
options.Converters.Add(new ToStringJsonConverterFactory());

// A collection of custom types
List<CustomId> ids = new()
{
    new CustomId { Value = "A001" },
    new CustomId { Value = "B002" },
    new CustomId { Value = "C003" }
};

// Serialize the collection
string json = JsonSerializer.Serialize(ids, options);
// json is now: ["A001","B002","C003"]

// Deserialize back to a collection
List<CustomId> deserializedIds = JsonSerializer.Deserialize<List<CustomId>>(json, options);

Using with Dictionaries as Keys

// Custom type can be used as dictionary keys
var dictionary = new Dictionary<CustomId, string>
{
    { new CustomId { Value = "key1" }, "value1" },
    { new CustomId { Value = "key2" }, "value2" }
};

string json = JsonSerializer.Serialize(dictionary, options);
// Serializes as a dictionary with string keys

var deserialized = JsonSerializer.Deserialize<Dictionary<CustomId, string>>(json, options);
// Keys are properly deserialized back to CustomId objects

API Reference

ToStringJsonConverterFactory

The primary class for integrating with System.Text.Json serialization.

Methods
Name Return Type Description
CanConvert(Type typeToConvert) bool Determines if a type can be converted by checking for a static FromString method
CreateConverter(Type typeToConvert, JsonSerializerOptions options) JsonConverter Creates a type-specific converter instance

Compatibility Requirements

For a type to work with ToStringJsonConverter, it must meet these requirements:

  1. Have a public static FromString(string) method that returns an instance of the type
  2. Override ToString() to provide a string representation that can be reversed by FromString

Contributing

Contributions are welcome! Here's how you can help:

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Please make sure to update tests as appropriate.

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

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 (3)

Showing the top 3 NuGet packages that depend on ktsu.ToStringJsonConverter:

Package Downloads
ktsu.AppDataStorage

Application data management library using JSON serialization to save and load data in the user's app data folder.

ktsu.Schema

Schema

ktsu.PkmnDB

PkmnDB

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.4 270 5/21/2025
1.2.4-pre.17 115 5/20/2025
1.2.4-pre.15 77 5/17/2025
1.2.4-pre.14 139 5/16/2025
1.2.4-pre.13 206 5/15/2025
1.2.4-pre.12 211 5/14/2025
1.2.4-pre.11 203 5/13/2025
1.2.4-pre.10 240 5/12/2025
1.2.4-pre.9 167 5/11/2025
1.2.4-pre.8 110 5/10/2025
1.2.4-pre.7 54 5/9/2025
1.2.4-pre.6 130 5/8/2025
1.2.4-pre.5 125 5/7/2025
1.2.4-pre.4 128 5/6/2025
1.2.4-pre.3 119 5/5/2025
1.2.4-pre.2 130 5/4/2025
1.2.4-pre.1 122 5/4/2025
1.2.3 2,845 5/4/2025
1.2.3-pre.1 54 4/26/2025
1.2.2 588 4/25/2025
1.2.2-pre.1 129 4/4/2025
1.2.1 701 3/30/2025
1.2.0 386 3/30/2025
1.1.1-pre.3 84 3/29/2025
1.1.1-pre.2 465 3/25/2025
1.1.1-pre.1 79 2/18/2025
1.1.0 960 2/17/2025
1.0.49-pre.3 88 2/6/2025
1.0.49-pre.2 72 2/5/2025
1.0.49-pre.1 72 2/5/2025
1.0.48 2,488 12/27/2024
1.0.48-pre.30 75 2/4/2025
1.0.48-pre.29 71 2/3/2025
1.0.48-pre.28 80 2/1/2025
1.0.48-pre.27 71 1/30/2025
1.0.48-pre.26 71 1/28/2025
1.0.48-pre.25 67 1/26/2025
1.0.48-pre.24 69 1/24/2025
1.0.48-pre.23 74 1/22/2025
1.0.48-pre.22 68 1/20/2025
1.0.48-pre.21 64 1/18/2025
1.0.48-pre.20 60 1/16/2025
1.0.48-pre.19 54 1/14/2025
1.0.48-pre.18 62 1/13/2025
1.0.48-pre.17 69 1/11/2025
1.0.48-pre.16 65 1/10/2025
1.0.48-pre.15 67 1/10/2025
1.0.48-pre.14 57 1/8/2025
1.0.48-pre.13 75 1/7/2025
1.0.48-pre.12 76 1/5/2025
1.0.48-pre.11 96 1/3/2025
1.0.48-pre.10 72 1/3/2025
1.0.48-pre.9 76 1/3/2025
1.0.48-pre.8 78 1/3/2025
1.0.48-pre.7 70 1/2/2025
1.0.48-pre.6 97 12/31/2024
1.0.48-pre.5 75 12/30/2024
1.0.48-pre.4 79 12/29/2024
1.0.48-pre.3 65 12/28/2024
1.0.48-pre.2 68 12/27/2024
1.0.48-pre.1 70 12/27/2024
1.0.47-pre.1 75 12/27/2024
1.0.46 292 12/26/2024
1.0.45 108 12/26/2024
1.0.44 101 12/26/2024
1.0.43 98 12/26/2024
1.0.42 108 12/26/2024
1.0.41 122 12/26/2024
1.0.40 100 12/26/2024
1.0.39 107 12/25/2024
1.0.38 257 12/23/2024
1.0.37 264 12/23/2024
1.0.36 98 12/23/2024
1.0.35 130 12/23/2024
1.0.34 328 12/19/2024
1.0.33 248 12/16/2024
1.0.32 339 12/6/2024
1.0.31 278 12/5/2024
1.0.30 155 12/4/2024
1.0.29 258 12/2/2024
1.0.28 122 12/2/2024
1.0.27 144 12/2/2024
1.0.26 188 12/1/2024
1.0.25 188 12/1/2024
1.0.24 157 11/30/2024
1.0.23 123 11/29/2024
1.0.22 208 11/27/2024
1.0.21 181 11/26/2024
1.0.20 200 11/20/2024
1.0.19 316 11/15/2024
1.0.18 191 11/14/2024
1.0.17 177 11/13/2024
1.0.16 425 11/4/2024
1.0.15 185 11/1/2024
1.0.14 598 10/17/2024
1.0.13 403 10/7/2024
1.0.12 160 10/4/2024
1.0.11 324 9/23/2024
1.0.10 145 9/20/2024
1.0.10-pre.1 59 12/27/2024
1.0.9 166 9/19/2024
1.0.8 170 9/19/2024
1.0.7 116 9/19/2024
1.0.6 145 9/19/2024
1.0.5 125 9/18/2024
1.0.4 120 9/18/2024
1.0.3 231 9/18/2024
1.0.2 160 9/18/2024
1.0.1 227 9/14/2024
1.0.0 129 9/14/2024

## v1.2.2 (patch)

Changes since v1.2.1:

- Update README with comprehensive documentation and usage examples ([@matt-edmondson](https://github.com/matt-edmondson))

## v1.2.1 (patch)

Changes since v1.2.0:

- Update packages ([@matt-edmondson](https://github.com/matt-edmondson))

## v1.2.0 (minor)

Changes since v1.1.0:

- Add LICENSE template ([@matt-edmondson](https://github.com/matt-edmondson))

## v1.1.0 (minor)

Changes since v1.0.0:

- Add mailmap ([@matt-edmondson](https://github.com/matt-edmondson))
- Add null check in TestRoundTrip to prevent null exception ([@matt-edmondson](https://github.com/matt-edmondson))
- Add scripts for automated metadata generation and versioning ([@matt-edmondson](https://github.com/matt-edmondson))
- Apply new editorconfig ([@matt-edmondson](https://github.com/matt-edmondson))
- Migrate ktsu.io to ktsu namespace ([@matt-edmondson](https://github.com/matt-edmondson))
- Refactor test method names to camel case ([@matt-edmondson](https://github.com/matt-edmondson))
- Rename package to ktsu.ToStringJsonConverter and update ktsu.Extensions to version 1.0.30 ([@matt-edmondson](https://github.com/matt-edmondson))
- Renamed metadata files ([@matt-edmondson](https://github.com/matt-edmondson))
- Replace LICENSE file with LICENSE.md and update copyright information ([@matt-edmondson](https://github.com/matt-edmondson))
- Update ktsu.Extensions package reference to version 1.0.31 ([@matt-edmondson](https://github.com/matt-edmondson))
- Update LICENSE file to LICENSE.md with copyright notice ([@matt-edmondson](https://github.com/matt-edmondson))

## v1.0.48 (patch)

Changes since v1.0.48-pre.30:

- Add mailmap ([@matt-edmondson](https://github.com/matt-edmondson))
- Add scripts for automated metadata generation and versioning ([@matt-edmondson](https://github.com/matt-edmondson))

## v1.0.42 (patch)

Changes since v1.0.41:

- Replace LICENSE file with LICENSE.md and update copyright information ([@matt-edmondson](https://github.com/matt-edmondson))

## v1.0.40 (patch)

Changes since v1.0.39:

- Refactor test method names to camel case ([@matt-edmondson](https://github.com/matt-edmondson))
- Update LICENSE file to LICENSE.md with copyright notice ([@matt-edmondson](https://github.com/matt-edmondson))

## v1.0.35 (patch)

Changes since v1.0.34:

- Add null check in TestRoundTrip to prevent null exception ([@matt-edmondson](https://github.com/matt-edmondson))

## v1.0.26 (patch)

Changes since v1.0.25:

- Update ktsu.Extensions package reference to version 1.0.31 ([@matt-edmondson](https://github.com/matt-edmondson))

## v1.0.25 (patch)

Changes since v1.0.24:

- Rename package to ktsu.ToStringJsonConverter and update ktsu.Extensions to version 1.0.30 ([@matt-edmondson](https://github.com/matt-edmondson))

## v1.0.1 (patch)

Changes since v1.0.0:

- Migrate ktsu.io to ktsu namespace ([@matt-edmondson](https://github.com/matt-edmondson))

## v1.0.0 (major)

Changes since 0.0.0.0:

- 1.0.0 ([@matt-edmondson](https://github.com/matt-edmondson))
- Add documentation comments ([@matt-edmondson](https://github.com/matt-edmondson))
- Add project files. ([@matt-edmondson](https://github.com/matt-edmondson))
- Suppress specific warnings in csproj ([@matt-edmondson](https://github.com/matt-edmondson))
- Update Extensions package, and project version ([@matt-edmondson](https://github.com/matt-edmondson))