{"id":26800573,"url":"https://github.com/ktsu-dev/tostringjsonconverter","last_synced_at":"2025-10-05T17:30:15.023Z","repository":{"id":245052265,"uuid":"817179671","full_name":"ktsu-dev/ToStringJsonConverter","owner":"ktsu-dev","description":null,"archived":false,"fork":false,"pushed_at":"2025-07-22T11:51:39.000Z","size":359,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-09-11T12:20:03.603Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PowerShell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ktsu-dev.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":"AUTHORS.md","dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-06-19T07:23:14.000Z","updated_at":"2025-07-22T11:51:43.000Z","dependencies_parsed_at":"2024-06-19T11:41:53.432Z","dependency_job_id":"37a81b52-91b6-4c7e-9212-0a1bf44786b1","html_url":"https://github.com/ktsu-dev/ToStringJsonConverter","commit_stats":null,"previous_names":["ktsu-io/tostringjsonconverter","ktsu-dev/tostringjsonconverter"],"tags_count":113,"template":false,"template_full_name":null,"purl":"pkg:github/ktsu-dev/ToStringJsonConverter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ktsu-dev%2FToStringJsonConverter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ktsu-dev%2FToStringJsonConverter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ktsu-dev%2FToStringJsonConverter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ktsu-dev%2FToStringJsonConverter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ktsu-dev","download_url":"https://codeload.github.com/ktsu-dev/ToStringJsonConverter/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ktsu-dev%2FToStringJsonConverter/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278487035,"owners_count":25995104,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-05T02:00:06.059Z","response_time":54,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-03-29T20:17:52.253Z","updated_at":"2025-10-05T17:30:15.017Z","avatar_url":"https://github.com/ktsu-dev.png","language":"PowerShell","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ktsu.ToStringJsonConverter\n\n\u003e A JSON converter factory that serializes objects using their ToString and FromString methods.\n\n[![License](https://img.shields.io/github/license/ktsu-dev/ToStringJsonConverter)](https://github.com/ktsu-dev/ToStringJsonConverter/blob/main/LICENSE.md)\n[![NuGet](https://img.shields.io/nuget/v/ktsu.ToStringJsonConverter.svg)](https://www.nuget.org/packages/ktsu.ToStringJsonConverter/)\n[![NuGet Downloads](https://img.shields.io/nuget/dt/ktsu.ToStringJsonConverter.svg)](https://www.nuget.org/packages/ktsu.ToStringJsonConverter/)\n[![Build Status](https://github.com/ktsu-dev/ToStringJsonConverter/workflows/build/badge.svg)](https://github.com/ktsu-dev/ToStringJsonConverter/actions)\n[![GitHub Stars](https://img.shields.io/github/stars/ktsu-dev/ToStringJsonConverter?style=social)](https://github.com/ktsu-dev/ToStringJsonConverter/stargazers)\n\n## Introduction\n\n`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.\n\n## Features\n\n- **Automatic Type Detection**: Automatically identifies types with compatible `FromString` methods.\n- **String-Based Serialization**: Converts objects to and from JSON using their string representation.\n- **Property Name Support**: Works with both JSON values and property names.\n- **Reflection Optimization**: Uses cached reflection for improved performance.\n- **Generic Method Support**: Handles both generic and non-generic `FromString` methods.\n\n## Installation\n\n### Package Manager Console\n\n```powershell\nInstall-Package ktsu.ToStringJsonConverter\n```\n\n### .NET CLI\n\n```bash\ndotnet add package ktsu.ToStringJsonConverter\n```\n\n### Package Reference\n\n```xml\n\u003cPackageReference Include=\"ktsu.ToStringJsonConverter\" Version=\"x.y.z\" /\u003e\n```\n\n## Usage Examples\n\n### Basic Example\n\n```csharp\nusing System.Text.Json;\nusing ktsu.ToStringJsonConverter;\n\n// Configure the converter in your JsonSerializerOptions\nvar options = new JsonSerializerOptions();\noptions.Converters.Add(new ToStringJsonConverterFactory());\n\n// Example custom type with ToString and FromString\npublic class CustomId\n{\n    public string Value { get; set; }\n    \n    public static CustomId FromString(string value) =\u003e new() { Value = value };\n    \n    public override string ToString() =\u003e Value;\n}\n\n// Serialization\nvar id = new CustomId { Value = \"12345\" };\nstring json = JsonSerializer.Serialize(id, options);\n// json is now: \"12345\"\n\n// Deserialization\nCustomId deserialized = JsonSerializer.Deserialize\u003cCustomId\u003e(json, options);\n// deserialized.Value is now: \"12345\"\n```\n\n### Integration with Other Converters\n\n```csharp\nusing System.Text.Json;\nusing System.Text.Json.Serialization;\nusing ktsu.ToStringJsonConverter;\n\nvar options = new JsonSerializerOptions\n{\n    WriteIndented = true,\n    PropertyNamingPolicy = JsonNamingPolicy.CamelCase,\n    Converters =\n    {\n        new ToStringJsonConverterFactory(),\n        new JsonStringEnumConverter()\n    }\n};\n\n// Now both enum values and custom types with FromString will be handled appropriately\n```\n\n## Advanced Usage\n\n### Working with Collections of Custom Types\n\n```csharp\nusing System.Text.Json;\nusing ktsu.ToStringJsonConverter;\n\n// Setup serializer options with the converter\nvar options = new JsonSerializerOptions();\noptions.Converters.Add(new ToStringJsonConverterFactory());\n\n// A collection of custom types\nList\u003cCustomId\u003e ids = new()\n{\n    new CustomId { Value = \"A001\" },\n    new CustomId { Value = \"B002\" },\n    new CustomId { Value = \"C003\" }\n};\n\n// Serialize the collection\nstring json = JsonSerializer.Serialize(ids, options);\n// json is now: [\"A001\",\"B002\",\"C003\"]\n\n// Deserialize back to a collection\nList\u003cCustomId\u003e deserializedIds = JsonSerializer.Deserialize\u003cList\u003cCustomId\u003e\u003e(json, options);\n```\n\n### Using with Dictionaries as Keys\n\n```csharp\n// Custom type can be used as dictionary keys\nvar dictionary = new Dictionary\u003cCustomId, string\u003e\n{\n    { new CustomId { Value = \"key1\" }, \"value1\" },\n    { new CustomId { Value = \"key2\" }, \"value2\" }\n};\n\nstring json = JsonSerializer.Serialize(dictionary, options);\n// Serializes as a dictionary with string keys\n\nvar deserialized = JsonSerializer.Deserialize\u003cDictionary\u003cCustomId, string\u003e\u003e(json, options);\n// Keys are properly deserialized back to CustomId objects\n```\n\n## API Reference\n\n### ToStringJsonConverterFactory\n\nThe primary class for integrating with System.Text.Json serialization.\n\n#### Methods\n\n| Name | Return Type | Description |\n|------|-------------|-------------|\n| `CanConvert(Type typeToConvert)` | `bool` | Determines if a type can be converted by checking for a static FromString method |\n| `CreateConverter(Type typeToConvert, JsonSerializerOptions options)` | `JsonConverter` | Creates a type-specific converter instance |\n\n### Compatibility Requirements\n\nFor a type to work with ToStringJsonConverter, it must meet these requirements:\n\n1. Have a public static `FromString(string)` method that returns an instance of the type\n2. Override `ToString()` to provide a string representation that can be reversed by `FromString`\n\n## Contributing\n\nContributions are welcome! Here's how you can help:\n\n1. Fork the repository\n2. Create your feature branch (`git checkout -b feature/amazing-feature`)\n3. Commit your changes (`git commit -m 'Add some amazing feature'`)\n4. Push to the branch (`git push origin feature/amazing-feature`)\n5. Open a Pull Request\n\nPlease make sure to update tests as appropriate.\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fktsu-dev%2Ftostringjsonconverter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fktsu-dev%2Ftostringjsonconverter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fktsu-dev%2Ftostringjsonconverter/lists"}