https://github.com/jjosh102/json-to-csharp-poco
This tool provides a simple way to convert JSON data to C# POCO (Plain Old CLR Object) classes
https://github.com/jjosh102/json-to-csharp-poco
csharp json show tools
Last synced: 11 months ago
JSON representation
This tool provides a simple way to convert JSON data to C# POCO (Plain Old CLR Object) classes
- Host: GitHub
- URL: https://github.com/jjosh102/json-to-csharp-poco
- Owner: jjosh102
- Created: 2025-01-07T12:11:47.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2025-03-09T23:10:07.000Z (over 1 year ago)
- Last Synced: 2025-03-10T00:18:35.216Z (over 1 year ago)
- Topics: csharp, json, show, tools
- Language: C#
- Homepage: https://jjosh102.github.io/json-to-csharp-poco/
- Size: 62.2 MB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# JSON to C# POCO Converter
A simple tool for converting JSON data into C# POCO (Plain Old CLR Object) classes with support for records, customizable property access, nullable types, and various collection types.
## Features
- Convert JSON to C# classes or records
- Support for nested objects and arrays
- Customizable property access patterns
- Optional JSON property name attributes
- Nullable type support
- Required property markers
- Default value initialization
- Multiple array type options
- Primary constructor support for records
## Settings
### Basic Settings
- `Namespace`: Set the namespace for generated classes (default: "JsonToCsharp")
- `RootTypeName`: Set the name for the root class/record (default: "Root")
- `UseRecords`: Generate C# records instead of classes
- `UsePrimaryConstructor`: Use primary constructor syntax for records (C# 9.0+)
### Property Settings
- `PropertyAccess`: Control property accessor patterns
- `Mutable`: Generate `{ get; set; }` properties
- `Immutable`: Generate `{ get; init; }` properties (C# 9.0+)
- `ArrayType`: Choose collection type for arrays
- `IReadOnlyList`: Immutable list interface
- `List`: Standard mutable list
- `T[]`: Array type
- `AddAttribute`: Add `[JsonPropertyName]` attributes for JSON serialization
- `IsNullable`: Generate nullable reference types
- `IsRequired`: Add `required` keyword to properties (C# 11.0+)
- `IsDefaultInitialized`: Initialize properties with default values
## Examples
### Basic Class Generation
Input JSON:
```json
{
"name": "John",
"age": 30,
"isEmployee": true
}
```
Settings:
```csharp
var options = new ConversionSettings
{
Namespace = "MyNamespace",
UseRecords = false,
PropertyAccess = PropertyAccess.Mutable
};
```
Output:
```csharp
namespace MyNamespace;
public class Root
{
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("age")]
public int Age { get; set; }
[JsonPropertyName("isEmployee")]
public bool IsEmployee { get; set; }
}
```
### Record with Primary Constructor
Settings:
```csharp
var options = new ConversionSettings
{
UseRecords = true,
UsePrimaryConstructor = true,
AddAttribute = true
};
```
Output:
```csharp
public record Root(
[property: JsonPropertyName("name")] string Name,
[property: JsonPropertyName("age")] int Age,
[property: JsonPropertyName("isEmployee")] bool IsEmployee
);
```
### Nullable and Required Properties
Settings:
```csharp
var options = new ConversionSettings
{
IsNullable = true,
IsRequired = true,
PropertyAccess = PropertyAccess.Immutable
};
```
Output:
```csharp
public class Root
{
[JsonPropertyName("name")]
public required string? Name { get; init; }
[JsonPropertyName("age")]
public required int? Age { get; init; }
}
```
### Array Type Options
Input JSON:
```json
{
"items": ["A", "B", "C"]
}
```
Settings for different array types:
```csharp
// IReadOnlyList
options.ArrayType = ArrayType.IReadOnlyList;
// Output: public IReadOnlyList Items { get; init; }
// List
options.ArrayType = ArrayType.List;
// Output: public List Items { get; init; }
// Array
options.ArrayType = ArrayType.Array;
// Output: public string[] Items { get; init; }
```
### Default Initialization
Settings:
```csharp
var options = new ConversionSettings
{
IsDefaultInitialized = true,
PropertyAccess = PropertyAccess.Immutable
};
```
Output:
```csharp
public class Root
{
public string Name { get; init; } = string.Empty;
public IReadOnlyList Tags { get; init; } = [];
public Address Address { get; init; } = new();
}
```
## Type Mapping
The converter automatically maps JSON types to C# types:
- JSON strings → `string`
- JSON numbers → `int` or `double`
- JSON booleans → `bool`
- JSON arrays → `IReadOnlyList`, `List`, or `T[]`
- JSON objects → Nested classes/records
- JSON dates → `DateTime`