hoshi-lang dev
Yet another programming language
Loading...
Searching...
No Matches
jsonlib.cpp
Go to the documentation of this file.
1#include <iostream>
2#include <fstream>
3#include <sstream>
4#include <string>
5#include <nlohmann/json.hpp>
6
7// Use the library namespace
8using json = nlohmann::json;
9
10// Equivalent to the iteration function
11void iteration(const std::string& s) {
12 try {
13 // Parse the string into a json object
14 auto j = json::parse(s);
15 } catch (json::parse_error& e) {
16 // Handle potential parse errors (optional, for safety)
17 }
18}
19
20int main() {
21 const std::string path = "benchmark/json/test.json";
22
23 // 1. Open the file
24 std::ifstream file(path);
25 if (!file.is_open()) {
26 std::cerr << "Could not open file " << path << std::endl;
27 return 1;
28 }
29
30 // 2. Read file content into a string
31 std::stringstream buffer;
32 buffer << file.rdbuf();
33 std::string s = buffer.str();
34
35 // 3. Close the file (handled automatically by ifstream destructor,
36 // but explicit call mimics the original code)
37 file.close();
38
39 // 4. Print the string
40 std::cout << s << "\n";
41
42 // 5. Benchmark loop
43 for (int i = 0; i < 1000000; ++i) {
44 iteration(s);
45 }
46
47 return 0;
48}
namespace for Niels Lohmann
Definition json.hpp:20114
exception indicating a parse error
Definition json.hpp:4627
void iteration(const std::string &s)
Definition jsonlib.cpp:11
int main()
Definition jsonlib.cpp:20