Wednesday, May 13, 2015

C# Using JSON in Web.Config (or any config) file for static object store

This is a short article on how to access static data using JSON in a config file. Hope you find it useful in your project.

I'm going to show you how I use static data to store things like commands, or anything that remains unchanged. This project is based on the Automotive CAN Bus, where there are many messages you can query, thousands, actually. By adding the ones you're interested in, into a static file, you can chose which messages you want to display to the user.

1. Create Your Static JSON Data in your added config file, this one is called CanReceive.Config.








2. Add reference to the added config file to your main config file. Here, Under configSections, section add value for canReceive and then add a section called canReceive with the filename of your configSource.




3. Create a POCO class to represent your JSON Data.


4. Create a Static Config Class to read the data from the config file as a JSON String. CanReceive is the one we are interested in here.
















5. Create a Dictionary Class to store your serialized JSON objects and an ID to get them.




















5. Use your dictionary in Code by calling the methods:

A. CanReceiveSingleton.Instance.GetMapByIdx(0);



There it is, I find this useful in a number of scenarios where I need a way to access a list of data for further processing.

Jonathan