Serialization

Creating a JSON Array from SQL Rows in C# Azure Function

In this article, we will explore how to create a JSON array from SQL rows in C# Azure Function. We will look at two approaches: one using ADO.NET and another using Entity Framework. We will use a "customer" table in our examples. This article is a comprehensive guide for anyone looking to convert SQL data into a JSON array using C# in Azure Function. Here is the code to serialize SQL data into JSON array using C# Azure Function with an HttpTrigger: With ADO.NET using System; using System.Data.SqlClient; using System.Net; using M…

Optimizing Deserialization with Newtonsoft.Json in C#: Techniques for Handling Private Setters

When deserializing objects in C#, it can be a common issue to encounter private setters. This can cause problems when trying to set properties during deserialization, as private setters cannot be accessed directly. In this article, we will discuss different options for deserializing objects that have private setters in C# and recommend the one with the best performance. We will also provide C# code examples to help you implement these solutions in your own code. Option 1: Using the JsonObjectAttribute The JsonObjectAttribute allows you to spec…

How to Map Json String to a Model in ASP.NET Core?

In this post, we are using .NET Core (.NET 6), we are trying to map a Json string to a class model in ASP.NET Core Web app. Here is the Json string: { "items" : [ { "ID" : 1 , "User" : { "FirstName" : "John" , "LastName" : "Smith" , "PhoneNumber" : "50-745671" , "Address" : "804 Foster Avenue" }, }, …

Serialize EntityFramework Object and Remove Null properties

In the following code, I'm providing a way to serialize an entity object from the EntityFramework taking into consideration the relationships with other entities. I'm providing a way to remove the empty properties, null values and empty arrays from the Json string. First of all, it is important to mention that the following code is applicable only for Newtonsoft API, I'm using JsonConvert.SerializeObject for serialization. Make sure to add a reference to the Newtonsoft.Json.dll. Serialize…

Sort Properties in JSON Object using Newtonsoft.Json

T his post is applicable if you are using Newtonsoft.Json for serialization/deserialization. In this article, I'm sharing C# code to sort the properties in the JSON Object. The order of properties in a JSON  object  is undefined, so the properties are listed in the order they were added to the object Sample JSON String String jsonString = @"{""Email"":""john@mail.com"",""Website"":""http://samplewebsite.com"",""Name…

Load More
That is All