1 #pragma once
2 
3 #ifdef SIMDJSON_COMPETITION_NLOHMANN_JSON
4 
5 #include "large_random.h"
6 
7 namespace large_random {
8 
9 using json = nlohmann::json;
10 
11 struct nlohmann_json_sax {
12     static constexpr diff_flags DiffFlags = diff_flags::NONE;
13 
14     struct Handler : json::json_sax_t
15     {
16         size_t k{0};
17         double buffer[3];
18         std::vector<point>& result;
19 
Handlernlohmann_json_sax::Handler20         Handler(std::vector<point> &r) : result(r) {  }
21 
keynlohmann_json_sax::Handler22         bool key(string_t& val) override {
23             switch(val[0]) {
24                 case 'x':
25                     k = 0;
26                     break;
27                 case 'y':
28                     k = 1;
29                     break;
30                 case 'z':
31                     k = 2;
32                     break;
33             }
34             return true;
35         }
number_unsignednlohmann_json_sax::Handler36         bool number_unsigned(number_unsigned_t val) override {
37             buffer[k] = val;
38             if (k == 2) {
39                 result.emplace_back(json_benchmark::point{buffer[0],buffer[1],buffer[2]});
40                 k = 0;
41             }
42             return true;
43         }
number_floatnlohmann_json_sax::Handler44         bool number_float(number_float_t val, const string_t& s) override {
45             buffer[k] = val;
46             if (k == 2) {
47                 result.emplace_back(json_benchmark::point{buffer[0],buffer[1],buffer[2]});
48                 k = 0;
49             }
50             return true;
51         }
52         // Irrelevant events
nullnlohmann_json_sax::Handler53         bool null() override { return true; }
booleannlohmann_json_sax::Handler54         bool boolean(bool val) override { return true; }
number_integernlohmann_json_sax::Handler55         bool number_integer(number_integer_t val) override { return true; }
stringnlohmann_json_sax::Handler56         bool string(string_t& val) override { return true; }
start_objectnlohmann_json_sax::Handler57         bool start_object(std::size_t elements) override { return true; }
end_objectnlohmann_json_sax::Handler58         bool end_object() override { return true; }
start_arraynlohmann_json_sax::Handler59         bool start_array(std::size_t elements) override { return true; }
end_arraynlohmann_json_sax::Handler60         bool end_array() override { return true; }
binarynlohmann_json_sax::Handler61         bool binary(json::binary_t& val) override { return true; }
parse_errornlohmann_json_sax::Handler62         bool parse_error(std::size_t position, const std::string& last_token, const json::exception& ex) override { return false; }
63     }; // Handler
64 
runnlohmann_json_sax65     bool run(simdjson::padded_string &json, std::vector<point> &result) {
66         Handler handler(result);
67         json::sax_parse(json.data(), &handler);
68         return true;
69     }
70 }; // nlohmann_json_sax
71 BENCHMARK_TEMPLATE(large_random, nlohmann_json_sax)->UseManualTime();
72 } // namespace large_random
73 #endif // SIMDJSON_COMPETITION_NLOHMANN_JSON