Back to blog

Swift Types from C#

Do you have a C# codebase with model classes for a JSON API? Did you ever have to use that API with another programming language, wishing there was a way to automatically convert your C# models to that language?

Do you have a C# codebase with model classes for a JSON API? Did you ever have to use that API with another programming language, wishing there was a way to automatically convert your C# models to that language?

Now you can! quicktype can read C# source files and generate equivalent types in Swift, TypeScript, Go, and many other languages.

Example

Given this C# model:

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public DateTime CreatedAt { get; set; }
    public List<Post> Posts { get; set; }
}

public class Post
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }
}

Generate Swift code:

quicktype --src-lang csharp User.cs -o User.swift

Result:

struct User: Codable {
    let id: Int
    let name: String
    let email: String
    let createdAt: Date
    let posts: [Post]
}

struct Post: Codable {
    let id: Int
    let title: String
    let content: String
}

Supported Source Languages

  • C# (.cs files)
  • JSON Schema
  • GraphQL (query + schema)
  • TypeScript (type definitions)

Use Cases

  • Cross-platform development: Share types between .NET backend and Swift iOS app
  • API documentation: Generate client SDKs from server models
  • Migration: Convert a codebase from one language to another