Optional Types and JSON Deserialization for C# for Brixen

Every small victory shall be rewarded with the next battle. Now that I have completed the task of doing the C# implementations of all the state beans in Brixen, I am planning the translations of the configuration beans. Preliminary research indicates that the closest equivalent to Java’s Optional type is the Nullable struct. And there is a tool for marshalling JSON data into object instances: Json.NET. Once I had established those facts, I had to tackle the same issue I had when approaching this problem in Java, which is: How do I distinguish between a missing key an an explicitly null value? And I determined that there is no built in way to do this and will require work on my part to make it happen. It also led to a funny Stack Overflow post where someone posted their implementation with the caveat:

I uploaded the implementation into a repository on Github, for everyone to use (no warranty, use at your own risk, etc… I’m in hospital with 6 broken ribs and under painkillers: if something doesn’t work it’s YOUR fault):

https://github.com/alberto-chiesa/SettableJsonProperties

So, I will most likely be following the guidance of someone who was under the influence of strong painkillers. I expect this will be an epic journey involving a lot more coding than was necessary to pull off the configuration beans in Java.

Share This:

0 Comments

Leave a Reply

Your email address will not be published.