public static void Run() { string json = File.ReadAllText("jsontest"); JsValue jsobj = JsValue.FromJson(json); Console.WriteLine($"[json demo] First key of object.hello[6]: {jsobj["hello"][6].Keys.First()}"); User[] users = jsobj["hello"][6]["users"].ArrayValue .Select(User.FromJsValue).ToArray(); Console.WriteLine($"[json demo] {users[1].Name} verified: {users[1].Verified}"); Console.WriteLine( $"[json demo] Extract strings: {new JsValue(jsobj["hello"].ArrayValue.Where(j => j.Type == JsValue.DataType.String).ToArray()).ToJson()}"); Dictionary <string, JsValue> dictionary = new Dictionary <string, JsValue> { ["store_id"] = "C555", ["pending_user_accounts"] = new [] { new User("Jedward"), new User("Hoagie"), }, ["unread_notice_ids"] = new [] { 52, 111 }, }; Console.WriteLine("[json demo] dictionary<string,jsvalue>.ToJson(true): " + dictionary.ToJson(true)); /* * [json demo] First key of object.hello[6]: animal * [json demo] Linda Picklesbury ✨ verified: True * [json demo] Extract strings: ["spaghetti","escaping with \"\\\""] * [json demo] dictionary.ToJSON(): {"store_id":"C555","pending_user_accounts":[{"name":"Jedward","email_verified":false},{"name":"Hoagie","email_verified":false}],"unread_notice_ids":[52,111]} */ }
public static void Run() { var bm = new Benchmarker("JSON"); if (File.Exists(compareDataFilename)) { bm.PreviousData = JsValue.FromJson(File.ReadAllText(compareDataFilename)); } bm.TimeLimitMs = 3000; bm.Participants["Utf8Json"] = data => Utf8Json.JsonSerializer.Deserialize <object>(data as string); bm.Participants["System.Text"] = data => System.Text.Json.JsonSerializer.Deserialize <object>(data as string); bm.Participants["Newtonsoft"] = data => Newtonsoft.Json.JsonConvert.DeserializeObject <object>(data as string); bm.Participants["Face.Json"] = data => JsValue.FromJson(data as string); bm.Tests.Add("hardcoded_example", new Benchmarker.Test { Remark = "basic example test", Action = (run, data) => run("[2, 4,6, 8, null, 3.142]") // called for each bm.Participants["..."] as run, data is from Setup: // Setup: () => "eg data" // called once per Test before iterating participants }); // load the rest from testidx.json JsValue[] testIndex = JsValue.FromJson(File.ReadAllText("demo/testidx.json"), true); foreach (var testSpec in testIndex) { if (testSpec.ContainsKey("filename") && !File.Exists(testSpec["filename"])) { Console.WriteLine($"Missing test source '{testSpec["filename"]}'"); continue; } Func <object> setup; string testRef; string remark; if (testSpec.IsString) { var displayJson = testSpec.StringValue.Replace("\n", "␊").Replace("\r", ""); if (displayJson.Length > 24) { displayJson = displayJson.Substring(0, 21) + "..."; } remark = $"{formatFileSize(testSpec.StringValue.Length)} ━ '{displayJson}'"; setup = () => testSpec.StringValue; testRef = testSpec.StringValue; } else if (testSpec.ContainsKey("filename")) { string filename = testSpec["filename"]; var length = new FileInfo(filename).Length; testRef = testSpec["filename"].StringValue.Split("/").Last(); setup = () => File.ReadAllText(filename); remark = $"{formatFileSize(length)} ━ {testSpec.PropertyValueOr("remark", testRef)}"; } else { string json = testSpec["json"]; var md5 = System.Security.Cryptography.MD5.Create(); testRef = string.Join("", md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(json)).Select(b => $"{b:X}")); setup = () => json; var displayJson = json.Replace("\n", "␊").Replace("\r", ""); if (displayJson.Length > 24) { displayJson = displayJson.Substring(0, 21) + "..."; } remark = $"{formatFileSize(json.Length)} ━ {testSpec.PropertyValueOr("remark", testRef.Substring(0, 6))} ━ '{displayJson}'"; } bm.Tests[testRef] = new Benchmarker.Test { Remark = remark, //Action = (run, data) => run(data), // this is now the default action Setup = setup // result passed to Action as data }; } File.WriteAllText(logFilename, ""); Thread.Sleep(1000); bm.Run((s, col) => { Console.ForegroundColor = col; Console.Write(s); var writer = File.AppendText(logFilename); writer.WriteAsync(s); writer.Close(); }); File.WriteAllText(compareDataFilename, bm.PreviousData.ToJson(true)); }
/// <summary> /// Attempts to parse all .json files in the given directory. Files beginning with 'y' are expected to parse without error, 'n' to fail and anything else to either parse or fail. /// </summary> /// <param name="pathToJsonFiles"></param> /// <param name="showPass">True to output all test results, false to only show failures</param> /// <param name="filter">Limits tests to filenames containing this string</param> public static void RunSuite(string pathToJsonFiles, bool showPass, string filter = "") { var filenames = Directory.GetFiles(pathToJsonFiles); foreach (var filename in filenames) { if (!filename.Contains(filter)) { continue; } string content = File.ReadAllText(filename); var basename = filename.Split("/").Last(); basename += " " + (content.Length > 32 ? content.Substring(0, 32) + "..." : content); var expectSuccess = basename[0] == 'y'; var expectFailure = basename[0] == 'n'; try { JsValue.FromJson(content); } catch (Exception e) { if (expectSuccess) { Console.WriteLine("** FAILED ** - " + basename + " - " + RenderException(e)); } else if (expectFailure) { if (showPass) { Console.WriteLine("Pass (expected error) - " + basename + " - " + RenderException(e)); } } else { if (showPass) { Console.WriteLine("Pass (optional, thrown) - " + basename + " - " + RenderException(e)); } } continue; } if (expectSuccess) { if (showPass) { Console.WriteLine("Pass (expected success) - " + basename); } } else if (expectFailure) { Console.WriteLine("** FAILED ** (expected error) - " + basename); Console.WriteLine(content); } else { if (showPass) { Console.WriteLine("Pass (optional) - " + basename); } } } }