Skip to content

Add a --omit-nulls option to the JSON export #2004

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

Merged
merged 6 commits into from
Jan 6, 2022

Conversation

dominiklohmann
Copy link
Member

This adds the vast.export.json.omit-nulls option that changes the JSON writer to skip over fields in JSON objects whose value is null.

Implementation-wise this turned out to be a bit more complicated than I had anticipated, as it required changes to both the JSON printer and the ostream writer. I re-used the same policy template argument for both.

📝 Checklist

  • All user-facing changes have changelog entries.
  • The changes are reflected on docs.tenzir.com/vast, if necessary.
  • The PR description contains instructions for the reviewer, if necessary.

🎯 Review Instructions

First commit contains the code changes, which should be straightforward. The second commit contains a new integration test, which exports the same data as the test right before it, except that nulls are omitted.

Please also give feedback on the --omit-nulls naming; maybe there's a more intuitive name out there, or something that other tools already expose that we can re-use such that users may already be familiar with the flag.

Note that we should only merge this after the December release, as we're in feature freeze as of writing this.

This adds the `vast.export.json.omit-nulls` option that changes the JSON
writer to skip over fields in JSON objects whose value is `null`.

Implementation-wise this turned out to be a bit more complicated than I
had anticipated, as it required changes to both the JSON printer and the
ostream writer. I re-used the same policy template argument for both.
@dominiklohmann dominiklohmann added feature New functionality blocked Blocked by an (external) issue labels Dec 11, 2021
@dominiklohmann dominiklohmann requested a review from a team December 11, 2021 13:21
@dominiklohmann dominiklohmann removed the blocked Blocked by an (external) issue label Dec 16, 2021
@tobim
Copy link
Member

tobim commented Jan 5, 2022

I added a unit test as a tool to try to simplify the json printable part:

diff --git a/libvast/test/format/writer.cpp b/libvast/test/format/writer.cpp
index 5623b9a0f..4f364ebdd 100644
--- a/libvast/test/format/writer.cpp
+++ b/libvast/test/format/writer.cpp
@@ -32,16 +32,18 @@ auto first_csv_http_log_line = "type,ts,uid,id.orig_h,id.orig_p,id.resp_h,id.res
 auto last_csv_http_log_line = R"__(zeek.http,2009-11-19T07:17:28.829955072,"rydI6puScNa",192.168.1.104,1224,87.106.66.233,80,1,"POST","87.106.66.233","/rpc.html?e=bl",,"SCSDK-6.0.0",1064,96,200,"OK",100,"Continue",,"",,,,"application/octet-stream",,)__";
 
 auto first_zeek_conn_log_line = R"__({"ts": "2009-11-18T08:00:21.486539008", "uid": "Pii6cUUq1v4", "id.orig_h": "192.168.1.102", "id.orig_p": 68, "id.resp_h": "192.168.1.1", "id.resp_p": 67, "proto": "udp", "service": null, "duration": "163.82ms", "orig_bytes": 301, "resp_bytes": 300, "conn_state": "SF", "local_orig": null, "missed_bytes": 0, "history": "Dd", "orig_pkts": 1, "orig_ip_bytes": 329, "resp_pkts": 1, "resp_ip_bytes": 328, "tunnel_parents": []})__";
+
+auto last_zeek_http_log_line_json = R"__({"ts": "2009-11-19T07:17:28.829955072", "uid": "rydI6puScNa", "id.orig_h": "192.168.1.104", "id.orig_p": 1224, "id.resp_h": "87.106.66.233", "id.resp_p": 80, "trans_depth": 1, "method": "POST", "host": "87.106.66.233", "uri": "/rpc.html?e=bl", "user_agent": "SCSDK-6.0.0", "request_body_len": 1064, "response_body_len": 96, "status_code": 200, "status_msg": "OK", "info_code": 100, "info_msg": "Continue", "tags": [], "mime_type": "application/octet-stream"})__";
 // clang-format on
 
 template <class Writer>
-std::vector<std::string> generate(const std::vector<table_slice>& xs) {
+std::vector<std::string>
+generate(const std::vector<table_slice>& xs, caf::settings options = {}) {
   std::string str;
   caf::containerbuf<std::string> sb{str};
   auto out = std::make_unique<std::ostream>(&sb);
-  caf::settings options;
   Writer writer{std::move(out), options};
-  for (auto& x : xs)
+  for (const auto& x : xs)
     if (auto err = writer.write(x))
       FAIL("failed to write event");
   writer.flush();
@@ -64,9 +66,16 @@ TEST(CSV writer) {
   CHECK_EQUAL(lines.back(), last_csv_http_log_line);
 }
 
-TEST(JSON writer) {
+TEST(JSON writer - defaults) {
   auto lines = generate<format::json::writer>(zeek_conn_log);
   CHECK_EQUAL(lines.front(), first_zeek_conn_log_line);
 }
 
+TEST(JSON writer - omit-nulls) {
+  caf::settings options;
+  caf::put(options, "vast.export.json.omit-nulls", true);
+  auto lines = generate<format::json::writer>(zeek_http_log, options);
+  CHECK_EQUAL(lines.back(), last_zeek_http_log_line_json);
+}
+
 FIXTURE_SCOPE_END()

@dominiklohmann
Copy link
Member Author

I added a unit test as a tool to try to simplify the json printable part:

@tobim Feel free to push this directly to the branch so it's properly signed. Thanks!

@tobim
Copy link
Member

tobim commented Jan 5, 2022

I just added some tests and tried to simplify the record to object formatting.

@dominiklohmann
Copy link
Member Author

dominiklohmann commented Jan 6, 2022

I approve of your unit tests and of the writer simplification @tobim—if you approve of my changes then I think this can be merged.

Copy link
Member

@tobim tobim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked at the implementation in detail and contributed additional tests to ensure my understanding is correct.

@dominiklohmann dominiklohmann merged commit b5181fe into master Jan 6, 2022
@dominiklohmann dominiklohmann deleted the story/sc-29955/omit-null-fields branch January 6, 2022 08:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants