From 39fb3859f5bf3913aee749bfbb0241dc24adeee1 Mon Sep 17 00:00:00 2001 From: mchadalavada <140562632+mchadalavada@users.noreply.github.com> Date: Fri, 22 Sep 2023 14:36:13 -0700 Subject: [PATCH] Add logic to remove null key-value pairs in trace json (#659) * Add logic to remove null key-value pairs in trace json * Modify logic to replace null with string null --- .../SystematicTesting/TestingEngine.cs | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/Src/PChecker/CheckerCore/SystematicTesting/TestingEngine.cs b/Src/PChecker/CheckerCore/SystematicTesting/TestingEngine.cs index a72cfbdd8..b86a3852a 100644 --- a/Src/PChecker/CheckerCore/SystematicTesting/TestingEngine.cs +++ b/Src/PChecker/CheckerCore/SystematicTesting/TestingEngine.cs @@ -591,6 +591,36 @@ public string GetReport() return TestReport.GetText(_checkerConfiguration, "..."); } + /// + /// Returns an object where the value null is replaced with "null" + /// + public object RecursivelyReplaceNullWithString(object obj) { + if (obj == null) { + return "null"; + } + if (obj is Dictionary dictionary) { + var newDictionary = new Dictionary(); + foreach (var item in dictionary) { + var newVal = RecursivelyReplaceNullWithString(item.Value); + if (newVal != null) + newDictionary[item.Key] = newVal; + } + return newDictionary; + } + else if (obj is List list) { + var newList = new List(); + foreach (var item in list) { + var newItem = RecursivelyReplaceNullWithString(item); + if (newItem != null) + newList.Add(newItem); + } + return newList; + } + else { + return obj; + } + } + /// /// Tries to emit the testing traces, if any. /// @@ -640,6 +670,11 @@ public void TryEmitTraces(string directory, string file) var jsonPath = directory + file + "_" + index + ".trace.json"; Logger.WriteLine($"..... Writing {jsonPath}"); + // Remove the null objects from payload recursively for each log event + for(int i=0; i