Skip to content

DaniilVdovin/CsvOutputFormatter

Repository files navigation

CsvOutputFormatter Banner

CsvOutputFormatter

🇷🇺 Русская версия

.NET 8 License


Custom IOutputFormatter for .NET 10 Web API that automatically serializes IEnumerable<T> to CSV when Accept: text/csv header is present.

✨ Features

  • 🔄 Auto-detection — CSV on Accept: text/csv, JSON otherwise
  • 📦 Streaming — Low memory footprint for large datasets
  • 🔤 Proper escaping — Handles commas, quotes, newlines correctly
  • 📅 Smart formatting — ISO 8601 dates, lowercase booleans
  • 🔧 Easy setup — One-line registration via extension method
  • 🎯 Type-safe — Works with any IEnumerable<T>

🚀 Quick Start

1. Register in Program.cs

using CsvOutputFormatter.Extensions;

var builder = WebApplication.CreateBuilder(args);

builder.Services
    .AddControllers()
    .AddCsvOutputFormatter();

var app = builder.Build();
app.MapControllers();
app.Run();

2. Create your controller

[ApiController]
[Route("api/[controller]")]
public class TradesController : ControllerBase
{
    [HttpGet]
    public IEnumerable<Trade> GetTrades() => _service.GetAll();
}

public record Trade(int Id, string Symbol, decimal Price, DateTime Timestamp, string Side);

3. Request data

Request Response
GET /api/trades
Accept: application/json
[{"id":1,"symbol":"AAPL","price":178.52}]
GET /api/trades
Accept: text/csv
csv\nId,Symbol,Price,Timestamp,Side\n1,AAPL,178.52,2024-01-15T10:30:00,Buy

⚙️ Configuration

Custom formatter priority

builder.Services.AddControllers(options =>
{
    options.OutputFormatters.Insert(0, new CsvOutputFormatter());
});

With extension method options

builder.Services
    .AddControllers()
    .AddCsvOutputFormatter(options =>
    {
        // Configure MvcOptions here if needed
    });

📋 Supported Types

Type Format
string, int, decimal, etc. Default ToString()
DateTime ISO 8601: yyyy-MM-ddTHH:mm:ss
bool true / false
null Empty string

🔒 CSV Escaping

Values containing ,, ", \n, or \r are automatically:

  • Wrapped in double quotes
  • Internal quotes escaped as ""

Example: value "with" commas"value ""with"" commas"

📁 Project Structure

CsvOutputFormatter/
├── Formatters/
│   └── CsvOutputFormatter.cs    # Core formatter implementation
├── Extensions/
│   └── ServiceCollectionExtensions.cs  # Registration helpers
├── assets/
│   └── banner.svg                 # Project banner
├── README.md                      # English docs (this file)
├── README-RU.md                   # Russian docs
└── CsvOutputFormatter.csproj      # NuGet package config

⚠️ Limitations

  • Works with IEnumerable<T> of complex types only
  • Serializes public parameterless properties only
  • Nested objects rendered via ToString()
  • For complex formatting, use dedicated DTOs

🤝 Contributing

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

📄 License

Distributed under the MIT License. See LICENSE for more information.


Built with ❤️ for .NET developers
↑ Back to top ↑

About

Custom IOutputFormatter for .NET 10 Web API that automatically serializes IEnumerable<T> to CSV when Accept: text/csv header is present.

Topics

Resources

License

Stars

Watchers

Forks

Contributors

Languages