static void TestToJson(string name, Func <JsongTests3, string> method, JsongTests3 test, string expectedJson) { var result = method(test); if (result != expectedJson) { throw new Exception($"Method {name} didn't produce correct json {expectedJson} actual {result}"); } var stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); for (int index = 0; index < 10000000; index++) { method(test); } stopwatch.Stop(); Console.WriteLine($"{name}: {stopwatch.ElapsedMilliseconds}"); }
static void Main(string[] args) { var testClass = new JsongTests3(); testClass.First = byte.MaxValue; testClass.Second = byte.MaxValue; testClass.Third = byte.MaxValue; Console.WriteLine(_convert.ToJson(testClass)); Func <JsongTests3, bool> check = toCheck => toCheck.First == byte.MaxValue && toCheck.Second == byte.MaxValue && toCheck.Third == byte.MaxValue; TestFromJson("System.Text.Json", json => System.Text.Json.JsonSerializer.Deserialize <JsongTests3>(json), check, ExpectedJson); TestFromJson("FromJsonSrcGen", json => FromJsonSrcGen(testClass, json), check, ExpectedJson); TestFromJson("FromJsonUtf8", json => FromJsonUtf8(json), check, ExpectedJson); TestToJson("To System.Text.Json", jsonClass => System.Text.Json.JsonSerializer.Serialize <JsongTests3>(jsonClass), testClass, ExpectedJson); TestToJson("ToJsonSrcGen", jsonClass => ToJsonSrcGen(jsonClass), testClass, ExpectedJson); TestToJson("ToJsonUtf8", jsonClass => ToJsonUtf8(jsonClass), testClass, ExpectedJson); }
static string ToJsonUtf8(JsongTests3 jsonString) { var testClass = Utf8Json.JsonSerializer.Serialize <JsongTests3>(jsonString); return(ExpectedJson); //Uft8 json return a byte array, so I will just return the expectedJson so as not to penalize it. }
static JsongTests3 FromJsonSrcGen(JsongTests3 testClass, string jsonString) { _convert.FromJson(testClass, jsonString); return(testClass); }
static string ToJsonSrcGen(JsongTests3 testClass) { return(_convert.ToJson(testClass));//Encoding.UTF8.GetString(bytes, 0, bytes.Length); }