Saturday, December 26, 2020

Implicit Conversion of .NET Objects and Json Converstion from String to .NET Object

Implicit Conversion of .NET Objects and Json Converstion from String to .NET Object

Using this method you can assign object to a string value which is the default value depending on environment vairable, and you can converrt string to a object using JSON serialization.

This instrumention is ideal for JSON configuration where it automatically resolve the DEV/UAT/PROD values so that you do not have to worry about configuration changes over multiple files while deployment

Config Value Class


using Newtonsoft.Json;using System;
using System.Collections.Generic;
namespace ImplicitConversion
{
    [JsonConverter(typeof(ConfigValueConverter))]
    class ConfigValue : Dictionary<string, string>
    {
        readonly string devKey = "DEV", uatKey = "UAT", prodKey = "PROD";
        public string DEV
        {
            get
            {
                if (ContainsKey(devKey)) return this[devKey];
                return default;
            }
            set
            {
                if (ContainsKey(devKey)) this[devKey] = value;
                else Add(devKey, value);
            }
        }
        public string UAT
        {
            get
            {
                if (ContainsKey(uatKey)) return this[uatKey];
                return default;
            }
            set
            {
                if (ContainsKey(uatKey)) this[uatKey] = value;
                else Add(uatKey, value);
            }
        }
        public string PROD
        {
            get
            {
                if (ContainsKey(prodKey)) return this[prodKey];
                return default;
            }
            set
            {
                if (ContainsKey(prodKey)) this[prodKey] = value;
                else Add(prodKey, value);
            }
        }
        public string Value
        {
            get
            {
                if (Environment.UserName.StartsWith("prod"))
                    return PROD;
                if (Environment.UserName.StartsWith("uat"))
                    return UAT;
                return DEV;
            }
            set
            {
                DEV = UAT = PROD = value;
            }
        }
        public static implicit operator string(ConfigValue value)
        {
            return value.Value;
        }
    }
}

Config Value Converter Class

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;

namespace ImplicitConversion
{
    class ConfigValueConverter : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            throw new NotImplementedException();
        }
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            ConfigValue value = new ConfigValue();
            try
            {
                JObject jObject = JObject.Load(reader);
                serializer.Populate(jObject.CreateReader(), value);
                return value;
            }
            catch
            {
                value.Value = reader.Value?.ToString();
                return value;
            }
        }
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            throw new NotImplementedException();
        }
    }
}

Simpile Refernece Class for JSON Serialization

namespace ImplicitConversion
{
    class Configuration
    {
        public ConfigValue Email { get; set; }
    }
}

Program Class to Test

using Newtonsoft.Json;
using System;

namespace ImplicitConversion
{
    class Program
    {
        static void Main(string[] args)
        {
            string json = @"{ 
                ""Email"": ""no-reply@company.com"" 
            }";
            Configuration configuration = JsonConvert.DeserializeObject(json);
            Console.WriteLine($"DEV value : { configuration.Email.DEV }");
            Console.WriteLine($"UAT value : { configuration.Email.UAT }");
            Console.WriteLine($"PROD value : { configuration.Email.PROD }");

            json = @"{ 
                ""Email"": {
                    ""DEV"":""no-reply-DEV@company.com"",
                    ""UAT"":""no-reply-UAT@company.com"",
                    ""PROD"":""no-reply-PROD@company.com""
                }
            }";
            configuration = JsonConvert.DeserializeObject(json);
            Console.WriteLine($"DEV value : { configuration.Email.DEV }");
            Console.WriteLine($"UAT value : { configuration.Email.UAT }");
            Console.WriteLine($"PROD value : { configuration.Email.PROD }");

            string email = configuration.Email;
            Console.WriteLine($"DEV value : { email }");

            Console.ReadLine();
        }
    }
}

No comments:

Azure Storage Account Types

Defferent Types of Blobs Block blobs store text and binary data. Block blobs are made up of blocks of data that can be managed individually...