JSON (JavaScript Object Nota­tion) is a data trans­fer for­mat widely adopted on the web because it’s light­weight com­pared to XML. I touched on the topic of JSON in an ear­lier post when I posted some exten­sion meth­ods for compressing/decompressing string.

If you are unfa­mil­iar with the JSON for­mat or how it’s sup­ported in .Net then you should take a look at the MSDN arti­cles in the ref­er­ences sec­tion to get you started.

With regards to Date­Time val­ues, they appear as JSON strings in the form of “\/Date(1276675934513+0100)\/” which is not easy to read when you’re debug­ging. In order to con­vert this JSON string back to a humanly read­able form you can use this LIN­Q­Pad to parse it, just replace the JSON string at the top:

var json = "\"\\/Date(1276675934513+0100)\\/\"";
json.Dump();
var serializer = new DataContractJsonSerializer(typeof(DateTime));
var memString = new MemoryStream(Encoding.ASCII.GetBytes(json));
var d = (DateTime) serializer.ReadObject(memString);
d.Dump();

If you’re not using LIN­Q­Pad already then you should! Writ­ten by Joe Alba­hari (co-writer of the C# in a Nut­shell books) it can be a huge time saver because you can use it as a code snip­pet edi­tor in addi­tion to being a Data­base query tool.

Ref­er­ence:

JSON.org

An intro­duc­tion to JSON in JavaScript and .Net

Stand-alone JSON serialization

JSON For­mat­ter & Validator

JSON.Net Code­plex project

LIN­Q­Pad

Share

http://v.youku.com/v_show/id_XNTQ1NDk0MDg=.html

Share

Months of work has finally bore fruit as my sec­ond game goes into pub­lic beta! Come and join in on the fun, it’s nice here:

http://apps.facebook.com/superfuntown

Go on! Try it out! Let us know what you think, your feed­backs are much appreciated!

About the Game

Super­Fun­Town is a city build­ing game where you assume the role of a mayor, entrusted with the task of bring­ing a rural town into a city buzzing with life! You’ll find all the famil­iar game mechan­ics such as zoom­ing, build­ing items, col­lect­ing items, and much more!

In Super­Fun­Town you are given all the free­dom to cus­tomize and make the town truly your own. You have the options to rotate any­thing in your see and you can even cus­tomize the look of your houses by paint­ing the walls and roofs to match the theme of your town.

And it wouldn’t be a social game if you can’t inter­act with oth­ers, would it? Luck­ily, there are ample oppor­tu­ni­ties for you to inter­act with friends here ;-), you can invite them to live and work in your town; you can go visit their town and help them out by bring­ing donuts to work­ers in their shops and fac­to­ries; you can send and receive nice gifts; hell, you can even kick them out of your houses if you really wanted!!

Screen­shots

The wel­come screen, where you can change the name of your town and get help on the rules of the game.

image

You start off with a rural town with a farm house and some fields.

image

As your town expands and your stature as a mayor grows, you get to build attrac­tions, shops, fac­to­ries, houses, etc. You can even invite a friend to live and work in your town!

image

You can zoom right in and see your town in close quarters.

image

You can stock your shops with goods and stocks which in time will make you a profit, and you even get a bonus if a friend is work­ing in your shop! :-)

image

You can also visit a friend’s town, and give donuts to the men and women work­ing there, and boost their productivity!

image

Peo­ple from far and beyond will love to come and live in your town, and they will arrive in a steady stream on buses, but as your town becomes more advanced, you can upgrade the trans­port of choice from buses to planes!

image

There’s a wide range of houses, shops for you to choose from, and more will be avail­able as you level up.

image

And don’t for­get, as a mayor you need to pro­vide social infra­struc­tures for your res­i­dents so your town can con­tinue its growth.

image

And, you can even cus­tomize the look of your houses, go on, let the artist in you go wild!

image

Share

In C#, there are two ways you can pro­vide an alter­na­tive imple­men­ta­tion of an inher­ited method:

Over­ride a vir­tual method

Unlike Java where every method can be over­rid­den by default unless marked with the final key­word, C# employs a safer, opt-in sys­tem where meth­ods can­not be over­rid­den unless marked with the vir­tual keyword.

A vir­tual method defined in the base class can be over­rid­den in a derived class by defin­ing a method with the same sig­na­ture and mark­ing it with the over­ride keyword.

Hid­ing a base method

With­out mark­ing a method in the base class with the vir­tual key­word it is still pos­si­ble (albeit not rec­om­mended) to pro­vide an over­ride by ‘hid­ing’ the method defined in the base class. All you need to do is to declare the same method sig­na­ture in the derived class.

Philo­soph­i­cal Difference

Under nor­mal cir­cum­stances, hid­ing a base method is a code smell. It unnec­es­sar­ily com­pli­cates the rela­tion­ship between the base and derived class as it’s not imme­di­ately clear which method is invoked when you call derived.Foo() if Foo() is declared in both the base and derived class.

To lessen this con­fu­sion, you should use the new key­word when­ever you absolutely have to hide a method in the base class:

public class BaseClass
{
    public void Foo() { … }
}

public class DerivedClass
{
    public new void Foo() { … }
}

This way, when some­one looks at your code it at least offers some indi­ca­tion that Foo() is an over­ride and more impor­tantly, it makes a state­ment of your inten­tion to hide the Foo() method in the base class (as opposed to it look­ing like a mistake).

Over­rid­ing a vir­tual method on the other hand, is very much part and par­cel of object ori­ented design, and an inte­gral part of the Tem­plate pat­tern (one of my favourites :-P) to allow vari­a­tions in the behav­iour of the con­crete classes. It allows you to make a clear state­ment both in the base and derived class that a method is intended and some­times expected to be overridden.

Seman­tic Difference

Seman­ti­cally there’s a very sub­tle dif­fer­ence between the two approaches because the tim­ing in which the method to invoke is deter­mined dif­fers between the two:

  • When you hide a base method the method is resolved at com­pile time – which method is invoked at run­time is pre­de­ter­mined based on what they look like at com­pile time and baked into the com­piled dll.
  • When you over­ride a vir­tual method the method is resolved at run­time – the CLR deter­mines which method to invoke by look­ing up a ‘call table’ and find the near­est over­ride of the method.

Here’s a sce­nario where you will notice the difference:

Imag­ine you have two classes TypeA and TypeB, located in Assem­blyA and Assem­blyB respec­tively:

public class TypeA // in AssemblyA.dll
{
    public void Foo()
    {
        Console.Write("TypeA.Foo()");
    }

    public virtual void Boo()
    {
        Console.Write("TypeA.Boo()");
    }
}

public class TypeB : TypeA // in AssemblyB.dll
{
}

In my appli­ca­tion which uses both libraries, I have these lines of code:

var typeb = new TypeB();
typeb.Foo();
Console.WriteLine();
typeb.Boo();

And no prizes for guess­ing the out­put of this code:

image

Now, sup­pose I decided at a later date that I wish to over­ride Foo() and Boo() in TypeB:

public class TypeB : TypeA // in AssemblyB.dll
{
    public void Foo()
    {
        Console.Write("TypeB.Foo()");
        base.Foo();
    }

    public override void Boo()
    {
        Console.Write("TypeB.Boo()");
        base.Boo();
    }
}

See­ing as noth­ing else have changed, so I recom­pile only Assem­blyB and dis­trib­ute the new dll, what do you think the out­put is when I rerun my appli­ca­tion? Well, a lit­tle surprisingly:

image

Ref­er­ences:

Eric Lippert’s blog post: putting a base in the middle

Share