namespaceMicrosoft.WindowsAzure.Storage.Table{usingSystem;usingSystem.Collections.Generic;usingSystem.Text.Json;/// <summary>/// Useful method for working with <see cref="TableEntity">table entities</see>./// </summary>/// <remarks>Source: https://bitbucket.org/snippets/just_dmitry/Lrg7gB/entitypropertyextensionscs.</remarks>publicstaticclassEntityPropertyExtensions{publicstaticreadonlyJsonSerializerOptionsJsonOptions=newJsonSerializerOptions{AllowTrailingCommas=true,Encoder=System.Text.Encodings.Web.JavaScriptEncoder.Create(System.Text.Unicode.UnicodeRanges.All),IgnoreNullValues=true,IgnoreReadOnlyProperties=true,PropertyNameCaseInsensitive=true,ReadCommentHandling=JsonCommentHandling.Skip,WriteIndented=false,};publicstaticEntityProperty?TryGet(thisIDictionary<string,EntityProperty>properties,stringname){properties=properties??thrownewArgumentNullException(nameof(properties));properties.TryGetValue(name,outvarprop);returnprop;}publicstaticvoidSet(thisIDictionary<string,EntityProperty>properties,stringname,stringvalue){properties=properties??thrownewArgumentNullException(nameof(properties));if(!string.IsNullOrEmpty(value)){properties[name]=newEntityProperty(value);}}publicstaticT?TryGetDeserialized<T>(thisIDictionary<string,EntityProperty>properties,stringname)whereT:class{properties=properties??thrownewArgumentNullException(nameof(properties));if(!properties.TryGetValue(name,outvarprop)){returndefault;}varval=prop.StringValue;if(string.IsNullOrEmpty(val)){returndefault;}returnJsonSerializer.Deserialize<T>(val,JsonOptions);}publicstaticvoidSetSerialized<T>(thisIDictionary<string,EntityProperty>properties,stringname,T?value)whereT:class{properties=properties??thrownewArgumentNullException(nameof(properties));if(value!=null){properties.Add(name,newEntityProperty(JsonSerializer.Serialize(value,JsonOptions)));}}}}
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.