marcoCasamento / Hangfire.Redis.StackExchange

HangFire Redis storage based on original (and now unsupported) Hangfire.Redis but using lovely StackExchange.Redis client
Other
456 stars 109 forks source link

Case Sensitive Dictionary Json Settings #18

Closed VictorTomaili closed 8 years ago

VictorTomaili commented 8 years ago

I use case insensitive json settings for dictionaries and i set it on my owin startup new KeyValuePairConverter()

JsonConvert.DefaultSettings = () => JsonSettings.Tolerant;
public static readonly JsonSerializerSettings Tolerant = new JsonSerializerSettings
        {
            Formatting = Formatting.Indented,
            NullValueHandling = NullValueHandling.Ignore,
            MissingMemberHandling = MissingMemberHandling.Ignore,
            TypeNameHandling = TypeNameHandling.None,
            ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
            PreserveReferencesHandling = PreserveReferencesHandling.None,
            ContractResolver = new CamelCasePropertyNamesContractResolver(),
            Converters = new List<JsonConverter>{
                    new IsoDateTimeConverter(),
                    new KeyValuePairConverter()
                }
        };

And i have exception when click the details on dashboard. When dictionary deserialized, its contains lowercase keys and throw key not found exception.

        public JobDetailsDto JobDetails(string jobId)
        {
            return UseConnection(redis =>
            {
                var job = redis.HashGetAll(String.Format(RedisStorage.Prefix + "job:{0}", jobId)).ToStringDictionary();
                if (job.Count == 0) return null;

                var hiddenProperties = new[] { "Type", "Method", "ParameterTypes", "Arguments", "State", "CreatedAt" };

                var historyList = redis.ListRange(String.Format(RedisStorage.Prefix + "job:{0}:history", jobId))
                    .Select(x=> (string)x).ToList();

                var history = historyList
                    .Select(JobHelper.FromJson<Dictionary<string, string>>)
                    .ToList();

                var stateHistory = new List<StateHistoryDto>(history.Count);
                foreach (var entry in history)
                {

                   // Dictionary give key not found also its contain lowercase keys
                    var dto = new StateHistoryDto
                    {
                        StateName = entry["State"],
                        Reason = entry.ContainsKey("Reason") ? entry["Reason"] : null,
                        CreatedAt = JobHelper.DeserializeDateTime(entry["CreatedAt"]),
                    };
....

I will think tommorrow someting. Good night.

VictorTomaili commented 8 years ago

I fix it via this resolver from stackowerflow. But i think its shouldn't be.

    public class CamelCaseExceptDictionaryKeysResolver : 
        CamelCasePropertyNamesContractResolver
    {
        protected override JsonDictionaryContract CreateDictionaryContract(Type objectType)
        {
            JsonDictionaryContract contract = base.CreateDictionaryContract(objectType);

            contract.DictionaryKeyResolver = propertyName => propertyName;

            return contract;
        }
    }

Edit: Ok, i try to fix it from this library but i got same exception from hangfire.