public void Ctor_WithCustomFilters() { // Arrange var filter = new TextEncoderSettings(); filter.AllowCharacters('a', 'b'); filter.AllowCharacters('\0', '&', '\uFFFF', 'd'); UnicodeEncoderBase encoder = new CustomUnicodeEncoderBase(filter); // Act & assert Assert.Equal("a", encoder.Encode("a")); Assert.Equal("b", encoder.Encode("b")); Assert.Equal("[U+0063]", encoder.Encode("c")); Assert.Equal("d", encoder.Encode("d")); Assert.Equal("[U+0000]", encoder.Encode("\0")); // we still always encode control chars Assert.Equal("[U+0026]", encoder.Encode("&")); // we still always encode HTML-special chars Assert.Equal("[U+FFFF]", encoder.Encode("\uFFFF")); // we still always encode non-chars and other forbidden chars }
public void Ctor_WithTextEncoderSettings() { // Arrange var filter = new TextEncoderSettings(); filter.AllowCharacters('a', 'b'); filter.AllowCharacters('\0', '&', '\uFFFF', 'd'); HtmlEncoder encoder = new HtmlEncoder(filter); // Act & assert Assert.Equal("a", encoder.HtmlEncode("a")); Assert.Equal("b", encoder.HtmlEncode("b")); Assert.Equal("c", encoder.HtmlEncode("c")); Assert.Equal("d", encoder.HtmlEncode("d")); Assert.Equal("�", encoder.HtmlEncode("\0")); // we still always encode control chars Assert.Equal("&", encoder.HtmlEncode("&")); // we still always encode HTML-special chars Assert.Equal("", encoder.HtmlEncode("\uFFFF")); // we still always encode non-chars and other forbidden chars }
public static void Run() { string jsonString; WeatherForecast weatherForecast = WeatherForecastFactories.CreateWeatherForecastCyrillic(); weatherForecast.DisplayPropertyValues(); Console.WriteLine("Default serialization - non-ASCII escaped"); var options = new JsonSerializerOptions { WriteIndented = true }; jsonString = JsonSerializer.Serialize(weatherForecast, options); Console.WriteLine(jsonString); Console.WriteLine(); Console.WriteLine("Serialize language sets unescaped"); // <LanguageSets> options = new JsonSerializerOptions { Encoder = JavaScriptEncoder.Create(UnicodeRanges.BasicLatin, UnicodeRanges.Cyrillic), WriteIndented = true }; jsonString = JsonSerializer.Serialize(weatherForecast, options); // </LanguageSets> Console.WriteLine(jsonString); Console.WriteLine(); Console.WriteLine("Serialize selected unescaped characters"); // <SelectedCharacters> var encoderSettings = new TextEncoderSettings(); encoderSettings.AllowCharacters('\u0436', '\u0430'); encoderSettings.AllowRange(UnicodeRanges.BasicLatin); options = new JsonSerializerOptions { Encoder = JavaScriptEncoder.Create(encoderSettings), WriteIndented = true }; jsonString = JsonSerializer.Serialize(weatherForecast, options); // </SelectedCharacters> Console.WriteLine(jsonString); Console.WriteLine(); Console.WriteLine("Serialize using unsafe relaxed encoder"); // <UnsafeRelaxed> options = new JsonSerializerOptions { Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, WriteIndented = true }; jsonString = JsonSerializer.Serialize(weatherForecast, options); // </UnsafeRelaxed> Console.WriteLine(jsonString); }
public void Ctor_WithTextEncoderSettings() { // Arrange var filter = new TextEncoderSettings(); filter.AllowCharacters('a', 'b'); filter.AllowCharacters('\0', '&', '\uFFFF', 'd'); UrlEncoder encoder = UrlEncoder.Create(filter); // Act & assert Assert.Equal("a", encoder.Encode("a")); Assert.Equal("b", encoder.Encode("b")); Assert.Equal("%63", encoder.Encode("c")); Assert.Equal("d", encoder.Encode("d")); Assert.Equal("%00", encoder.Encode("\0")); // we still always encode control chars Assert.Equal("%26", encoder.Encode("&")); // we still always encode HTML-special chars Assert.Equal("%EF%BF%BF", encoder.Encode("\uFFFF")); // we still always encode non-chars and other forbidden chars }
public void Ctor_WithTextEncoderSettings() { // Arrange var filter = new TextEncoderSettings(); filter.AllowCharacters('a', 'b'); filter.AllowCharacters('\0', '&', '\uFFFF', 'd'); JavaScriptStringEncoder encoder = new JavaScriptStringEncoder(filter); // Act & assert Assert.Equal("a", encoder.JavaScriptStringEncode("a")); Assert.Equal("b", encoder.JavaScriptStringEncode("b")); Assert.Equal(@"\u0063", encoder.JavaScriptStringEncode("c")); Assert.Equal("d", encoder.JavaScriptStringEncode("d")); Assert.Equal(@"\u0000", encoder.JavaScriptStringEncode("\0")); // we still always encode control chars Assert.Equal(@"\u0026", encoder.JavaScriptStringEncode("&")); // we still always encode HTML-special chars Assert.Equal(@"\uFFFF", encoder.JavaScriptStringEncode("\uFFFF")); // we still always encode non-chars and other forbidden chars }
public void AllowChars_String() { // Arrange var filter = new TextEncoderSettings(); filter.AllowCharacters('\u0100', '\u0102'); // Assert Assert.True(filter.IsCharacterAllowed('\u0100')); Assert.False(filter.IsCharacterAllowed('\u0101')); Assert.True(filter.IsCharacterAllowed('\u0102')); Assert.False(filter.IsCharacterAllowed('\u0103')); }
public async Task StorageTranslatedWordAsync(List <Word> wordList, string path) { var encoderSettings = new TextEncoderSettings(); encoderSettings.AllowCharacters('\u0027'); //Invalid encoderSettings.AllowRange(UnicodeRanges.All); var options = new JsonSerializerOptions { Encoder = JavaScriptEncoder.Create(encoderSettings), WriteIndented = true }; string txt = JsonSerializer.Serialize(wordList, options); txt = txt.Replace("\\u0027", "'"); await File.WriteAllTextAsync(path, txt); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { var encoderSettings = new TextEncoderSettings(); encoderSettings.AllowRange(UnicodeRanges.All); encoderSettings.AllowCharacters(':'); services.AddControllers().AddJsonOptions(options => { options.JsonSerializerOptions.Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping; }); services.AddSingleton <IImageTableStorage, ImageTableStorage>(); services.AddSingleton <IUserNameProvider, UserNameProvider>(); services.AddSingleton <ICloudStorageAccountProvider, CloudStorageAccountProvider>(); services.AddSingleton <IConnectionStringProvider, ConnectionStringProvider>(); }
public static string Serialize(object obj) { // Reuse options, https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-configure-options?pivots=dotnet-5-0 // https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-character-encoding var encoderSettings = new TextEncoderSettings(); // ä, Ö, å, Ä, ö encoderSettings.AllowCharacters('\u00E4', '\u00D6', '\u00E5', '\u00C4', '\u00F6'); encoderSettings.AllowRange(UnicodeRanges.BasicLatin); var options = new JsonSerializerOptions { Encoder = JavaScriptEncoder.Create(encoderSettings) }; return(JsonSerializer.Serialize(obj, options)); }
public void AllowChars_Null() { TextEncoderSettings filter = new TextEncoderSettings(); Assert.Throws <ArgumentNullException>("characters", () => filter.AllowCharacters(null)); }
static async System.Threading.Tasks.Task <int> Main(string[] args) { PaulsOKWrapper github; // Processors not cores, probably need something else in the future int ProcessorCount = Environment.ProcessorCount; int NumThreadsToRun = ThreadsPerProcessor * ProcessorCount; if (args.Length < 3) { Console.WriteLine("need atleast 3 args"); return(1); } string[] extensionList = new string[] { ".jpg", ".png", ".jpeg", ".JPG", ".PNG", ".JPEG", ".bmp", ".BMP" }; System.IO.DirectoryInfo ImagesDirectory; System.IO.FileInfo fi; var CurrentBranch = "master"; var GHPages = "gh-pages"; var AutoMergeLabel = "automerge"; var Repo = "Repo"; var Owner = "Owner"; var ImgDir = args[0]; var jsonPath = args[1] + "\\" + Jekyll_data_Folder + "\\" + Jekyll_data_File; var repopath = args[1]; var domain = args[2]; if (ImgDir is string) { ImagesDirectory = new System.IO.DirectoryInfo(ImgDir); } else if (ImgDir as String != null) { ImagesDirectory = new System.IO.DirectoryInfo(ImgDir as string); } else { Console.WriteLine("First Arg must be a directory"); return(1); } var GitHubStuff = false; if (args.Length >= 4 && args[3] is string && bool.Parse(args[3])) { GitHubStuff = bool.Parse(args[3]); } github = new PaulsOKWrapper(GitHubStuff); if (GitHubStuff) { github.AttemptLogin(); } bool CommitContainedImages = true; if (GitHubStuff && github.CleanlyLoggedIn) { {// Setup the Owner and the Repo Repo = args[4] as string; Owner = args[5] as string; github.SetOwnerAndRepo(Owner, Repo); } {// Setup for merge between branches by label if (args.Length >= 7 && args[6] is string) { CurrentBranch = args[6] as string; } if (args.Length >= 8 && args[7] is string) { GHPages = args[7] as string; } if (args.Length >= 9 && args[8] is string) { AutoMergeLabel = args[8] as string; } github.SetupForMergesByLabel(AutoMergeLabel, CurrentBranch, GHPages); bool commitsetup = await github.SetupCommit(); Console.WriteLine("Setup to commit changes: " + commitsetup.ToString()); CommitContainedImages = await github.CommitContainedImages(ImagesDirectory); } } if (repopath is string || repopath as string != null) { github.RepoDirectory = new System.IO.DirectoryInfo(repopath as string); } else { Console.WriteLine("Second Arg must be a directory"); return(1); } if (jsonPath is string) { fi = new System.IO.FileInfo(jsonPath); } else if (jsonPath as String != null) { fi = new System.IO.FileInfo(jsonPath as string); } else { Console.WriteLine("Second Arg must be a directory that can lead to " + "\\" + Jekyll_data_Folder + "\\" + Jekyll_data_File); return(1); } if (!fi.Directory.Exists) { fi.Directory.Create(); } if (ImagesDirectory.Exists == false) // VSCode keeps offering to "fix this" for me... { Console.WriteLine("Directory [" + ImgDir + "] Doesn't exist"); return(1); } if (!(domain is string)) { Console.WriteLine("arg 3 needs to be your domain"); } int successfull = 0; if (CommitContainedImages) { int NumCommitted = 0; DirectoryDescriptor DD = new DirectoryDescriptor(ImagesDirectory.Name, ImagesDirectory.FullName); // Traverse Image Directory ImageHunter ih = new ImageHunter(ref DD, ImagesDirectory, extensionList); string v = ih.NumberImagesFound.ToString(); Console.WriteLine(v + " Images Found"); var ImagesList = ih.ImageList; System.IO.DirectoryInfo thumbnail = new System.IO.DirectoryInfo(ImgDir + "\\" + THUMBNAILS); if (!thumbnail.Exists) { thumbnail.Create(); } Console.WriteLine("Images to be resized"); ImageResizer ir = new ImageResizer(thumbnail, 256, 256, 1024, 1024, true, true); //@@ Add Multithreading via thread pool here foreach (ImageDescriptor id in ImagesList) { try{ System.Console.WriteLine("Image: " + id.Name); id.FillBasicInfo(); if (ir.ThumbnailNeeded(id)) { var increase_count = ir.GenerateThumbnail(id, github); if (github.DoGitHubStuff && increase_count) { ++NumCommitted; } } if (ir.NeedsResize(id)) { // when our algorithm gets better, or or image sizes change var increase_count = ir.ResizeImages(id, github); if (github.DoGitHubStuff && increase_count) { ++NumCommitted; } } }catch (Exception ex) { Console.WriteLine(ex.ToString()); } } Console.WriteLine("Images have been resized"); Console.WriteLine("fixing up paths"); DD.FixUpPaths(github.RepoDirectory); github.SetNumberImagesCommitted(NumCommitted); if (github.DoGitHubStuff && NumCommitted >= 1) // Need more than 1 changed file to try and commit things { Console.WriteLine(NumCommitted + " Images were generated, now to push to push them to master"); int RateLimitCallsLeft = github.DecrementAPICallsBy(0); if ((NumCommitted * 2) > RateLimitCallsLeft) { Console.WriteLine(); Console.WriteLine("WARNING!!!!"); Console.WriteLine(); Console.WriteLine("WARNING!!!!"); Console.WriteLine(); Console.WriteLine("WARNING!!!!"); Console.WriteLine(); Console.WriteLine("GitHub API Will rate limit to 1000 calls an hour. you are adding " + NumCommitted + " files and you have " + RateLimitCallsLeft + " API calls left"); Console.WriteLine(); Console.WriteLine("WARNING!!!!"); Console.WriteLine(); Console.WriteLine("WARNING!!!!"); Console.WriteLine(); Console.WriteLine("WARNING!!!!"); Console.WriteLine(); } //https://laedit.net/2016/11/12/GitHub-commit-with-Octokit-net.html if (!await github.CommitAndPush()) { successfull = 2; } Console.WriteLine(" --- "); } else { Console.WriteLine("No images were altered, nothing to push to main"); } Console.WriteLine("Domain Set to: " + domain); DD.SaveMDFiles(domain, ImagesDirectory, github); //This follows another path... Console.WriteLine("Image indexes written"); var encoderSettings = new TextEncoderSettings(); encoderSettings.AllowCharacters('\u0436', '\u0430'); encoderSettings.AllowRange(UnicodeRanges.BasicLatin); var options = new JsonSerializerOptions { IgnoreReadOnlyProperties = false, WriteIndented = true, IgnoreNullValues = false, //Encoder = JavaScriptEncoder.Create(UnicodeRanges.BasicLatin, UnicodeRanges.Cyrillic), }; var jsonString = JsonSerializer.Serialize <DirectoryDescriptor>(DD, options); { var fs = fi.Create(); System.IO.TextWriter tw = new System.IO.StreamWriter(fs); tw.Write(jsonString); tw.Close(); //fs.Close(); } Console.WriteLine("Json written"); if (github.DoGitHubStuff && NumCommitted >= 1) { Console.WriteLine("Committing Json files"); await github.ImmediatlyAddorUpdateTextFile(fi); System.IO.FileInfo NewPath = new System.IO.FileInfo(args[1] + "\\_includes\\gallery.json"); if (NewPath.Exists) { NewPath.Delete(); } System.IO.File.Copy(fi.FullName, NewPath.FullName); await github.ImmediatlyAddorUpdateTextFile(NewPath); Console.WriteLine("Json files Committed"); Console.WriteLine(" --- "); } else { Console.WriteLine("No Images Altered, No Json files Committed"); Console.WriteLine(" --- "); } // do we need a synronication point here? lots of things could be going on in parallel // DO NOT TRY AND GET CLEVER AND || your github api usage. it will end in tears // if you are looking at this call, you are about to do something stupid, don't do it. await github.SyncPoint(true); } bool APICALLSAVING = true; //code that we are only doing because we have 1k API calls an hour if (github.DoGitHubStuff) { if (!github.CleanlyLoggedIn) { Console.WriteLine("GitHub Login State unclear, bad things may happen"); } string PRname = "From " + CurrentBranch + " to " + GHPages; bool StalePRSuccess = await github.FindStalePullRequests(PRname); if (!StalePRSuccess && successfull == 0) { successfull = 3; } if (!APICALLSAVING && StalePRSuccess) { //would prefer to close out and re-open, but need to save API Calls Console.WriteLine("add Attempt to close stale pull request here"); StalePRSuccess = await github.CloseStalePullRequests(PRname); } if (APICALLSAVING) { // would prefer to close PR"s and re-open, but gotta save them API calls, // only get 1k an hour if (!StalePRSuccess) { bool CreatePRSuccess = await github.CreateAndLabelPullRequest(PRname); if (!CreatePRSuccess && successfull == 0) { successfull = 4; } } else { Console.WriteLine("PR Already exists, so not creating one - saving API calls"); } } else { bool CreatePRSuccess = await github.CreateAndLabelPullRequest(PRname); if (!CreatePRSuccess && successfull == 0) { successfull = 4; } } github.RefreshRateLimits(); Console.WriteLine("Run has finished Exiting..."); Console.WriteLine("Current Code is : " + successfull.ToString()); } github.PrintRateLimits(); if (successfull == 3 || successfull == 4) {// let's not say we failed in these cases. return(0); } return(successfull); }
/// <summary> /// Creates a JSON objectstring to render the reserved Seats in the view /// </summary> /// <param name="Room">A Room of type Room.</param> /// <param name="Seats">A list of type ReservedSeat.</param> /// <returns>A JSON string to the ViewModel</returns> public static string JSONSeating(Room Room, List <ReservedSeat> Seats) { if (Room == null) { Room = new Room(); } List <ObjRow> ObjRowList = new List <ObjRow>(); for (var j = 1; j <= Room.Rows; j++) { List <ObjSeat> objSeatList = new List <ObjSeat>(); for (var i = 1; i <= (Room.Capacity / Room.Rows); i++) { ObjSeat ObjSeat = new ObjSeat { GridSeatNum = i, seatNumber = i, SeatStatus = "0" }; var seatTaken = 0; Seats.ForEach(s => { if (s.SeatId == i & s.RowId == j & s.SeatState == SeatState.Reserved) { seatTaken = 1; } if (s.SeatId == i & s.RowId == j & s.SeatState == SeatState.Disabled) { seatTaken = 2; } }); if (seatTaken > 0) { ObjSeat.SeatStatus = seatTaken.ToString(); seatTaken = 0; } objSeatList.Add(ObjSeat); } ObjRow ObjRow = new ObjRow { GridRowId = j, PhyRowId = j.ToString(), objSeat = objSeatList }; ObjRowList.Add(ObjRow); } ObjArea ObjArea = new ObjArea { AreaDesc = "EXECUTIVE", AreaCode = "0000000003", AreaNum = "1", HasCurrentOrder = true, objRow = ObjRowList }; List <ObjArea> ObjAreaList = new List <ObjArea>(); ObjAreaList.Add(ObjArea); ColAreas ColAreas = new ColAreas { Count = 1, intMaxSeatId = 21, intMinSeatId = 2, objArea = ObjAreaList }; SeatLayout SeatLayout = new SeatLayout { colAreas = ColAreas }; List <object> areaList = new List <object>(); List <object> groupedSeatsList = new List <object>(); Root Root = new Root { product_id = 46539040, freeSeating = false, tempTransId = "1ecae165f2d86315fea19963d0ded41a", seatLayout = SeatLayout, areas = areaList, groupedSeats = groupedSeatsList }; var encoderSettings = new TextEncoderSettings(); encoderSettings.AllowCharacters('\u0022'); encoderSettings.AllowRange(UnicodeRanges.BasicLatin); var options = new JsonSerializerOptions { Encoder = JavaScriptEncoder.Create(encoderSettings), WriteIndented = true }; string jsonString = JsonSerializer.Serialize(Root, options); return(jsonString); }