Understanding the Newtonsoft JSON Token

One of the concepts worth understanding when writing a custom converter for the popular Newtonsoft JSON framework is that of the JsonToken, and the role it plays in helping you understand the JSON being read. The JsonToken documentation for Newtonsoft is relatively sparse - in part because once you understand it, it’s a relatively simple concept. Effectively, when you’re attempting to deserialise a string representation of a JSON object, such as in the ReadJson method of a custom JsonConverter, the JsonToken represents each element or component (token if you prefer) of the object as it’s deserialised....

March 26, 2019 · 2 min

Throw or throw ex in C#?

One common question that comes up when working with exceptions in C#, is whether or not you should re-throw an existing exception by specifying the exception, or just using the throw keyword where an error occurs. In most cases, it’s best to rethrow the existing exception, such as below: 1 try { var networkResponse = await someNetworkService.GetAllPosts();}catch (Exception ex){ Log.Warning("An exception occurred", ex); //Now lets rethrow this same exception throw;} By doing so, you ensure that the original stack trace for the exception is maintained....

March 20, 2019 · 1 min