C#, Consuming anonymous types at a later point

Last night I was dealing with anonymous types and some IL code to creating and assigning values to instances of anonymous types. I stumbled upon this question:

How do I declare a Field with Anonymous Type (C#)

- http://stackoverflow.com/questions/964334/how-do-i-declare-a-field-with-anonymous-type-c

It didn’t have anything to do with what I was doing but the thing I was doing would solve the issue. I was bringing anonymous types support to ServiceStack.Text, the .Net communities most awesome Serialization framework. Yes I know that the feature already exists in JSON.Net, but when it comes to serialization, I want speed, hence why I turn to ServiceStack.

Alternative 1 – Lets solve the problem using JSON

Step 1 – Install ServiceStack.Text v3.3.6 or later

This feature was introduced in v3.3.6, so use that NuGet.

install-package ServiceStack.Text

Step 2 – Get some data

For this simple demo I will just use some simple person entities that gets yielded.

public class Person
{
	public string Firstname { get; set; }
	public string Lastname { get; set; }
	public int Age { get; set; }
}

public static class Db
{
	public static IEnumerable<Person> Persons()
	{
		yield return new Person
		{
			Firstname = "Hans",
			Lastname = "Wertheim", 
			Age = 30
		};
		yield return new Person
		{
			Firstname = "Daniel", 
			Lastname = "Wertheim", 
			Age = 31
		};
		yield return new Person
		{
			Firstname = "Anton",
			Lastname = "Wertheim", 
			Age = 32
		};
	}
}

Step 3 – Turn it into JSON

Now we need the state turned into JSON. If we want we could transform the data now. Lets combine Firstname and Lastname to Name.

var personsAsJson = Db.Persons()
	.Select(p => new
	{
		p.Age, 
		Name = string.Concat(p.Firstname, " ", p.Lastname)
	})
	.Select(JsonSerializer.SerializeToString)
	.ToArray();

Step 4 – Define a extension method for deserialization

Once we have the JSON blob, we need a deserialization method allowing us to define a anonymous type, used as a template.

public static IEnumerable<T> ToAnonymousType<T>(
	this IEnumerable<string> json, T template) where T : class
{
	TypeConfig<T>.EnableAnonymousFieldSetters = true;
	var templateType = template.GetType();
	return json.Select(j => JsonSerializer.DeserializeFromString(j, templateType) as T);
}

Step 5 – Consume it as we want in another domain

Now, in another domain we could just turn it into a anonymous type of our like. E.g only picking out Name.

var anonymousPersons = personsAsJson.ToAnonymousType(new {Name = default(string)});

foreach (var anonymousPerson in anonymousPersons)
	Console.WriteLine(anonymousPerson.Name);

That’s it. No classes (other than anonymous) used in the tranformations what so ever. Not saying you should, just saying you could.

//Daniel

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s