Detecting mismatched objects with Newtonsoft.Json

A common issue in API client libraries is mismatches between the JSON response and the expected strongly typed objects. I encountered this with TMDbLib and created a validator to catch these mismatches during unit testing. The Settings We can set up a JsonSerializerSettings object to: Treat missing properties as errors. Use a custom ContractResolver to enforce that all expected properties must exist in the JSON. Handle and log deserialization errors without throwing immediately. private static readonly List<ErrorEventArgs> Errors = new List<ErrorEventArgs>(); public static void Main() { JsonSerializerSettings settings = new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Error, ContractResolver = new FailingContractResolver(), Error = Error }; JsonSerializer serializer = JsonSerializer.Create(settings); } private static void Error(object sender, ErrorEventArgs errorEventArgs) { Errors.Add(errorEventArgs); errorEventArgs.ErrorContext.Handled = true; } This configuration makes sure that: ...

August 10, 2016 · 2 min · Michael Bisbjerg

Bencoding Library–Part two

Previously, I described a Bencoding Library I made to simplify working with the Bencoding structure in .NET. I also published it on Codeplex. However, I discovered a flaw—if a string contains a null-byte (as most .torrent files do), it will fail. So I made a fix, which has been uploaded to Codeplex as changeset 1102. As can be seen in the changeset, a few files have been changed. Ignoring all the test files, the changes were made to: ...

August 13, 2010 · 1 min · Michael Bisbjerg

LINQ vs. Foreach

I asked around while trying to learn how to use LINQ. LINQ is essentially SQL for .NET—it allows you to query objects directly in memory. For example: List<int> test = new List<int> { 1, 3, 5 }; List<int> testnew = (from a in test where a == 5 select a).ToList(); Someone claimed LINQ was faster than using foreach for selecting items. So, I tested it. The Benchmark static List<int> testlist = new List<int>(); static void Main(string[] args) { DateTime b, a; List<double> regularTimes = new List<double>(); List<double> LINQTimes = new List<double>(); for (int x = 0; x < 1000000; x++) testlist.Add(x * 5); // Foreach loop test for (int x = 0; x < 1000; x++) { b = DateTime.Now; TestRegular(); a = DateTime.Now; regularTimes.Add((a - b).TotalMilliseconds); } Console.WriteLine("Regular Avg.: {0}", regularTimes.Average()); // LINQ test for (int x = 0; x < 1000; x++) { b = DateTime.Now; TestLINQ(); a = DateTime.Now; LINQTimes.Add((a - b).TotalMilliseconds); } Console.WriteLine("LINQ Avg.: {0}", LINQTimes.Average()); Console.ReadLine(); } private static void TestRegular() { List<int> newlist = new List<int>(); foreach (int item in testlist) if (item % 2000 == 0) newlist.Add(item); } private static void TestLINQ() { var newlist = (from a in testlist where a % 2000 == 0 select a).ToList(); } This runs each selection method 1000 times on a list of 1,000,000 items. ...

August 10, 2010 · 2 min · Michael Bisbjerg

PDF Search Site

An Introduction So, I’m moving into an apartment in the near future. Following that, I will have to learn how to manage my documents and ensure I can find them back in time. I’ve thought of a few solutions, one of them being what I do now – where I simply have a folder with PDFs of my documents (I digitalize everything). This works fine now… (I have like … 20 documents)… But it may not work in 2 years when I have a hundred documents spread over many different corporations and years. ...

August 10, 2010 · 4 min · Michael Bisbjerg

Bencoding – A C# Library

I’ve long wanted to write a Bencoding library. Bencoding is an encoding format for encoding objects like text, lists, dictionaries, etc., into a single piece of text. It’s often used for transporting configuration files. Bencoding is most famous for its use in the Bittorrent protocol—it forms the basis for .torrent files. I wanted to create a C# library to support all known Bencoding object types. You can read more about the encoding here. ...

August 9, 2010 · 3 min · Michael Bisbjerg