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

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

Danish CPR Numbers

I was reading several articles about the new NemID system in Denmark, and concerns around how organized groups might generate valid CPR numbers and brute-force passwords. This could lead to account lockouts—potentially paralyzing major parts of the country’s digital infrastructure. To test this, I created code to generate valid CPR numbers based on known rules. The Rules The original rules were based on a textbook I no longer have, but I found newer ones via this blog post, official article, and PDF document from the CPR register. ...

August 3, 2010 · 3 min · Michael Bisbjerg