Mikson
Mikson
CC#
Created by Mikson on 2/22/2024 in #help
Trying to utilize async and await
I am trying to display loading animation while the user waits for information fetching. I've been struggling for a couple of days to do things the right way. Of course Http.Get is copied. I didn't write it myself. The loading method has to be modified as well. Description on how to use async and await and Tasks effectively and easily. Any patterns? Code: class Program { static class Utils { public static void Loading(string mess, int spinDelay, ConsoleColor color) { if(Console.BackgroundColor != color) Console.ForegroundColor = color; int i = 0; char[] rFrames = { '|', '/', '-', '\' }; char[] lFrames = { '\', '-', '/', '|' }; int rightid = 0, leftid = 3; while (i < 20) { Console.ForegroundColor = ConsoleColor.Green; Console.Write($"{lFrames[leftid]} {mess} {rFrames[rightid]}"); rightid = (rightid + 1) % rFrames.Length; leftid = (leftid + 1) % lFrames.Length; Thread.Sleep(spinDelay); Console.Write('\r'); Console.Write(new string(' ', 4 + mess.Length)); Console.Write('\r'); i++; } Console.Write('\r'); Console.Write(new string(' ', 4 + mess.Length)); Console.Write('\r'); } } static class Http { public static async Task<string> Get(string url) { using HttpClient client = new HttpClient(); { HttpResponseMessage response = await client.GetAsync(url); response.EnsureSuccessStatusCode(); return await response.Content.ReadAsStringAsync(); } } }
static async Task Main(string[] args) { //I only know that await gives the control back to the caller Console.WriteLine("Hello, World!"); } }
1 replies
CC#
Created by Mikson on 2/19/2024 in #help
How do I easily modify *any* property/object/nested object of C# deserialized .json?
config/data.json { "title": "XYZ", "obj1": { "s1": -1, "s2": "auto", "s3": false, }, "obj2": { "s1": true, "s4": [ [5,0], [4,1] ] }, "obj3": [ { "s3": "s", } ], } program.cs // Classes for JSON de-serialization `public class Obj1 { public int s1 { get; set; } public string s2 { get; set; } public bool s3 { get; set;} } public class Obj2 { public bool s1 { get; set; } public IList<IList<int>> s4 { get; set; } } public class Obj3 { public string s3 { get; set; } } public class Example { public string title { get; set; } public Obj1 obj1 { get; set; } public Obj2 o2 { get; set; } public IList<Obj3> obj3 { get; set; } } //Methods to modify JSON private static string rigJsonPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.Combine("config", "data.json")); public static Example ReadAndDeserialize() { string json = File.ReadAllText(rigJsonPath); Example ex = JsonSerializer.Deserialize<Example>(json)!; return ex;
} public static void SerializeAndWrite(Example ex) { string json = JsonSerializer.Serialize(ex); File.WriteAllText(rigJsonPath, json); } public static void ModifyProperty(Example ex, string propertyName, object propertyValue) { switch (propertyName) { case "obj1.s1": ex.obj1!.s1 = (int)propertyValue; break; // etc } } I want to be able to modify any property/object of JSON through a method in C#. I'd like to create a universal method for this task. The switch is a monkey solution, can't write for lots of objects.
22 replies