Janus 2.0.0
High-performance C++20 dual-mode numerical framework
Loading...
Searching...
No Matches
JsonUtils.hpp
Go to the documentation of this file.
1
3#pragma once
4
6#include <algorithm>
7#include <fstream>
8#include <iomanip>
9#include <iostream>
10#include <map>
11#include <sstream>
12#include <string>
13#include <vector>
14
15namespace janus::utils {
16
22inline void write_json(const std::string &filename,
23 const std::map<std::string, std::vector<double>> &data) {
24 std::ofstream file(filename);
25 if (!file.is_open()) {
26 throw janus::RuntimeError("Could not open file for writing: " + filename);
27 }
28
29 file << "{\n";
30 auto it = data.begin();
31 while (it != data.end()) {
32 file << " \"" << it->first << "\": [";
33 const auto &vec = it->second;
34 for (size_t i = 0; i < vec.size(); ++i) {
35 file << std::scientific << std::setprecision(16) << vec[i];
36 if (i < vec.size() - 1) {
37 file << ", ";
38 }
39 }
40 file << "]";
41
42 if (++it != data.end()) {
43 file << ",";
44 }
45 file << "\n";
46 }
47 file << "}\n";
48}
49
55inline std::map<std::string, std::vector<double>> read_json(const std::string &filename) {
56 std::ifstream file(filename);
57 if (!file.is_open()) {
58 throw janus::RuntimeError("Could not open file for reading: " + filename);
59 }
60
61 std::stringstream buffer;
62 buffer << file.rdbuf();
63 std::string content = buffer.str();
64
65 std::map<std::string, std::vector<double>> data;
66
67 // Very naive parser
68 size_t pos = 0;
69 while (pos < content.length()) {
70 // Find key
71 size_t quote_start = content.find('"', pos);
72 if (quote_start == std::string::npos)
73 break;
74
75 size_t quote_end = content.find('"', quote_start + 1);
76 if (quote_end == std::string::npos)
77 throw janus::RuntimeError("Malformed JSON: Unclosed string");
78
79 std::string key = content.substr(quote_start + 1, quote_end - quote_start - 1);
80 pos = quote_end + 1;
81
82 // Find colon
83 size_t colon = content.find(':', pos);
84 if (colon == std::string::npos)
85 throw janus::RuntimeError("Malformed JSON: Missing colon");
86 pos = colon + 1;
87
88 // Find value (array)
89 size_t bracket_start = content.find('[', pos);
90 if (bracket_start == std::string::npos)
91 throw janus::RuntimeError("Malformed JSON: Missing array start [");
92 pos = bracket_start + 1;
93
94 size_t bracket_end = content.find(']', pos);
95 if (bracket_end == std::string::npos)
96 throw janus::RuntimeError("Malformed JSON: Missing array end ]");
97
98 // Parse array content
99 std::string array_content = content.substr(pos, bracket_end - pos);
100 std::vector<double> vec;
101 std::stringstream ss(array_content);
102 std::string number_str;
103
104 while (std::getline(ss, number_str, ',')) {
105 // Trim whitespace
106 number_str.erase(0, number_str.find_first_not_of(" \t\n\r"));
107 number_str.erase(number_str.find_last_not_of(" \t\n\r") + 1);
108 if (!number_str.empty()) {
109 try {
110 vec.push_back(std::stod(number_str));
111 } catch (const std::exception &e) {
112 throw janus::RuntimeError("Malformed JSON: could not parse number '" +
113 number_str + "': " + e.what());
114 }
115 }
116 }
117 data[key] = vec;
118
119 pos = bracket_end + 1;
120 }
121
122 return data;
123}
124
125} // namespace janus::utils
Custom exception hierarchy for Janus framework.
Operation failed at runtime (e.g., CasADi eval with free variables).
Definition JanusError.hpp:41
Definition JsonUtils.hpp:15
std::map< std::string, std::vector< double > > read_json(const std::string &filename)
Parse a flat JSON file into a string-to-vector<double> map.
Definition JsonUtils.hpp:55
void write_json(const std::string &filename, const std::map< std::string, std::vector< double > > &data)
Write a map of string-to-vector<double> as JSON.
Definition JsonUtils.hpp:22