![]() |
VOOZH | about |
dotnet add package ktsu.RoundTripStringJsonConverter --version 1.0.10
NuGet\Install-Package ktsu.RoundTripStringJsonConverter -Version 1.0.10
<PackageReference Include="ktsu.RoundTripStringJsonConverter" Version="1.0.10" />
<PackageVersion Include="ktsu.RoundTripStringJsonConverter" Version="1.0.10" />Directory.Packages.props
<PackageReference Include="ktsu.RoundTripStringJsonConverter" />Project file
paket add ktsu.RoundTripStringJsonConverter --version 1.0.10
#r "nuget: ktsu.RoundTripStringJsonConverter, 1.0.10"
#:package ktsu.RoundTripStringJsonConverter@1.0.10
#addin nuget:?package=ktsu.RoundTripStringJsonConverter&version=1.0.10Install as a Cake Addin
#tool nuget:?package=ktsu.RoundTripStringJsonConverter&version=1.0.10Install as a Cake Tool
A versatile JSON converter factory that serializes objects using their ToString method and deserializes using FromString, Parse, Create, or Convert methods.
👁 License
👁 NuGet
👁 NuGet Downloads
👁 Build Status
👁 GitHub Stars
RoundTripStringJsonConverter is a powerful JSON converter factory for System.Text.Json that simplifies serialization and deserialization of custom types by leveraging their string representation methods. It automatically detects and uses the most appropriate conversion method from a prioritized list: FromString, Parse, Create, or Convert. This approach is particularly useful for value types, strong types, domain objects, and any other types where a string representation makes logical sense.
FromString, Parse, Create, and Convert methods with intelligent priority selectionFromString first, then Parse, Create, and finally ConvertInstall-Package ktsu.RoundTripStringJsonConverter
dotnet add package ktsu.RoundTripStringJsonConverter
<PackageReference Include="ktsu.RoundTripStringJsonConverter" Version="x.y.z" />
using System.Text.Json;
using ktsu.RoundTripStringJsonConverter;
// Configure the converter in your JsonSerializerOptions
var options = new JsonSerializerOptions();
options.Converters.Add(new RoundTripStringJsonConverterFactory());
// Example custom type with ToString and FromString
public class UserId
{
public string Value { get; set; }
public static UserId FromString(string value) => new() { Value = value };
public override string ToString() => Value;
}
// Serialization
var userId = new UserId { Value = "USER-12345" };
string json = JsonSerializer.Serialize(userId, options);
// json is now: "USER-12345"
// Deserialization
UserId deserialized = JsonSerializer.Deserialize<UserId>(json, options);
// deserialized.Value is now: "USER-12345"
The converter automatically detects and uses the appropriate method based on priority:
// Type with Parse method (common in .NET)
public class ProductCode
{
public string Code { get; set; }
public static ProductCode Parse(string code) => new() { Code = code };
public override string ToString() => Code;
}
// Type with Create method (factory pattern)
public class OrderId
{
public string Id { get; set; }
public static OrderId Create(string id) => new() { Id = id };
public override string ToString() => Id;
}
// Type with Convert method
public class CategoryName
{
public string Name { get; set; }
public static CategoryName Convert(string name) => new() { Name = name };
public override string ToString() => Name;
}
// All types work seamlessly with the same converter
var options = new JsonSerializerOptions();
options.Converters.Add(new RoundTripStringJsonConverterFactory());
var product = ProductCode.Parse("PROD-ABC");
var order = OrderId.Create("ORD-001");
var category = CategoryName.Convert("Electronics");
// All serialize and deserialize correctly
string productJson = JsonSerializer.Serialize(product, options);
string orderJson = JsonSerializer.Serialize(order, options);
string categoryJson = JsonSerializer.Serialize(category, options);
When multiple methods are available, the converter uses this priority order:
public class MultiMethodType
{
public string Value { get; set; }
// Highest priority - will be used
public static MultiMethodType FromString(string value) => new() { Value = $"FromString:{value}" };
// Lower priority - will be ignored
public static MultiMethodType Parse(string value) => new() { Value = $"Parse:{value}" };
public override string ToString() => Value;
}
// FromString method will be used for deserialization
var result = JsonSerializer.Deserialize<MultiMethodType>("\"test\"", options);
// result.Value will be "FromString:test"
using System.Text.Json;
using System.Text.Json.Serialization;
using ktsu.RoundTripStringJsonConverter;
var options = new JsonSerializerOptions
{
WriteIndented = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Converters =
{
new RoundTripStringJsonConverterFactory(),
new JsonStringEnumConverter()
}
};
// Now both enum values and custom types with conversion methods will be handled appropriately
using System.Text.Json;
using ktsu.RoundTripStringJsonConverter;
// Setup serializer options with the converter
var options = new JsonSerializerOptions();
options.Converters.Add(new RoundTripStringJsonConverterFactory());
// A collection of custom types
List<UserId> userIds = new()
{
UserId.FromString("USER-001"),
UserId.FromString("USER-002"),
UserId.FromString("USER-003")
};
// Serialize the collection
string json = JsonSerializer.Serialize(userIds, options);
// json is now: ["USER-001","USER-002","USER-003"]
// Deserialize back to a collection
List<UserId> deserializedIds = JsonSerializer.Deserialize<List<UserId>>(json, options);
// Custom types can be used as dictionary keys
var userProducts = new Dictionary<UserId, List<ProductCode>>
{
{ UserId.FromString("USER-001"), [ProductCode.Parse("PROD-A"), ProductCode.Parse("PROD-B")] },
{ UserId.FromString("USER-002"), [ProductCode.Parse("PROD-C")] }
};
string json = JsonSerializer.Serialize(userProducts, options);
// Serializes as a dictionary with string keys
var deserialized = JsonSerializer.Deserialize<Dictionary<UserId, List<ProductCode>>>(json, options);
// Keys are properly deserialized back to UserId objects
public class Order
{
public OrderId Id { get; set; }
public UserId CustomerId { get; set; }
public List<ProductCode> Products { get; set; }
public Dictionary<CategoryName, int> CategoryCounts { get; set; }
public DateTime OrderDate { get; set; }
}
// All custom types are automatically handled
var order = new Order
{
Id = OrderId.Create("ORD-001"),
CustomerId = UserId.FromString("USER-123"),
Products = [ProductCode.Parse("PROD-A"), ProductCode.Parse("PROD-B")],
CategoryCounts = new Dictionary<CategoryName, int>
{
{ CategoryName.Convert("Electronics"), 2 }
},
OrderDate = DateTime.UtcNow
};
string json = JsonSerializer.Serialize(order, options);
Order deserializedOrder = JsonSerializer.Deserialize<Order>(json, options);
The primary class for integrating with System.Text.Json serialization.
| Name | Return Type | Description |
|---|---|---|
CanConvert(Type typeToConvert) |
bool |
Determines if a type can be converted by checking for compatible conversion methods |
CreateConverter(Type typeToConvert, JsonSerializerOptions options) |
JsonConverter |
Creates a type-specific converter instance |
The converter looks for static methods in this priority order:
For a type to work with RoundTripStringJsonConverter, it must meet these requirements:
FromString, Parse, Create, or Convert)string parameterToString() to provide a string representation that can be reversed by the conversion method// All of these are valid conversion methods:
public static MyType FromString(string value) { ... }
public static MyType Parse(string value) { ... }
public static MyType Create(string value) { ... }
public static MyType Convert(string value) { ... }
// These will NOT work:
public MyType FromString(string value) { ... } // Not static
public static MyType FromString(int value) { ... } // Wrong parameter type
public static string FromString(string value) { ... } // Wrong return type
public static MyType FromString(string value, IFormatProvider provider) { ... } // Too many parameters
The converter provides comprehensive error handling:
JsonException for non-string JSON tokensArgumentNullException handlingIf you're migrating from the previous ToStringJsonConverter:
ktsu.RoundTripStringJsonConverterusing ktsu.RoundTripStringJsonConverter;new RoundTripStringJsonConverterFactory()FromString methods will continue to work unchangedParse, Create, or Convert methodsContributions are welcome! Here's how you can help:
git checkout -b feature/amazing-feature)git commit -m 'Add some amazing feature')git push origin feature/amazing-feature)Please make sure to update tests as appropriate.
This project is licensed under the MIT License - see the file for details.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 is compatible. net5.0-windows net5.0-windows was computed. net6.0 net6.0 is compatible. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 is compatible. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 is compatible. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 is compatible. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 is compatible. |
| .NET Framework | net461 net461 was computed. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
Showing the top 5 NuGet packages that depend on ktsu.RoundTripStringJsonConverter:
| Package | Downloads |
|---|---|
|
ktsu.AppDataStorage
A .NET library for persistent application data storage using JSON serialization. Provides a simple inherit-and-use pattern with automatic file management, thread-safe operations, debounced saves, backup recovery, and singleton access. Stores data in the user's app data folder with support for custom subdirectories and file names. |
|
|
ktsu.CredentialCache
CredentialCache |
|
|
ktsu.Semantics.Strings
A comprehensive .NET library for creating type-safe, validated string and physics quantity types using semantic meaning. Transform primitive string and numeric obsession into strongly-typed, self-validating domain models with 50+ validation attributes, polymorphic path handling, complete physics system covering 80+ quantities across 8 scientific domains, centralized physical constants with dimensional analysis, and performance-optimized utilities. Features include bootstrap architecture for circular dependency resolution, factory pattern support, dependency injection integration, and enterprise-ready capabilities for building robust, maintainable scientific and domain-specific applications. |
|
|
ktsu.Schema
Schema |
|
|
ktsu.AppData
Application data storage library for .NET that provides type-safe persistence with dependency injection support. Features automatic backup and recovery, debounced saves, mock file system support for testing, and cross-platform storage using the user's app data directory. |
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.11-pre.1 | 72 | 2/17/2026 |
| 1.0.10 | 8,154 | 2/16/2026 |
| 1.0.10-pre.1 | 69 | 2/16/2026 |
| 1.0.9 | 149 | 2/14/2026 |
| 1.0.8 | 135 | 2/14/2026 |
| 1.0.8-pre.3 | 69 | 2/6/2026 |
| 1.0.8-pre.2 | 72 | 2/5/2026 |
| 1.0.8-pre.1 | 82 | 2/3/2026 |
| 1.0.7 | 885 | 2/1/2026 |
| 1.0.6 | 166 | 1/30/2026 |
| 1.0.5 | 365 | 1/28/2026 |
| 1.0.5-pre.4 | 177 | 11/24/2025 |
| 1.0.5-pre.3 | 155 | 11/23/2025 |
| 1.0.5-pre.2 | 126 | 11/23/2025 |
| 1.0.5-pre.1 | 134 | 11/23/2025 |
| 1.0.4 | 1,902 | 9/11/2025 |
| 1.0.3 | 765 | 8/25/2025 |
| 1.0.2 | 438 | 6/16/2025 |
| 1.0.1 | 232 | 6/16/2025 |
| 1.0.1-pre.2 | 177 | 6/16/2025 |
## v1.0.10 (patch)
Changes since v1.0.9:
- Merge remote-tracking branch 'refs/remotes/origin/main' ([@ktsu[bot]](https://github.com/ktsu[bot]))
- Sync .github\workflows\dotnet.yml ([@ktsu[bot]](https://github.com/ktsu[bot]))
- Sync .github\workflows\dotnet.yml ([@ktsu[bot]](https://github.com/ktsu[bot]))
- Sync .github\workflows\dotnet.yml ([@ktsu[bot]](https://github.com/ktsu[bot]))
- Sync .github\workflows\dotnet.yml ([@ktsu[bot]](https://github.com/ktsu[bot]))
## v1.0.10-pre.1 (prerelease)
Changes since v1.0.9:
- Sync .github\workflows\dotnet.yml ([@ktsu[bot]](https://github.com/ktsu[bot]))
- Sync .github\workflows\dotnet.yml ([@ktsu[bot]](https://github.com/ktsu[bot]))
- Sync .github\workflows\dotnet.yml ([@ktsu[bot]](https://github.com/ktsu[bot]))
## v1.0.9 (patch)
Changes since v1.0.8:
- Remove legacy build scripts ([@matt-edmondson](https://github.com/matt-edmondson))
## v1.0.8 (patch)
Changes since v1.0.7:
- Sync .github\workflows\dotnet.yml ([@ktsu[bot]](https://github.com/ktsu[bot]))
- Sync global.json ([@ktsu[bot]](https://github.com/ktsu[bot]))
- Merge remote-tracking branch 'refs/remotes/origin/main' ([@ktsu[bot]](https://github.com/ktsu[bot]))
- Sync scripts\update-winget-manifests.ps1 ([@ktsu[bot]](https://github.com/ktsu[bot]))
- Sync scripts\update-winget-manifests.ps1 ([@ktsu[bot]](https://github.com/ktsu[bot]))
- Sync scripts\PSBuild.psm1 ([@ktsu[bot]](https://github.com/ktsu[bot]))
- Sync global.json ([@ktsu[bot]](https://github.com/ktsu[bot]))
- Merge remote-tracking branch 'refs/remotes/origin/main' ([@ktsu[bot]](https://github.com/ktsu[bot]))
- Sync scripts\PSBuild.psm1 ([@ktsu[bot]](https://github.com/ktsu[bot]))
- Sync scripts\PSBuild.psm1 ([@ktsu[bot]](https://github.com/ktsu[bot]))
- Sync global.json ([@ktsu[bot]](https://github.com/ktsu[bot]))
## v1.0.8-pre.3 (prerelease)
Changes since v1.0.8-pre.2:
- Merge remote-tracking branch 'refs/remotes/origin/main' ([@ktsu[bot]](https://github.com/ktsu[bot]))
- Sync scripts\update-winget-manifests.ps1 ([@ktsu[bot]](https://github.com/ktsu[bot]))
## v1.0.8-pre.2 (prerelease)
Changes since v1.0.8-pre.1:
- Sync scripts\update-winget-manifests.ps1 ([@ktsu[bot]](https://github.com/ktsu[bot]))
- Sync scripts\PSBuild.psm1 ([@ktsu[bot]](https://github.com/ktsu[bot]))
- Sync global.json ([@ktsu[bot]](https://github.com/ktsu[bot]))
## v1.0.8-pre.1 (prerelease)
No significant changes detected since v1.0.8.
## v1.0.7 (patch)
Changes since v1.0.6:
- Refactor null check in RoundTripStringJsonConverterFactory to use Ensure.NotNull ([@matt-edmondson](https://github.com/matt-edmondson))
## v1.0.6 (patch)
Changes since v1.0.5:
- Remove .github\workflows\project.yml ([@matt-edmondson](https://github.com/matt-edmondson))
## v1.0.5 (patch)
Changes since v1.0.4:
- migrate to dotnet 10 ([@matt-edmondson](https://github.com/matt-edmondson))
## v1.0.5-pre.4 (prerelease)
Changes since v1.0.5-pre.3:
- Sync scripts\update-winget-manifests.ps1 ([@ktsu[bot]](https://github.com/ktsu[bot]))
## v1.0.5-pre.3 (prerelease)
Changes since v1.0.5-pre.2:
- Sync .github\workflows\dotnet.yml ([@ktsu[bot]](https://github.com/ktsu[bot]))
## v1.0.5-pre.2 (prerelease)
Changes since v1.0.5-pre.1:
- Sync scripts\PSBuild.psm1 ([@ktsu[bot]](https://github.com/ktsu[bot]))
- Sync .github\workflows\dotnet.yml ([@ktsu[bot]](https://github.com/ktsu[bot]))
## v1.0.5-pre.1 (prerelease)
No significant changes detected since v1.0.5.
## v1.0.4 (patch)
Changes since v1.0.3:
- Fix an issue where classes that have a factory method on a base class would not be converted ([@matt-edmondson](https://github.com/matt-edmondson))
## v1.0.3 (patch)
Changes since v1.0.2:
- Add CompatibilitySuppressions.xml for diagnostic suppression in RoundTripStringJsonConverter ([@matt-edmondson](https://github.com/matt-edmondson))
- Update SDKs and enhance project configuration ([@matt-edmondson](https://github.com/matt-edmondson))
- Update project metadata and package versions ([@matt-edmondson](https://github.com/matt-edmondson))
## v1.0.2 (patch)
Changes since v1.0.1:
- Update copyright notice and enhance winget manifest script ([@matt-edmondson](https://github.com/matt-edmondson))
## v1.0.1 (patch)
Changes since v1.0.0:
- [patch] Force a patch ([@matt-edmondson](https://github.com/matt-edmondson))
- Enhance CI/CD workflow and improve code coverage ([@matt-edmondson](https://github.com/matt-edmondson))
- Update project dependencies and configuration settings ([@matt-edmondson](https://github.com/matt-edmondson))
## v1.0.1-pre.2 (prerelease)
Changes since v1.0.1-pre.1:
- Enhance CI/CD workflow and improve code coverage ([@matt-edmondson](https://github.com/matt-edmondson))
## v1.0.1-pre.1 (prerelease)
No significant changes detected since v1.0.1.
## v1.0.0 (major)
- Refactor test files to improve code organization and consistency ([@matt-edmondson](https://github.com/matt-edmondson))
- Enhance RoundTripStringJsonConverter and fix collection handling in tests ([@matt-edmondson](https://github.com/matt-edmondson))
- Add new SpecStory history file for untitled entry on 2025-06-13 ([@matt-edmondson](https://github.com/matt-edmondson))
- Fix compilation errors and warnings in test suite for RoundTripStringJsonConverter ([@matt-edmondson](https://github.com/matt-edmondson))
- Update documentation and changelog for RoundTripStringJsonConverter ([@matt-edmondson](https://github.com/matt-edmondson))
- Refactor test classes to utilize primary constructor syntax for string types ([@matt-edmondson](https://github.com/matt-edmondson))
- Refactor and enhance testing suite for RoundTripStringJsonConverter ([@matt-edmondson](https://github.com/matt-edmondson))
- Enhance RoundTripStringJsonConverter to support multiple string conversion methods ([@matt-edmondson](https://github.com/matt-edmondson))
- Add SpecStory configuration files and rename library to RoundTripStringJsonConverter ([@matt-edmondson](https://github.com/matt-edmondson))
- Update ktsu.Extensions package reference to version 1.5.5 ([@matt-edmondson](https://github.com/matt-edmondson))
- Remove Directory.Build.props and Directory.Build.targets files; add copyright headers to ToStringJsonConverter and test files. ([@matt-edmondson](https://github.com/matt-edmondson))
- Refactor DESCRIPTION and update project SDK references to ktsu.Sdk.Lib and ktsu.Sdk.Test ([@matt-edmondson](https://github.com/matt-edmondson))
- Update README with comprehensive documentation and usage examples ([@matt-edmondson](https://github.com/matt-edmondson))
- Update packages ([@matt-edmondson](https://github.com/matt-edmondson))
- Add LICENSE template ([@matt-edmondson](https://github.com/matt-edmondson))
- Apply new editorconfig ([@matt-edmondson](https://github.com/matt-edmondson))
- Add mailmap ([@matt-edmondson](https://github.com/matt-edmondson))
- Add scripts for automated metadata generation and versioning ([@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))
- 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))
- Add null check in TestRoundTrip to prevent null exception ([@matt-edmondson](https://github.com/matt-edmondson))
- Update ktsu.Extensions package reference to version 1.0.31 ([@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))
- Migrate ktsu.io to ktsu namespace ([@matt-edmondson](https://github.com/matt-edmondson))
- Update VERSION ([@matt-edmondson](https://github.com/matt-edmondson))
- Migrate ktsu.io to ktsu namespace ([@matt-edmondson](https://github.com/matt-edmondson))
- 1.0.0 ([@matt-edmondson](https://github.com/matt-edmondson))
- Add documentation comments ([@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))
- Add project files. ([@matt-edmondson](https://github.com/matt-edmondson))