/*                private Dictionary<string, object> deserializeToDictionary(string jo)
 *              {
 *                  var values = JsonConvert.DeserializeObject<Dictionary<string, object>>(jo);
 *                  var values2 = new Dictionary<string, object>();
 *                  foreach (KeyValuePair<string, object> d in values)
 *                  {
 *                      if (d.Value.GetType().FullName.Contains("Newtonsoft.Json.Linq.JObject"))
 *                      {
 *                          values2.Add(d.Key, deserializeToDictionary(d.Value.ToString()));
 *                      }
 *                      else
 *                      {
 *                          values2.Add(d.Key, d.Value);
 *                      }
 *
 *                  }
 *                  return values2;
 *              } */

        public T DeserializeFromBundle <T>(string configuration, string bundleName, string jsonPath, IEnumerable <string> parameters)
        {
            var extraParams = parameters.Select(x => replaceParams(x)).ToArray();

            bundleName = replaceParams(bundleName, false);
            var configurationName = getConfigurationName(configuration);

            var bundle = m_Provider.GetBundle(configurationName, bundleName, extraParams);

            if (bundle == null)
            {
                throw new ConfigurationErrorsException(string.Format("bundle '{0}' not found", bundleName));
            }

            var token = JObject.Parse(bundle).SelectToken(jsonPath);

            if (token == null)
            {
                throw new ConfigurationErrorsException(string.Format("property path {0} in bundle '{1}' not found",
                                                                     jsonPath, bundleName));
            }

            /*   if(typeof(T)==typeof(Dictionary<string, object>) ||typeof(T)==typeof(IDictionary<string, object>)  )
             *     return (T)(object)deserializeToDictionary(token.ToString());*/
            return(JsonConvert.DeserializeObject <T>(token.ToString()));
        }
示例#2
0
        public string GetBundle(string configuration, string bundleName, params string[] extraParams)
        {
            string content;

            try
            {
                content = m_ExternalProvider.GetBundle(configuration, bundleName, extraParams);
            }catch (Exception e)
            {
                m_Logger.WarnFormat(e, "Failed to retrieve bundle '{0}' with extra params {1} from remote source. Using cached value.", bundleName, string.Join(",", extraParams.Select(p => "'" + p + "'").ToArray()));
                content = null;
            }

            if (content == null)
            {
                try
                {
                    content = m_FileSystemConfigurationProvider.GetBundle(configuration, bundleName, extraParams.ToArray());
                }
                catch (Exception e)
                {
                    m_Logger.ErrorFormat(e, "Failed to retrieve bundle '{0}' with extra params {1} from cache.", bundleName, string.Join(",", extraParams.Select(p => "'" + p + "'").ToArray()));
                    return(null);
                }

                if (content != null)
                {
                    m_Logger.InfoFormat("Bundle '{0}' with extra params {1}  was loaded from cache. Bundle Content:\r\n{2}", bundleName, string.Join(",", extraParams.Select(p => "'" + p + "'").ToArray()), content);
                }
                else
                {
                    m_Logger.WarnFormat("Bundle '{0}' with extra params {1}  was not found in cache.", bundleName, string.Join(",", extraParams.Select(p => "'" + p + "'").ToArray()));
                }
                return(content);
            }

            m_Logger.InfoFormat("Bundle '{0}' with extra params {1}  was received from remote source. Bundle Content:\r\n{2}", bundleName, string.Join(",", extraParams.Select(p => "'" + p + "'").ToArray()), content);

            try
            {
                m_FileSystemConfigurationProvider.StoreBundle(configuration, bundleName, extraParams, content);
            }catch (Exception e)
            {
                m_Logger.WarnFormat(e, "Failed to persist bundle '{0}' with extra params {1}", bundleName, string.Join(",", extraParams.Select(p => "'" + p + "'").ToArray()));
            }

            return(content);
        }