# Introduction to CSharp2Dart.com

If you've ever written a mobile app with Flutter that integrates with an API, then you'll know that there are a lot of model objects that get passed between the app and the API.

JSON Serialization in Flutter can also take a fair amount of boilerplate code in order to work and can feel somewhat tedious and repetitive. If you're building your API using C#, you'll also duplicate the same model structure in C# in order to receive the serialized models from Flutter and vice versa.

With this in mind, and to help remove some of the slog work, I've created https://www.csharp2dart.com/.
![A screenshot of the csharp2dart website](https://cdn.hashnode.com/res/hashnode/image/upload/v1664281619126/p1SUOIY0D.png)

The site provides a simple interface where you can paste your C# code and then generate it's Dart equivalent. If the site looks somewhat familiar, I did take some inspiration from [https://csharp2json.io/](https://csharp2json.io/)

You'll have some control over the code that is generated by setting the following options:

**a) Add @JsonSerialization() annotation**
This will add an import and part statement to the top of the file as well as decorate the class with the @JsonSerialization() annotation. e.g.:
![Json Serialization annotation screenshot](https://cdn.hashnode.com/res/hashnode/image/upload/v1664281620558/2AEf3I6zD.png) 

**b) Add toJson() method**
This option generates the _toJson()_ method, which in turn will make it easy to serialize the object to JSON. The [json_serializable ](https://pub.dev/packages/json_serializable) package will generate this method for you and it can be found inside the generated _*.g.dart_ file.
![toJson method screenshot](https://cdn.hashnode.com/res/hashnode/image/upload/v1664281622624/GR-6bVOOI.png) 

**c) Add fromJson() method**
This method does the inverse of the _toJson()_ method. With this method you can create a new instance of your class from JSON. As with the _toJson()_ method json_serializable does all the code generation.
![fromJson method screenshot](https://cdn.hashnode.com/res/hashnode/image/upload/v1664281623857/y3B4VH3aS.png)

**d) All fields should be nullable**
If you need all the fields of the Dart class to be nullable, setting this property will automatically do that for you.
![Nullable fields option screenshot](https://cdn.hashnode.com/res/hashnode/image/upload/v1664281625147/12K2NE8nN.png)
 
**e) Required constructor parameters**
The last option, if set, makes all the constructor parameters required.
![Required constructor parameters screenshot](https://cdn.hashnode.com/res/hashnode/image/upload/v1664281626437/EcffjtNCN.png)
 
If you want to generate more than one class at a time, it is possible. For example, the following code:
```csharp
	public class Product
	{
		public int ProductId { get; set; }		
		public string Title { get; set; }		
		public string SubTitle { get; set; }		
		public bool IsActive { get; set; } = true;	
	}
	
	public class ProductOption 
	{		
		public string Title { get; set; }
		public double Price { get; set; }
		public int QuantityInStock { get; set; }
	}
```

will produce two Dart classes:

```dart
// -- product.dart --
class Product {
	int productId;
	String title;
	String subTitle;
	bool isActive;

	Product(this.productId,this.title,this.subTitle,this.isActive,);
}
// -- product_option.dart --
class ProductOption {
	String title;
	double price;
	int quantityInStock;

	ProductOption(this.title,this.price,this.quantityInStock,);
}

```

Please feel free to use it and if you run into any bugs or want to suggest any new feature, please create a new [GitHub issue](https://github.com/Coalition-Software/csharp2dart-issues).

Thank you for reading. Until next time, keep coding!

 -P

> P.S. To learn more about JSON and serialization in Flutter, please see the [Flutter documentation](https://docs.flutter.dev/development/data-and-backend/json).
