Overriding the default serialization behavior in Json.NET

We use Newtonsoft Json.NET heavily in our project to control our JSON serialization. For the most part, it works absolutely perfectly out-of-the-box, but every now and then, we need to customize the way that serialization works for a particular object.

Such an occasion occurred today. Basically, I have a list of EmployeeActivity objects – where each instance is a subclass of EmployeeActivity. So, for example:

public abstract class EmployeeActivity
{
    public TimeSpan Start { get; set; }
    public TimeSpan Stop { get; set; }

    public virtual bool IsAvailable
    {
        get { return false; }
    }
}

public class AbsenceActivity : EmployeeActivity
{
    public string Reason { get; set; }
}

public class BreakActivity : EmployeeActivity
{
    public string BreakName { get; set; }
}

json.NET does a perfect job of serializing my objects, and they rebuild perfectly when I’m back in javascript land – however, I have no way of knowing the actual type of each activity in my array! To alleviate this, I wanted to create my own serializer which also injected the type name into each of the serialized objects – or at least, that’s what I thought I wanted to do!

As it turns out, there’s a bit of an easier option. JsonConvert uses a ContractResolver in order to work out how to Serialize/Deserialize each class, and you can override the default contract resolver when running serialization:

// This class contains the activity list
ActivityViewModel = new ActivityViewModel(DateTime.Today);

// Serialize using a custom contract resolver
string jsonViewModel = JsonConvert.SerializeObject(viewModel,
    Formatting.None, new JsonSerializerSettings
    {
        ContractResolver = new ActivityJsonContractResolver()
    });

The contract resolver’s job is to return a JsonContract which describes the object to be returned. For a POCO, this would typically be an instance of JsonObjectContract, which (amongst other things) describes the properties of the class which should be serialized.

So, all I needed to do was to extend the contract for subclasses of EmployeeActivity, such that it inherited a new Property to be serialized:

public class DiaryActivityJsonContractResolver : DefaultContractResolver
{
    protected override JsonObjectContract CreateObjectContract(Type objectType)
    {
        JsonObjectContract contract = base.CreateObjectContract(objectType);

        if (typeof(EmployeeActivity).IsAssignableFrom(objectType))
        {
            contract.Properties.Add(new JsonProperty
            {
                Readable = true,
                ShouldSerialize = value => true,
                PropertyName = "Type",
                PropertyType = typeof(string),
                Converter = ResolveContractConverter(typeof(string)),
                ValueProvider = new StaticValueProvider(objectType.Name)
            });
        }

        return contract;
    }
}

Truth be told, I’m not 100% sure if I need to implement as many of the properties on the JsonProperty that I’m creating. But you can see what’s going on here – I’m effectively telling the object contract that there’s an additional property called Type which needs to be serialized, that it’s a string, and that it should return whatever value is resolved by the StaticValueProvider. Speaking of which, here’s the last piece of the puzzle – a simple value resolver that always returns the same value:

/// <summary>
/// JSON value provider that always returns a static value
/// </summary>
public class StaticValueProvider : IValueProvider
{
    private readonly object _staticValue;

    public StaticValueProvider(object staticValue)
    {
        _staticValue = staticValue;
    }

    public void SetValue(object target, object value)
    {
        throw new NotSupportedException();
    }

    public object GetValue(object target)
    {
        return _staticValue;
    }
}

json.NET is fantastic, and it’s very extensible, but finding out where to start and what to do can be a little bit tricky. I managed to figure this out only by downloading the source files and poking through the code myself; hopefully this will be helpful to someone else in the same situation!

Posted in Asp.Net, C# at August 8th, 2011. No Comments.

Extension methods for base/derived types

I had a situation today which I thought was prime for writing some extension methods: I wasn’t allowed to modify the original type, and I also couldn’t subclass it, either. Extension methods to the rescue!

One thing which made the situation slightly interesting though, was that the original type also had a whole bunch of derived types – and for each of them, I wanted to customise the relevant extension method! Well, that’s all well and good, but what I really wanted to know was: lets say I was operating on a collection of the base types (where each individual item was an instance of a derived type), which extension method would get invoked?

Only one way to find out – write some code! For simplicity, I used the base class Animal, and added two derived types: Dog and Elephant.

    public abstract class Animal
    {
        public string Name { get; set; }
        public bool HasBoyBits { get; set; }
        public abstract string Type { get; }
    }

    public class Dog : Animal
    {
        public string Breed { get; set; }

        public override string Type
        {
            get { return "Dog"; }
        }
    }

    public class Elephant : Animal
    {
        public bool HasTusks { get; set; }

        public override string Type
        {
            get { return "Elephant"; }
        }
    }

Now for the extension method: CanBreed. For the base type Animal, the rule was simple – the animals had to be of the same type, and of the opposite sex:

    public static class AnimalExtensions
    {
        public static bool CanBreedWith(this Animal animal, Animal other)
        {
            return animal.Type == other.Type
                && animal.HasBoyBits != other.HasBoyBits;
        }
    }

For dogs, we only want to breed with dogs that are already of the same pedigree; and for elephants, we only want to breed with other elephants that have the same tusk status (yes, it’s pretty ambiguous, but whatever):

    public static class DogExtensions
    {
        public static bool CanBreedWith(this Dog dog, Animal other)
        {
            return AnimalExtensions.CanBreedWith(dog, other)
                && ((Dog) other).Breed == dog.Breed;
        }
    }

    public static class ElephantExtensions
    {
        public static bool CanBreedWith(this Elephant elephant, Animal other)
        {
            return AnimalExtensions.CanBreedWith(elephant, other)
                && ((Elephant) other).HasTusks == elephant.HasTusks;
        }
    }

Finally, the test data: two lists of animals, separated by gender:

    var baxter = new Dog { Name = "Baxter", HasBoyBits = true, Breed = "Jack Russell" };
    var missy = new Dog { Name = "Missy", HasBoyBits = false, Breed = "Jack Russell" };
    var jazzy = new Dog { Name = "Jazzy", HasBoyBits = false, Breed = "Poodle" };

    List<Animal> boys = new List<Animal>
    {
        baxter,
        new Elephant { Name = "Lance", HasBoyBits = true, HasTusks = true }
    };

    List<Animal> girls = new List<Animal>
    {
        missy,
        jazzy,
        new Elephant { Name = "Renee", HasBoyBits = false, HasTusks = true },
        new Elephant { Name = "Hephalump", HasBoyBits = false, HasTusks = false }
    };

Now, what I had to test was if .NET would use the Runtime type of each animal to work out which extension method to invoke. I was pretty confident that both of these would pass:

    Assert.IsTrue(baxter.CanBreedWith(missy));
    Assert.IsFalse(baxter.CanBreedWith(jazzy));

And sure enough, the unit test succeeds with flying colours. However, to make things slightly trickier, what if we do this?

    foreach (var boy in boys)
    {
        foreach (var girl in girls)
        {
            bool canBreed = boy.CanBreedWith(girl);

            Console.WriteLine("{0} can{2} breed with {1}",
                boy.Name, girl.Name, canBreed ? String.Empty : "'t");
        }
    }

Unfortunately, here’s what the output looks like:

Baxter can breed with Missy
Baxter can breed with Jazzy
Baxter can’t breed with Renee
Baxter can’t breed with Hephalump
Lance can’t breed with Missy
Lance can’t breed with Jazzy
Lance can breed with Renee
Lance can breed with Hephalump

So we can see that the animals aren’t allowed to breed when they’re of differing types (hence AnimalExtensions.CanBreedWith is being invoked); however, each type of animal is allowed to breed with any other animal of the same type. No joy! (Well, joy for the animals I suppose…)

On retrospect, this is pretty much the behavior that I expected, but I was still disappointed to find out that I had been right. Worse still, once the sample code was up and running, I thought, “Hey, I should have used Rabbits instead of Elephants, and then always returned false in the CanBreed method, because that would have been funnier”. Ahh, hindsight, where were you before?

Oh well. Time for some refactoring!

Posted in .Net, C# at July 25th, 2011. 1 Comment.

Fixing debugging displays with ToString()

Debugging can be tricky when you’re trying to inspect a collection of objects that superficially all look the same! But there’s a few things you can do to alleviate that problem, and a few gotchas to try and avoid, too.

Lets say you have a simple class called TimeRange, which defines a start time and an end time, and also has a calculated property for the length of time it represents. Your class might look something like this:

public class TimeRange
{
    public TimeSpan Start { get; set; }
    public TimeSpan End { get; set; }

    public TimeSpan Length
    {
        get { return End.Subtract(Start); }
    }
}

Now, lets say you create a list of these Time Ranges. When you debug your file, inspecting the list will give you a picture like this:

Unhelpful time ranges

That’s not very helpful now, is it?! It would be much easier to work out what was going on if the debugger showed us a little bit about each time range, rather than simply the object type’s full name.

Thankfully, it’s actually pretty easy to customise what the debugger displays during object inspection using one of two methods:

  • The “hard” way: Decorate your class with the DebuggerDisplayAttribute (which you can read about here). Essentially, this allows you to write custom text, and/or an expression which will be evaluated by the debugger when inspecting an object.
  • The “easy” way: Override the ToString() method! This is what the debugger uses by default to display information about your object.

(Note: If you override the ToString() method as well as decorate your class with the DebuggerDisplayAttribute, the attribute will take precedence.)

So, with that in mind, lets override ToString() to fix our debugging display:

override public string ToString()
{
    return String.Format("{0:hh:mm} - {1:hh:mm} (Length: {2:h}h {2:mm}m)",
        Start, End, Length);
}

And now lets run our debugger and check out… hey, wait a minute! Our debugging display hasn’t changed! What’s going on? I thought overriding ToString() would change what the debugger shows when inspecting our objects! Lets use a hammer to crack a nut, and add in the DebuggerDisplayAttribute as well:

[DebuggerDisplay("{ToString()}")]
public class TimeRange

And now lets see what we get:

Slightly more helpful...

Ah ha! We have a problem with our String.Format() statement – it turns out the formatting strings that we’ve used for the TimeSpan instances are incorrect. So, lets fix our formatting string, and remove the DebuggerDisplay attribute. Here’s how our class looks now:

public class TimeRange
{
    public TimeSpan Start { get; set; }
    public TimeSpan End { get; set; }

    public TimeSpan Length
    {
        get { return End.Subtract(Start); }
    }

    override public string ToString()
    {
        return String.Format(@"{0:hh\:mm} - {1:hh\:mm} (Length: {2:%h}h {2:mm}m)",
            Start, End, Length);
    }
}

And here’s what we see when we inspect a list of Time Ranges in the debugger:

Much better!

Much better!

Posted in C# at July 19th, 2011. 1 Comment.

Varchar, Nvarchar and Entity Framework

Further to my previous post about a bug with Entity Framework 1.0, I’ve have discovered another bug but this time due to its handling of varchars.

Let’s assume we’re using the following query:

var query = context.Orders.Where(o => o.Customer.Address.Country == "Australia");

If our Country column is a varchar Entity Framework will pass through our “Australia” string as an nvarchar parameter regardless of what we define in our SSDL!
Now has is this a bad thing you may ask? Well what this means is that even if we have an index on our country column, SQL Server will have to do an index scan rather than an index seek because it has to convert the nvarchar “Australia” down to a varchar and do the comparison across every row in the table.

Thankfully there is a workaround .

This problem has also been addressed in EF 4.0 as well.

Posted in .Net, C#, Sql Server at September 23rd, 2010. No Comments.

DateTime Localization in ASP .NET

You’ve got an ASP .NET app and you need to display a date. Normally that just involves the old DateTime.ToShortDateString() or DateTime.ToString(“d”) and you continue coding on your merry little way. But what happens when your users are from different countries and expect different date formats.  Between Australia, USA and Japan there are 3 different date formats.

Then answer? The HttpRequest’s UserLanguages. This string array basically maps to the following languages that you can setup in your browser:

Now for the code:


CultureInfo culture = Request.UserLanguages != null
? CultureInfo.CreateSpecificCulture(Request.UserLanguages[0])
: CultureInfo.CurrentCulture;
myLabel.Text = DateTimeOffset.Now.ToLocalTime().ToString("d", culture);
Don’t forget to check if the UserLanguages is not null since you can remove all the values from the list if you like.
Also, if you are looking at ways to do this client side, ScottGu’s Blog has a nice post on a new jquery plugin for this.

* Update *
Just realised that you don’t need to any of this if you use the following in your web.config:

<globalization uiCulture="auto" culture="auto" />
Posted in .Net, Asp.Net, C# at June 18th, 2010. No Comments.

Parsing comma separated values with regular expressions

We’ve all had to write code to parse comma separated values before; it sounds simple, but it can actually be quite tricky! Sure, if our lists were always nicely defined like this:

  • “one”,”two”,”three”,”four”
  • five,six,seven,eight

Then we could simply use String.Split. But life is never that kind! When your input strings may be a bit more loosely defined, like this:

  • one,”two,three”,four,,six,”seven”
  • ,two,”three,four,five”,,

It gets a little tougher.

So can you do it using a single regular expression? Yes, you most certainly can! It’s simply a matter of breaking down the possibilities, then catering for the best case scenario (quoted values), down to the worse case scenario (zero-length values), and finally, catering for the delimiters (either a comma, or the end of the string). Lets look at them one step at a time.

Firstly, quoted values. This is by far the easiest of all the conditions – find any length of text between two quotes. We’ll use a non-greedy expression (the question mark after the star) to ensure we don’t over-extend the length of text that we match:

private const string
    Template_QuotedValues = @"""(?<content>.*?)""";

The next easiest type of match to capture are non-quoted, non-zero length values. To do this, we’ll simply look for one or more characters which are not a comma. Again, we’re using a non-greedy match:

private const string
    Template_UnquotedValues = @"(?<content>[^,]+?)";

Notice also that that for both templates, we’re creating a named group called “content” – this allows us to easily extract the contents the match, no matter what conditions were matched under.

The last type of match we need to cater for is non-quoted, zero-length matches. This is the trickiest of the three situations, since there’s “nothing” to actually match on! So instead, we look zero repetitions of any character, immediately after a delimiter. Since the first value in the list may be empty, the possible values for our delimiter are either the start of string (specified by the hat – ^), or a comma:

private const string
    Template_EmptyValues = @"(?<=(?:,|^))(?<content>.{0})",

The final piece of the puzzle is the delimiters. Since we’re matching from left-to-right, we can assume that every match will be followed either by a comma, or the end of the string. We’ll use a non-capturing group since we don’t want the delimiter to be explicitly captured in a group.

private const string
    Template_Delimiter = @"(?=(?:,|$))";

Now, to put it all together. We have our three types of matches that we’re expecting, and our delimiter, so all we need to do is create a single RegEx for it all. Here goes:

private const string
    // Any length value within quotes...
    Template_QuotedValues = @"""(?<content>.*?)""",

    // ... or values with at least 1 character, not in quotes...
    Template_UnquotedValues = @"(?<content>[^,]+?)",

    // ...or zero-length matches, not in quotes...
    Template_EmptyValues = @"(?<=(?:,|^))(?<content>.{0})",

    // ... followed either a comma, or end of string
    Template_Delimiter = @"(?=(?:,|$))";

// Now join as one Template - notice the OR condition (pipe)
// between the three match types
readonly static private string
    Template = String.Format("({0}|{1}|{2}){3}",
        Template_QuotedValues,
        Template_UnquotedValues,
        Template_EmptyValues,
        Template_Delimiter);

// Finally, our RegEx!
readonly static private Regex CsvSplitterRegex
    = new Regex(Template, RegexOptions.Compiled);

Was that so bad? ;-) Iterating through the list of values in our comma separated list is now a piece of cake.

// Assume CSV is in "record" field
foreach (Match match in CsvSplitterRegex.Matches(record))
{
    Console.WriteLine("Match value: {0}",
        match.Groups["content"].Value);
}

Simple, eh?

Posted in C#, RegEx at May 15th, 2010. 4 Comments.

Handling of Nulls in Entity Framework

I think most of you can agree that we are advocates of Entity Framework here. But sometimes things can get a little hairy (as with all ORM solutions). So I just thought I’d post a quick solution to a problem that had me tearing my hair out at work to solve the other day.

Given the following two queries using Entity Framework one would assume the generated sql would the same?

string country = null;
var query = context.Orders.Where(o => o.Customer.Address.Country == country);
var query2 = context.Orders.Where(o => o.Customer.Address.Country == null);
Wrong. Thanks to this bug and this Microsoft article, I found out that not only will Entity Framwork will not honour what I have specified for ANSI NULLs, it will result in two inconsistent sql queries as the first query will do a “@country = null” comparison while the second query will do a “Country IS NULL” comparison.
Now, while I get that just because two objects are null it doesn’t mean they are equal, the workaround here can result in some ugly looking linq queries! Thank you ever so much Microsoft :)
Posted in .Net, C#, Sql Server at April 24th, 2010. 2 Comments.

Path resolution in ASP.NET

One thing I’ve always thought that ASP.NET doesn’t do a great job of is handling resource paths. For example, lets say on your development box, you have a website hosted under the virtual directory “MyProject”, such that you browse to http://localhost/MyProject/ to go to the home page. Lets also say that when you deploy the website to an external domain, it falls into the domain root, so instead you go to http://myproject.com/ to see your home page.

Sound familiar? The real problem then comes when you try and define a resource (e.g. a javascript file) using a relative path:

<script
    type="text/javascript"
    src="/Scripts/jquery.js">

This will work fine and dandy when you deploy the website, however on your local machine it will be looking for jquery at the path http://localhost/Scripts/jquery.js” – which more than likely isn’t going to work.

To get around this problem, I wrote a quick helper method called “Locate”, which I find myself reusing time and time again between websites. More often than not, I put it into a static class called “SiteManager” which basically just contains a bunch of helper functions relevant to the current website. Here’s the method:

/// <summary>
/// Resolves the path to a URL
/// </summary>
/// <param name="url">The path to be resolved, relative to the
/// application root</param>
/// <param name="formatArgs">String formatting arguments</param>
/// <returns>The URL</returns>
static public string Locate(string url, params object[] formatArgs)
{
    if (String.IsNullOrEmpty(url))
        url = "/";

    if (formatArgs != null && formatArgs.Length > 0)
        url = String.Format(url, formatArgs);

    HttpContext context = HttpContext.Current;
    return String.Concat(
        context.Request.ApplicationPath,
        !url.StartsWith("/") ? "/" : String.Empty,
        url);
}

To use the method, you simply change your script declaration (or whatever resource you’re trying to find) as follows:

<script
    type="text/javascript"
    src="<%= SiteManager.Locate("/Scripts/jquery.js") %>">
</script>

You’ll also notice that it supports string formatting – so even though it would be pointless in this particular example, you could, if you preferred, do something like this:

<script
    type="text/javascript"
    src="<%= SiteManager.Locate("/Scripts/{0}.js", "jquery") %>">
</script>

In both instances, this will resolve the URL in your output document using the current application path –

  • http://localhost/MyProject/Scripts/jquery.js on your local machine; and
  • http://myproject.com/Scripts/jquery.js on the web server.

Enjoy!

Posted in Asp.Net, C# at January 14th, 2010. 5 Comments.

Quick WPF Performance Statistics

Just thought I’d put out some quick WPF performance statistics thanks to Microsoft. You can download the entire slideshow here.
  • DependencyProperty is x3 faster than INotifyPropertyChanged
  • ObservableCollection accesses single items 90 times faster than List
  • ObjectDataProvider is x20 smaller than XmlDataProvider

Microsoft also have some other good tips over here too.
I’d like to see some real world statistics on the differences between using static vs dynamic resources too. Anybody know of any?

Posted in .Net, C# at December 23rd, 2009. 1 Comment.

Question of the day: Enums

Some fun with enums! Assume you have an enum defined as follows:

enum Frequency {
    None = 0,
    Annual = 1,
    SemiAnnual = 2,
    Quarterly = 4,
    Monthly = 12,
    Weekly = 52
}

Now, given the following block of code:

Frequency myFrequency = (Frequency) 104;
System.Console.WriteLine("My frequency is: {0}", myFrequency.ToString());
bool is100Defined = Enum.IsDefined(typeof(Frequency), 100);
bool is52Defined  = Enum.IsDefined(typeof(Frequency), 52);
  1. What is written to the console?
  2. What is the value of is100Defined?
  3. What is the value of is52Defined?

First correct answer gets absolutely nothing (but think how great you’ll feel!)

Posted in C#, Daily Question at October 7th, 2009. 2 Comments.
Quickduck logo