Skip to content

Commit

Permalink
Add logic to remove null key-value pairs in trace json (#659)
Browse files Browse the repository at this point in the history
* Add logic to remove null key-value pairs in trace json

* Modify logic to replace null with string null
  • Loading branch information
mchadalavada committed Sep 22, 2023
1 parent 6417c6b commit 39fb385
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions Src/PChecker/CheckerCore/SystematicTesting/TestingEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,36 @@ public string GetReport()
return TestReport.GetText(_checkerConfiguration, "...");
}

/// <summary>
/// Returns an object where the value null is replaced with "null"
/// </summary>
public object RecursivelyReplaceNullWithString(object obj) {
if (obj == null) {
return "null";
}
if (obj is Dictionary<string, object> dictionary) {
var newDictionary = new Dictionary<string, object>();
foreach (var item in dictionary) {
var newVal = RecursivelyReplaceNullWithString(item.Value);
if (newVal != null)
newDictionary[item.Key] = newVal;
}
return newDictionary;
}
else if (obj is List<object> list) {
var newList = new List<object>();
foreach (var item in list) {
var newItem = RecursivelyReplaceNullWithString(item);
if (newItem != null)
newList.Add(newItem);
}
return newList;
}
else {
return obj;
}
}

/// <summary>
/// Tries to emit the testing traces, if any.
/// </summary>
Expand Down Expand Up @@ -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<JsonLogger.Logs.Count; i++) {
JsonLogger.Logs[i].Details.Payload = RecursivelyReplaceNullWithString(JsonLogger.Logs[i].Details.Payload);
}

// Stream directly to the output file while serializing the JSON
using var jsonStreamFile = File.Create(jsonPath);
JsonSerializer.Serialize(jsonStreamFile, JsonLogger.Logs, jsonSerializerConfig);
Expand Down

0 comments on commit 39fb385

Please sign in to comment.