How To: Serialize and Deserialize JSON with .NET Framework

Serializing an Object to JSON

To serialize an object to JSON you will need  to create a data contract, which is a class containing all the properties marked with attributes. To demonstrate this I will create a Person class and apply a DataContractAttribute and DataMemberAttribute.

[DataContract]
internal class Person
{
	[DataMember]
	internal string name;

	[DataMember]
	internal int age;
}

Now we need to write some code to populate the Person class with some data and then use DataContractJsonSerializer to serialize the object to Json and just for you information DataContractJsonSerializer has been available since .NET Framework 3.5. (more…)

Writing Python Scripts on Android Mobile

I found a cool app on Google Play recently, QPython 3 which allows you to write Python scripts and run it under a console on your Android mobile phone. I tried typing using the built in keyboard, but if you’re a coder like me and need to type fast you can still connect a USB or Bluetooth Keyboard to your mobile phone.

wpid-tempFileForShare.jpg

In the console window you can still enter your code and run it on the fly or you can choose to write your script in their text editor called QEdit from the menu.

wpid-tempFileForShare.jpg Console Screen.

wpid-tempFileForShare.jpgQEdit – Text Editor

 

How to Convert JSON to a Class in C#

I have been working on .Net projects where I need to work with JSON to a class in C#. I came across a web site created by Jonathan Keith using the popular JSON.Net library by James Newton-King.

The web site allows me to copy and paste JSON code to a form and by pressing the generate button I was able to get back a class that I can copy and use in my .Net project. This web site is very useful to me since I am also using the JSON.Net library in my current project to Deserialise JSON data in to an object.

http://json2csharp.com

json2csharp_web

Design Patterns: Factory Pattern

What is a Factory Pattern?
Factory Pattern take cares of the creation of objects and hides the implementation from the client. Think of a real factory that make cars and there are different type of cars eg. sedans, hatch backs, and suvs.

When do You Use it?
You should use this pattern when you have a code that creates new object of different type but each object has the same method. The code below is a client method called Buy() that has a switch statement that determine which type of car object to instantiate to get the price. (more…)

C#: Joining an Array of Elements Into One Single String

If you need to convert an array to singe delimited string it is easy to do. Below is an example C# code to demonstrate how to join an array of integer values into a single delimited string:

using System;

namespace JoinArrayElements
{
	class MainClass
	{
		public static void Main (string[] args)
		{
			int[] numbers = new int[] { 1,2,3,4 };

			foreach (int num in numbers)
			{
				Console.WriteLine(num);
			}

			var strJoined = string.Join(",", Array.ConvertAll(numbers, Convert.ToString));

			Console.WriteLine ("Joined Integer Array as a Single String: {0}", strJoined);
		}
	}
}

I am using the string.Join() to join my array, the first parameter would be the delimiter character for this example I am using the comma “,”. Then the second parameter is the array that contains all the elements, but this parameter only takes an array of string. Okay we can fix this by converting the array of integers to an array of strings, by using the Array.ConvertAll() method and passing the array values to be converted and set the data type to convert to. (more…)

Shares