C# to JSON

I got tired of writing throw-away code to generate JSON representations of my C# model classes (typically for ASP.NET & WebApi/REST applications), so I built this converter tool.

It uses Microsoft Roslyn to compile C# code on the fly and then serialize it to JSON.

Give it a try!

  • You must supply compile-able C# code, ideally just the classes and/or structs that make up your models. No main() function or any other application code necessary. The tool will create an instance of each type using its default constructor.
  • Common using statements (ie. System, System.Collections.Generic, etc.), as well as namespace declarations, are not required, just the class declarations themselves. However, you can always add using statements for non-standard DTO types.
  • If a class depends on another type, be sure to include the dependencies as class definitions.
  • If you want your JSON to be populated with sample data, initialize those properties in a constructor, otherwise you'll get null/default values.
  • If you want a class excluded from your JSON, create a default constructor for that class and mark it as private. For example, in the sample below if you only want JSON for ContactsCollection and not the dependencies Contact or Address, change their default constructor(s) to private.
See below for a typical example and try it out.
Note: This tool is only intended to consume reasonably simple C# class hierarchies that have no external or 3rd party dependencies, anything too complex will probably generate an error.

  Generate comments
  Camel case