using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
public class CustomDictionaryConverter : JsonConverter
sealed class InconvertibleDictionary : Dictionary<object, object>
public InconvertibleDictionary(DictionaryEntry entry)
this[entry.Key] = entry.Value;
public override bool CanConvert(Type objectType)
return typeof(IDictionary).IsAssignableFrom(objectType) && objectType != typeof(InconvertibleDictionary);
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
serializer.Serialize(writer, Entries(((IDictionary)value)).Select(p => new InconvertibleDictionary(p)));
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
if (reader.MoveToContentAndAssert().TokenType == JsonToken.Null)
var dictionary = existingValue ?? serializer.ContractResolver.ResolveContract(objectType).DefaultCreator();
switch (reader.TokenType)
case JsonToken.StartObject:
serializer.Populate(reader, dictionary);
case JsonToken.StartArray:
switch (reader.ReadToContentAndAssert().TokenType)
case JsonToken.StartObject:
serializer.Populate(reader, dictionary);
throw new JsonSerializationException(string.Format("Unexpected token {0}", reader.TokenType));
throw new JsonSerializationException(string.Format("Unexpected token {0}", reader.TokenType));
static IEnumerable<DictionaryEntry> Entries(IDictionary dict)
foreach (DictionaryEntry entry in dict)
public static partial class JsonExtensions
public static JsonReader ReadToContentAndAssert(this JsonReader reader)
return reader.ReadAndAssert().MoveToContentAndAssert();
public static JsonReader MoveToContentAndAssert(this JsonReader reader)
throw new ArgumentNullException();
if (reader.TokenType == JsonToken.None)
while (reader.TokenType == JsonToken.Comment)
public static JsonReader ReadAndAssert(this JsonReader reader)
throw new ArgumentNullException();
throw new JsonReaderException("Unexpected end of JSON stream.");
public enum DeploymentEnvironment { DEV = 1, TST = 2 }
public enum TargetApplication { Apollo = 1, Gemini = 2 }
public enum DbKeyType { DmkPassword = 1, SymmetricKeySource = 2 }
public class DeploymentSettings
[JsonProperty("defaultSettings")]
public DefaultSettings DefaultSettings { get; set; }
public DeploymentSettings()
DefaultSettings = new DefaultSettings();
public partial class DefaultSettings
[JsonProperty("applications")]
public Dictionary<TargetApplication, ApplicationContainer> Applications { get; set; }
Applications = new Dictionary<TargetApplication, ApplicationContainer>();
public partial class ApplicationContainer
[JsonProperty("environments")]
public Dictionary<DeploymentEnvironment, EnvironmentContainer> Environments { get; set; }
public ApplicationContainer()
Environments = new Dictionary<DeploymentEnvironment, EnvironmentContainer>();
public partial class EnvironmentContainer
[JsonProperty("dbKeyTypes")]
public Dictionary<DbKeyType, string> DbKeyTypes { get; set; }
public EnvironmentContainer()
DbKeyTypes = new Dictionary<DbKeyType, string>();
public static void Test()
var settings = new JsonSerializerSettings
Converters = { new CustomDictionaryConverter(), new StringEnumConverter() }
var ds = JsonConvert.DeserializeObject<DeploymentSettings>(json, settings);
var json2 = JsonConvert.SerializeObject(ds, Formatting.Indented, settings);
Console.WriteLine("Re-serialized {0}:", ds);
Console.WriteLine(json2);
Assert.IsTrue(JToken.DeepEquals(JToken.Parse(json), JToken.Parse(json2)));
""DmkPassword"": ""AEikOooIuGxXC9UBJQ3ckDj7Q126tB06""
""SymmetricKeySource"": ""bTU7XOAYA2FFifmiBUggu99yHxX3Ftds""
""DmkPassword"": ""AEikOooIuGxXC9UBJQ3ckDj7Q126tB06""
""SymmetricKeySource"": ""bTU7XOAYA2FFifmiBUggu99yHxX3Ftds""
""DmkPassword"": ""AEikOooIuGxXC9UBJQ3ckDj7Q126tB06""
""SymmetricKeySource"": ""bTU7XOAYA2FFifmiBUggu99yHxX3Ftds""
""DmkPassword"": ""AEikOooIuGxXC9UBJQ3ckDj7Q126tB06""
""SymmetricKeySource"": ""bTU7XOAYA2FFifmiBUggu99yHxX3Ftds""
public static void Main()
Console.WriteLine("Environment version: " + Environment.Version);
Console.WriteLine("Json.NET version: " + typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine("Failed with unhandled exception: ");