Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logic to remove null key-value pairs in trace json #659

Merged
merged 2 commits into from
Sep 22, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 keys with null values are removed
/// </summary>
public object RecursivelyRemoveNullValueKeys(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 = RecursivelyRemoveNullValueKeys(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 = RecursivelyRemoveNullValueKeys(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 = RecursivelyRemoveNullValueKeys(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