How to Build Custom JSON Using JSON.Net

The serializer from JSON.Net is really good, and the default output from JsonConvert.SerializeObject will work for most situations, but sometimes you want fine control over the structure of your JSON. Fortunately, JSON.Net also includes an API for building out custom JSON. You will need the Nuget package Newtonsoft.Json and add the namespace Newtonsoft.Json.Linq. To start, create a new JObject that will represent the root of the JSON. Call the Add method to add properties to the JSON....

August 3, 2017 · Joshua Marble

Convert a DataTable to a Markdown table

I am working on a project in which I want to export some tabular data in Markdown. I started writing methods to do this with each set of data, but that got to be tedious. I thought instead it would be easier to get the data into a DataTable then just have a generic method that converts a DataTable to Markdown. That idea became a new extension method that I thought I would share on gist....

January 16, 2017 · Joshua Marble

Migrating .Net Core Preview 2 to Preview 3

If you have any .Net Core Preview 2 applications that use project.json, you will encounter errors if you try to update to .Net Core Preview 3. This is due to the fact that .Net Core is moving away from project.json and will use the normal .csproj files going forward. For more information on that change, see this blog post by the .Net Core engineering team. Fortunately, the new dotnet command line utility supports migrating from project....

December 8, 2016 · Joshua Marble

ConsoleColorContext - Give Some Color to Your Console Applications

I previously wrote about the Context Pattern where I used an example of temporarily turning off an event. I was recently writing a console application and came up with another use of the Context Pattern that I thought I would share. Often in a console application, the need arises to output some text in a different color, perhaps for displaying errors and warnings, or maybe just to give the user some extra visual feedback....

December 7, 2016 · Joshua Marble

NuGet Package: Costura.Fody

I was writing a small tool recently and wanted to give it to some people in the office to use, but I wanted it to be simple. I was going to put it out on a network share, but I didn’t like how I needed a folder with all the dependencies, which inevitably raises questions of which file to run. I thought it would be nice to just bundle it all up into a single executable....

October 19, 2016 · Joshua Marble

TimeSpanInterval - Natural Language TimeSpans

Have you ever wanted to give your users some predefined choices for a time span (minutes, hours, etc)? That’s a pretty natural thing to do using language, but not such a natural thing to do using the TimeSpan class in .Net. You could write your own methods to convert from those options into an actual TimeSpan, but you’d likely be using switch statements to tie together the idea of a unit of time to the TimeSpan class....

September 2, 2016 · Joshua Marble

NuGet Package: FluentValidation

I came across a pretty neat nuget package that may very well be my go-to validation logic from now on. The package is called FluentValidation and seems to be a very flexible and usable fluent approach to object validation. There’s not much else to say about it other than go check it out and give it a spin. There is a quick example on the github page, but the api is very discoverable....

May 9, 2016 · Joshua Marble

DataReader Extension GetNullable

It has always been a minor pain that fields in a database can be null while the same value type in .Net cannot be. If you wanted to read a nullable DateTime field in the database into your object’s DateTime property, the only choice was to check it for null first then read the field if not null. if (!dr.IsDBNull(3)) obj.Date = dr.GetDateTime(3); I like to use extension methods to prevent the need to lookup ordinals every time I read from the data reader....

March 10, 2016 · Joshua Marble

Fixed: Visual Studio Fails to Start

I have had a recurring problem on my PC for a while where after some period of time Visual Studio will fail to start and show a generic error message box. I have multiple versions of Visual Studio and all would fail to load, none telling me exactly what the problem was. I experienced a similar problem trying to start SQL Server Management Studio. Every time this happened, the only thing that could fix it was a reboot....

October 30, 2015 · Joshua Marble

Concurrent Operations with Reactive Extensions

Today I encountered a scenario which I could describe with words much better than I could describe with code. I was reading results from a REST API, reading a few thousand records over multiple requests. I was saving these records to a database and didn’t want to wait until all records were pulled before saving, so I implemented some LINQ magic to enumerate the items in between each HTTP request....

October 26, 2015 · Joshua Marble