I am trying to set an arbitrary path in a JSON structure and I am having difficulty figuring out how to do a simple set value...
What I would like is some method like, SetValue(path,value) which operates like SelectToken, but creates the path if it does not exist and sets the value.
public void SetPreference(string username, string path, string value)
{
var prefs = GetPreferences(username);
var jprefs = JObject.Parse(prefs ?? #"{}");
var token = jprefs.SelectToken(path);
if (token != null)
{
// how to set the value of the path?
}
else
// how to add the path and value, example {"global.defaults.sort": { "true" }}
}
what I mean by global.defaults.sort path is actually { global: { defaults: { sort: { true } } } }
public string SetPreference(string username, string path, string value)
{
if (!value.StartsWith("[") && !value.StartsWith("{"))
value = string.Format("\"{0}\"", value);
var val = JObject.Parse(string.Format("{{\"x\":{0}}}", value)).SelectToken("x");
var prefs = GetPreferences(username);
var jprefs = JObject.Parse(prefs ?? #"{}");
var token = jprefs.SelectToken(path) as JValue;
if (token == null)
{
dynamic jpart = jprefs;
foreach (var part in path.Split('.'))
{
if (jpart[part] == null)
jpart.Add(new JProperty(part, new JObject()));
jpart = jpart[part];
}
jpart.Replace(val);
}
else
token.Replace(val);
SetPreferences(username, jprefs.ToString());
return jprefs.SelectToken(path).ToString();
}
Related
I am trying to perform validation using custom class attributes. I have to keep my model validation rules inside XML files. Custom Class attribute specifies the path to the XML file and custom attributes will contain the logic to read XML files and get the validation rules for that class. When I will call Model.Isvalidate() that time Validation will get executed. I already have this working in .Net 4.5 MVC application. The same thing I am trying in .Net 6 its not working.
public class CustomDynamicModelValidatorProvider : DataAnnotationsModelValidatorProvider,IModelValidatorProvider
{
protected override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, IEnumerable<ModelValidatorProvider> validatorProviders, IEnumerable<Attribute> attributes)
{
string resourceKeyPath = string.Empty;
IList<Attribute> newAttributes = new List<Attribute>(attributes);
CustomDynamicValidatorsAttribute resourceMapperAttr = attributes.FirstOrDefault(a => a is CustomDynamicValidatorsAttribute) as CustomDynamicValidatorsAttribute;
if (resourceMapperAttr == null)
{
System.Reflection.MemberInfo classInfo = metadata.ContainerType;
if (classInfo != null)
{
var resourceMapperRootAttr = classInfo.GetCustomAttributes(typeof(ResourceMappingRootAttribute), false).FirstOrDefault() as ResourceMappingRootAttribute;
if (resourceMapperRootAttr != null)
{
resourceKeyPath = resourceMapperRootAttr.Path + "." + metadata.PropertyName;
}
}
}
else
{
resourceKeyPath = resourceMapperAttr.ResourceKeyPath;
}
if (!string.IsNullOrEmpty(resourceKeyPath))
{
string[] validators = ResourceManager.GetValidators(resourceKeyPath).Replace(" ", "").Split(',');
var maxLength = ResourceManager.GetMaxLength(resourceKeyPath);
if (!string.IsNullOrEmpty(maxLength))
{
var required = new MaxLengthAttribute(maxLength.ToInteger());
required.ErrorMessage = string.Format(CustomAttributeHelper.GetResourceText("Shared.Messages.MaximumLengthExceeded"), maxLength);
newAttributes.Add(required);
}
for (int i = 0; i < validators.Length; i++)
{
if (string. Equals(validators[i], "required", StringComparison.OrdinalIgnoreCase))
{
var required = new CustomRequiredAttribute();
newAttributes.Add(required);
}
else if (validators[i].StartsWith("email", StringComparison.OrdinalIgnoreCase))
{
var email = new CustomEmailAttribute();
newAttributes.Add(email);
}
}
}
return base.GetValidators(metadata,validatorProviders);
}
public static IDictionary<string, object> GetValidatorAttributes(string validators)
{
IDictionary<string, object> attributes = new Dictionary<string, object>();
string[] validatorList = !string.IsNullOrEmpty(validators) ? validators.Replace(" ", "").Split(',') : new string[] { };
foreach (var item in validatorList)
{
if (!attributes.Keys.Contains("data-val"))
{
attributes.Add("data-val", "true");
}
if (string.Equals(item, "required", StringComparison.OrdinalIgnoreCase))
{
attributes.Add("data-val-required", CustomAttributeHelper.GetResourceText("Shared.Messages.Mandatory"));
}
else if (item.StartsWith("email", StringComparison.OrdinalIgnoreCase))
{
attributes.Add("data-val-email", CustomAttributeHelper.GetResourceText("Shared.Messages.vEmailField"));
}
else if (item.StartsWith("phone", StringComparison.OrdinalIgnoreCase))
{
attributes.Add("data-val-phone", CustomAttributeHelper.GetResourceText("Shared.Messages.vPhoneField"));
}
else if (item.StartsWith("zipcode", StringComparison.OrdinalIgnoreCase))
{
attributes.Add("data-val-zipcode", CustomAttributeHelper.GetResourceText("Shared.Messages.vZipCodeField"));
}
else if (item.StartsWith("mark", StringComparison.OrdinalIgnoreCase))
{
string min = string.Empty;
string max = string.Empty;
string rangeValidatorMessage = string.Empty;
if (item.Contains("-"))
{
var rangeArray = item.Split('-');
if (rangeArray.Length > 2)
{
min = rangeArray[1];
max = rangeArray[2];
rangeValidatorMessage = CustomAttributeHelper.GetResourceText("Shared.Messages.NumberRange");
}
else
{
max = rangeArray[1];
rangeValidatorMessage = CustomAttributeHelper.GetResourceText("Shared.Messages.vNumericLimited");
}
}
else
{
max = "10000";
}
if (!string.IsNullOrWhiteSpace(min))
{
attributes.Add("data-val-range-min", min);
}
attributes.Add("data-val-range-max", max);
attributes.Add("data-val-range", rangeValidatorMessage);
}
else if (item.StartsWith("number", StringComparison.OrdinalIgnoreCase))
{
attributes.Add("data-val-number", CustomAttributeHelper.GetResourceText("Shared.Messages.NumberRange"));
}
else if (item.StartsWith("number", StringComparison.OrdinalIgnoreCase))
{
attributes.Add("data-val-decimal", string.Format(CustomAttributeHelper.GetResourceText("Shared.Messages.DecimalRange"), CustomAttributeHelper.GetResourceText("Shared.Limits.Decimal")));
}
else if (item.StartsWith("file-", StringComparison.OrdinalIgnoreCase))
{
attributes.Add("data-val-filetype", item.Split('-')[1]);
}
}
return attributes;
}
public void CreateValidators(ModelValidatorProviderContext context)
{
}
}
I'm new in C# and I'm writing API test. I need to write test which will create new user. Here it is:
public async Task CreatingNewUser_Returns200()
{
string client = BaseClass.mainURL;
var json = SettingsHelper.ReadSetting("NewUser");
httpc.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(BaseClass.authKey, BaseClass.authValue);
var content = new StringContent(json);
var response = await httpc.PostAsync(client, content);
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
}
Also, I have SettingsHelper class with two methods to make my json readable:
private static JObject _jObject;
public static string ReadSetting(string name)
{
var parts = name.Split('.', StringSplitOptions.RemoveEmptyEntries);
JToken token = GetObject();
foreach (var part in parts)
{
token = token[part];
if (token == null)
{
return null;
}
}
return token.Value<string>();
}
private static JObject GetObject()
{
if (_jObject != null)
{
return _jObject;
}
var filename = Path.Combine(
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!,
"appsettings.json");
var json = File.ReadAllText(filename);
_jObject = JObject.Parse(json);
return _jObject;
}
And I have appsettings.json file with my user:
"NewUser": {
"externalDealId": "6051",
"Id": "12312111" // and so on
}
But when I launch this test I get an error (title name). How can I fix this?
Modify the code to
public static string ReadSetting(string name)
{
var parts = name.Split('.', StringSplitOptions.RemoveEmptyEntries);
JObject jObj= GetObject();
foreach (var part in parts)
{
var token = jObj[part];
if (token == null)
{
return null;
}
else
{
return token.ToString();
}
}
return null;
}
Actually, there is still a logic which needs to be noticed var parts = name.Split('.', StringSplitOptions.RemoveEmptyEntries); may mean that support multiple setting results. But logic in the ReadSetting function is not. It only return the first setting is matched.
I'm trying to use PHPickerController and access PHAsset to get file name and file size but the PHAsset are null
var config = new PHPickerConfiguration(PHPhotoLibrary.SharedPhotoLibrary) {
Filter = PHPickerFilter.ImagesFilter,
SelectionLimit = 1
};
var picker= new PHPickerViewController(config) {
ModalPresentationStyle = UIModalPresentationStyle.Popover,
Delegate = new ImagePickerDelegate((fileSize, fileName, url) => {
})
};
ViewController.PresentViewController(picker, true, null);
public class ImagePickerDelegate : PHPickerViewControllerDelegate
{
public ImagePickerDelegate(Action<int, string, string> action)
{
Action = action;
}
public Action<int, string, string> Action { get; }
public override void DidFinishPicking(PHPickerViewController picker, PHPickerResult[] results)
{
picker.DismissViewController(true, null);
foreach (var result in results)
{
var asset = PHAsset.FetchAssets(result.AssetIdentifier, null)?.firstObject as PHAsset;
// The asset are null
var fileSize = asset.ValueForKey((NSString)"fileSize");
}
}
}
As you can see in the image the request dialog show and code are not pause on following line
var asset = PHAsset.FetchAssets(result.AssetIdentifier, null)?.firstObject as PHAsset;
and return null
You could use FetchAssetsUsingLocalIdentifiers method to get PHAsset object, then it will return value.
Sample code as follows:
public override void DidFinishPicking(PHPickerViewController picker, PHPickerResult[] results)
{
picker.DismissViewController(true, null);
foreach (var result in results)
{
var refID = result.AssetIdentifier;
string[] refIDs = new string[] { refID };
var asset = PHAsset.FetchAssetsUsingLocalIdentifiers(refIDs, null)?.firstObject as PHAsset;
// var fileSize = asset.ValueForKey((NSString)"fileSize");
}
}
Also could have a look at this native code link.
Cookies are update fine in all browsers but in google chrome, it fails to update the cookies.
Bellow is my code:
public static string CustomerName
{
get { return CookieStore.GetCookie("customername"); }
set { CookieStore.SetCookie("customername", value.ToString(), TimeSpan.FromHours(24), true); }
}
public static void SetCookie(string key, string value, TimeSpan expires, bool http = false)
{
HttpCookie encodedCookie = new HttpCookie(key, value);
// encodedCookie.HttpOnly = http;
if (HttpContext.Current.Request.Cookies[key] != null)
{
var cookieOld = HttpContext.Current.Request.Cookies[key];
cookieOld.Expires = DateTime.Now.Add(expires);
cookieOld.Value = encodedCookie.Value;
HttpContext.Current.Response.Cookies.Add(cookieOld);
}
else
{
encodedCookie.Expires = DateTime.Now.Add(expires);
HttpContext.Current.Response.Cookies.Add(encodedCookie);
}
}
Actually the problem found in SetCookie function. you need to replace the following line of code
var cookieOld = HttpContext.Current.Request.Cookies[key];
with the following line while updating your cookie.
var cookieOld = new HttpCookie(key);
Bellow is the complete SetCookie function.
public static void SetCookie(string key, string value, TimeSpan expires, bool http = false)
{
if (HttpContext.Current.Request.Cookies[key] != null)
{
var cookieOld = new HttpCookie(key);
cookieOld.Expires = DateTime.Now.Add(expires);
cookieOld.Value = value;
HttpContext.Current.Response.Cookies.Add(cookieOld);
}
else
{
HttpCookie encodedCookie = new HttpCookie(key, value);
encodedCookie.Expires = DateTime.Now.Add(expires);
HttpContext.Current.Response.Cookies.Add(encodedCookie);
}
}
I have a FilterDefinition build that will look for an address based on the properties that are not empty.
public static FilterDefinition<TU> FindPointByAddress<TU>(Address address)
{
var filterBuilder = Builders<TU>.Filter;
var filterItems = new List<FilterDefinition<TU>>();
if (!String.IsNullOrWhiteSpace(address.Street))
{
filterItems.Add(filterBuilder.Eq("Address.Street", address.Street));
}
if (!String.IsNullOrWhiteSpace(address.City))
{
filterItems.Add(filterBuilder.Eq("Address.City", address.City));
}
if (!String.IsNullOrWhiteSpace(address.StateProvince))
{
filterItems.Add(filterBuilder.Eq("Address.StateProvince", address.StateProvince));
}
if (!String.IsNullOrWhiteSpace(address.PostCode))
{
filterItems.Add(filterBuilder.Eq("Address.PostCode", address.PostCode));
}
return filterBuilder.And(filterItems);
}
IMO this query feels dirty, is there a better way to build this type of query or is this the correct way?
A few days ago I had a similar situation. I wrote a simple method that takes a field name and field value as a string.
public void AddEqualCompareFilter(string fieldName, string fieldValue)
{
if (String.IsNullOrEmpty(fieldValue) == false) {
if (Filter != null) {
Filter = Filter & Builders<TranslationsDocument>.Filter.Eq(fieldName, fieldValue);
}
else {
FilterCount++;
Filter = Builders<TranslationsDocument>.Filter.Eq(fieldName, fieldValue);
}
}
}
I am then using this snippet to decide based on FilterCount:
if (FilterCount > 0) {
Result = collection.Find(Filter).ToListAsync().GetAwaiter().GetResult();
return true;
}
else {
Result = collection.Find(new BsonDocument()).ToListAsync().GetAwaiter().GetResult();
return true;
}