Example #1
0
        private static JsonCompareResult NewJsonMatchesExistingJson(JsonSnapshotType type, Object new_obj, Uri existing_obj_uri)
        {
            var result = JsonCompareResult.different;

            try
            {
                if (BlobStorage.ExistsBlob(existing_obj_uri))
                {
                    if (type == JsonSnapshotType.DictStr)
                    {
                        var new_dict_str = (Dictionary <string, string>)new_obj;
                        if (new_dict_str.Keys.Count == 0)
                        {
                            return(JsonCompareResult.invalid);
                        }
                        var existing_dict_str = ObjectUtils.GetDictStrFromJsonUri(existing_obj_uri);
                        if (existing_dict_str.Keys.Count == 0)                         // this shouldn't happen, but...
                        {
                            return(JsonCompareResult.invalid);
                        }
                        var equal = ObjectUtils.DictStrEqualsDictStr((Dictionary <string, string>)existing_dict_str, new_dict_str);
                        result = equal ? JsonCompareResult.same : JsonCompareResult.different;
                    }
                    else                     // JsonSnapshotType.ListDictStr
                    {
                        var new_list_dict_str = (List <Dictionary <string, string> >)new_obj;
                        if (AllDictsHaveKeys(new_list_dict_str) == false)
                        {
                            return(JsonCompareResult.invalid);
                        }
                        var existing_list_dict_str = ObjectUtils.GetListDictStrFromJsonUri(existing_obj_uri);
                        if (AllDictsHaveKeys(existing_list_dict_str) == false)
                        {
                            return(JsonCompareResult.invalid);
                        }
                        var equal = ObjectUtils.ListDictStrEqualsListDictStr((List <Dictionary <string, string> >)existing_list_dict_str, new_list_dict_str);
                        result = equal ? JsonCompareResult.same : JsonCompareResult.different;
                    }
                }
            }
            catch (Exception e)
            {
                GenUtils.LogMsg("exception", "NewJsonMatchesExistingJson", e.Message + e.StackTrace);
            }

            return(result);
        }
Example #2
0
        public static bool SavedJsonSnapshot(string id, JsonSnapshotType type, string name, Object new_obj)
        {
            try
            {
                var json_blob_name           = id + "." + name + ".json";
                var existing_obj_uri         = BlobStorage.MakeAzureBlobUri(id, json_blob_name, false);
                var exists                   = BlobStorage.ExistsBlob(existing_obj_uri);
                JsonCompareResult comparison = NewJsonMatchesExistingJson(type, new_obj, existing_obj_uri);
                if (!exists || comparison == JsonCompareResult.different)
                {
                    var    bs = BlobStorage.MakeDefaultBlobStorage();
                    var    timestamped_json_blob_name = string.Format(id + "." + string.Format("{0:yyyy.MM.dd.HH.mm}" + "." + name + ".json", DateTime.UtcNow));
                    var    timestamped_dict_uri       = BlobStorage.MakeAzureBlobUri(id, timestamped_json_blob_name, false);
                    string new_obj_as_json;
                    if (type == JsonSnapshotType.DictStr)
                    {
                        new_obj_as_json = ObjectUtils.DictStrToJson((Dictionary <string, string>)new_obj);
                    }
                    else                     // JsonSnapshotType.ListDictStr
                    {
                        new_obj_as_json = ObjectUtils.ListDictStrToJson((List <Dictionary <string, string> >)new_obj);
                    }
                    //bs.PutBlob(id, json_blob_name, new_obj_as_json, "application/json");
                    bs.PutBlob(id, timestamped_json_blob_name, new_obj_as_json, "application/json");
                }

                if (comparison == JsonCompareResult.same || comparison == JsonCompareResult.invalid)
                {
                    return(false);                    // either the objects matched, or the comparison failed, either way a json snapshot was not saved
                }
                else
                {
                    return(true);                     // the objects did not match, a json snapshot was saved
                }
            }
            catch (Exception e)
            {
                GenUtils.PriorityLogMsg("exception", "SavedJsonSnapshot", e.Message + e.StackTrace);
                return(false);
            }
        }