using Newtonsoft.Json.Linq;
using System.Collections.Generic;
public static class DictionaryExtensions
public static TValue SafeGet<TKey, TValue>(
this IDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue = default(TValue))
if (dictionary != null && key != null && dictionary.TryGetValue(key, out value_))
public static IEnumerable<TValue> SafeGet<TKey, TValue>(
this ILookup<TKey, TValue> lookup, TKey key)
if (lookup != null && key != null && lookup.Contains(key))
return Enumerable.Empty<TValue>();
public static Dictionary<TKey, TValue> ToDictionarySafe<TItem, TKey, TValue>(this IEnumerable<TItem> collection,
Func<TItem, TKey> keySelector, Func<TItem, TValue> valueSelector)
var result = new Dictionary<TKey, TValue>();
foreach (var item in collection)
var key = keySelector(item);
if (key == null) continue;
result[key] = valueSelector(item);
public class IdentityDocsSettings
public Dictionary<string, Dictionary<string,string>> this_;
public IdentityDocsSettings(string json)
var parsed = JObject.Parse(json);
this_ = parsed.ToObject<Dictionary<string, Dictionary<string,string>>>();
private IDictionary<string, Docs> _docsCache;
public string GetView(string identityType)
return _docsCache.SafeGet(identityType)?.View;
return _docsCache.SafeGet(identityType)?.View;
public string GetCountry(string identityType)
return _docsCache.SafeGet(identityType)?.Country;
return _docsCache.SafeGet(identityType)?.Country;
private void BuildCache()
_docsCache = this_.SelectMany(x => x.Value.Select(xx =>
public string Country { get; set; }
public string Type { get; set; }
public string View { get; set; }
public static void Main()
""passport_arm"": ""passport_arm_view"",
""passport_arm_stripped"": ""passport_arm_stripped_view"",
""transborder_passport"": ""transborder_passport_arm_view"",
""transborder_passport_stripped"": ""transborder_passport_stripped_arm_view""
""passport_aze"": ""passport_aze_view"",
""transborder_passport"": ""transborder_passport_aze_view"",
""transborder_passport_stripped"": ""transborder_passport_stripped_aze_view""
""passport_blr"": ""passport_blr_view"",
""passport_blr_stripped"": ""passport_blr_stripped_view"",
""permanent_residency_blr"": ""permanent_residency_blr_view"",
""refugee_cert_blr"": ""refugee_cert_blr_view"",
""transborder_passport"": ""transborder_passport_blr_view""
""id_kaz"": ""id_kaz_view"",
""id_kaz_stripped"": ""id_kaz_stripped_view"",
""passport_kaz"": ""passport_kaz_view"",
""passport_kaz_stripped"": ""passport_kaz_stripped_view"",
""permanent_residency_kaz"": ""permanent_residency_kaz_view"",
""transborder_passport"": ""transborder_passport_kaz_view"",
""transborder_passport_stripped"": ""transborder_passport_stripped_kaz_view""
""id_kgz"": ""id_kgz_view"",
""id_kgz_stripped"": ""id_kgz_stripped_view"",
""passport_kgz"": ""passport_kgz_view"",
""passport_kgz_stripped"": ""passport_kgz_stripped_view"",
""transborder_passport"": ""transborder_passport_kgz_view"",
""transborder_passport_stripped"": ""transborder_passport_stripped_kgz_view""
""id_mda"": ""id_mda_view"",
""passport_mda"": ""passport_mda_view"",
""transborder_passport"": ""transborder_passport_mda_view"",
""transborder_passport_stripped"": ""transborder_passport_stripped_mda_view""
""transborder_passport"": ""transborder_passport_view"",
""transborder_passport_stripped"": ""transborder_passport_stripped_view""
""military_id_card"": ""military_id_card_view"",
""passport_rus"": ""passport_view"",
""passport_rus_stripped"": ""passport_stripped_view"",
""permanent_residency"": ""permanent_residency_view"",
""refugee_cert"": ""refugee_cert_view"",
""residence_permit"": ""residence_permit_view"",
""sailor_rus"": ""sailor_view"",
""temporary_asylum_cert"": ""temporary_asylum_cert_view"",
""temporary_id_card"": ""temporary_id_card_view"",
""transborder_passport_stripped"": ""transborder_passport_stripped_rus_view""
""id_tjk"": ""id_tjk_view"",
""passport_tjk"": ""passport_tjk_view"",
""transborder_passport"": ""transborder_passport_tjk_view"",
""transborder_passport_stripped"": ""transborder_passport_stripped_tjk_view""
""id_ukr"": ""id_ukr_view"",
""passport_ukr"": ""passport_ukr_view"",
""transborder_passport"": ""transborder_passport_ukr_view"",
""transborder_passport_stripped"": ""transborder_passport_stripped_ukr_view""
""passport_uzb"": ""passport_uzb_view"",
""transborder_passport"": ""transborder_passport_uzb_view"",
""transborder_passport_stripped"": ""transborder_passport_stripped_uzb_view""
var identityDocsSettings = new IdentityDocsSettings(json);
Console.WriteLine(identityDocsSettings.GetCountry("passport_arm"));
Console.WriteLine(identityDocsSettings.GetCountry("military_id_card"));
Console.WriteLine(identityDocsSettings.GetCountry("permanent_residency_blr"));
Console.WriteLine(identityDocsSettings.GetCountry("transborder_passport"));
Console.WriteLine(identityDocsSettings.GetCountry("transborder_passport_stripped"));
public string Country { get; set; }
public string Type { get; set; }
public string View { get; set; }