1 #pragma once
2 
3 #ifdef SIMDJSON_COMPETITION_NLOHMANN_JSON
4 
5 #include "distinct_user_id.h"
6 
7 namespace distinct_user_id {
8 
9 using json = nlohmann::json;
10 
11 struct nlohmann_json_sax {
12     struct Handler : json::json_sax_t
13     {
14         std::vector<uint64_t>& result;
15         bool user = false;
16         bool user_id = false;
Handlernlohmann_json_sax::Handler17         Handler(std::vector<uint64_t> &r) : result(r) { }
18 
keynlohmann_json_sax::Handler19         bool key(string_t& val) override {
20             // Assume that valid user/id pairs appear only once in main array of user objects
21             if (user) { // If already found user object, find id key
22                 if (val.compare("id") == 0) { user_id = true; }
23             }
24             else if (val.compare("user") == 0) { user = true; } // Otherwise, find user object
25             return true;
26         }
number_unsignednlohmann_json_sax::Handler27         bool number_unsigned(number_unsigned_t val) override {
28             if (user_id) {
29                 result.emplace_back(val);
30                 user = false;
31                 user_id = false;
32             }
33             return true;
34         }
35         // Irrelevant events
nullnlohmann_json_sax::Handler36         bool null() override { return true; }
booleannlohmann_json_sax::Handler37         bool boolean(bool val) override { return true; }
number_floatnlohmann_json_sax::Handler38         bool number_float(number_float_t val, const string_t& s) override { return true; }
number_integernlohmann_json_sax::Handler39         bool number_integer(number_integer_t val) override { return true; }
stringnlohmann_json_sax::Handler40         bool string(string_t& val) override { return true; }
start_objectnlohmann_json_sax::Handler41         bool start_object(std::size_t elements) override { return true; }
end_objectnlohmann_json_sax::Handler42         bool end_object() override { return true; }
start_arraynlohmann_json_sax::Handler43         bool start_array(std::size_t elements) override { return true; }
end_arraynlohmann_json_sax::Handler44         bool end_array() override { return true; }
binarynlohmann_json_sax::Handler45         bool binary(json::binary_t& val) override { return true; }
parse_errornlohmann_json_sax::Handler46         bool parse_error(std::size_t position, const std::string& last_token, const json::exception& ex) override { return false; }
47     }; // Handler
48 
runnlohmann_json_sax49     bool run(simdjson::padded_string &json, std::vector<uint64_t> &result) {
50         Handler handler(result);
51         json::sax_parse(json.data(), &handler);
52 
53         return true;
54     }
55 }; // nlohmann_json_sax
56 BENCHMARK_TEMPLATE(distinct_user_id, nlohmann_json_sax)->UseManualTime();
57 } // namespace distinct_user_id
58 
59 #endif // SIMDJSON_COMPETITION_NLOHMANN_JSON