PACS System 0.1.0
PACS DICOM system library
Loading...
Searching...
No Matches
kcenon::pacs::ai::json_util Namespace Reference

Functions

std::string escape_string (std::string_view str)
 Escape special characters in JSON string.
 
std::string to_iso8601 (std::chrono::system_clock::time_point tp)
 Convert time_point to ISO 8601 string.
 
std::optional< std::chrono::system_clock::time_point > from_iso8601 (const std::string &str)
 Parse ISO 8601 string to time_point.
 
std::optional< std::string > extract_string (const std::string &json, const std::string &key)
 Simple JSON value extractor (for basic parsing)
 
std::optional< int > extract_int (const std::string &json, const std::string &key)
 Simple JSON integer extractor.
 
std::string build_request_json (const inference_request &request)
 Build inference request JSON.
 

Function Documentation

◆ build_request_json()

std::string kcenon::pacs::ai::json_util::build_request_json ( const inference_request & request)
inlinenodiscard

◆ escape_string()

std::string kcenon::pacs::ai::json_util::escape_string ( std::string_view str)
inlinenodiscard

Escape special characters in JSON string.

Definition at line 61 of file ai_service_connector.cpp.

61 {
62 std::string result;
63 result.reserve(str.size() + 10);
64
65 for (char c : str) {
66 switch (c) {
67 case '"':
68 result += "\\\"";
69 break;
70 case '\\':
71 result += "\\\\";
72 break;
73 case '\b':
74 result += "\\b";
75 break;
76 case '\f':
77 result += "\\f";
78 break;
79 case '\n':
80 result += "\\n";
81 break;
82 case '\r':
83 result += "\\r";
84 break;
85 case '\t':
86 result += "\\t";
87 break;
88 default:
89 if (static_cast<unsigned char>(c) < 0x20) {
90 char buf[8];
91 std::snprintf(buf, sizeof(buf), "\\u%04x",
92 static_cast<unsigned char>(c));
93 result += buf;
94 } else {
95 result += c;
96 }
97 break;
98 }
99 }
100
101 return result;
102}

Referenced by build_request_json(), kcenon::pacs::ai::if(), and kcenon::pacs::ai::if().

Here is the caller graph for this function:

◆ extract_int()

std::optional< int > kcenon::pacs::ai::json_util::extract_int ( const std::string & json,
const std::string & key )
inlinenodiscard

Simple JSON integer extractor.

Definition at line 179 of file ai_service_connector.cpp.

181 {
182 std::string search_key = "\"" + key + "\"";
183 auto pos = json.find(search_key);
184 if (pos == std::string::npos) {
185 return std::nullopt;
186 }
187
188 pos = json.find(':', pos);
189 if (pos == std::string::npos) {
190 return std::nullopt;
191 }
192
193 // Skip whitespace
194 pos++;
195 while (pos < json.size() && std::isspace(json[pos])) {
196 pos++;
197 }
198
199 if (pos >= json.size()) {
200 return std::nullopt;
201 }
202
203 try {
204 std::size_t end_pos;
205 int value = std::stoi(json.substr(pos), &end_pos);
206 return value;
207 } catch (...) {
208 return std::nullopt;
209 }
210}

Referenced by parse_status_json().

Here is the caller graph for this function:

◆ extract_string()

std::optional< std::string > kcenon::pacs::ai::json_util::extract_string ( const std::string & json,
const std::string & key )
inlinenodiscard

Simple JSON value extractor (for basic parsing)

Definition at line 148 of file ai_service_connector.cpp.

150 {
151 std::string search_key = "\"" + key + "\"";
152 auto pos = json.find(search_key);
153 if (pos == std::string::npos) {
154 return std::nullopt;
155 }
156
157 pos = json.find(':', pos);
158 if (pos == std::string::npos) {
159 return std::nullopt;
160 }
161
162 pos = json.find('"', pos);
163 if (pos == std::string::npos) {
164 return std::nullopt;
165 }
166
167 auto start = pos + 1;
168 auto end = json.find('"', start);
169 if (end == std::string::npos) {
170 return std::nullopt;
171 }
172
173 return json.substr(start, end - start);
174}

Referenced by parse_model_json(), and parse_status_json().

Here is the caller graph for this function:

◆ from_iso8601()

std::optional< std::chrono::system_clock::time_point > kcenon::pacs::ai::json_util::from_iso8601 ( const std::string & str)
inlinenodiscard

Parse ISO 8601 string to time_point.

Definition at line 127 of file ai_service_connector.cpp.

127 {
128 std::tm tm_val{};
129 std::istringstream iss(str);
130 iss >> std::get_time(&tm_val, "%Y-%m-%dT%H:%M:%SZ");
131
132 if (iss.fail()) {
133 return std::nullopt;
134 }
135
136#if defined(_MSC_VER)
137 auto time_t_val = _mkgmtime(&tm_val);
138#else
139 auto time_t_val = timegm(&tm_val);
140#endif
141
142 return std::chrono::system_clock::from_time_t(time_t_val);
143}

Referenced by parse_status_json().

Here is the caller graph for this function:

◆ to_iso8601()

std::string kcenon::pacs::ai::json_util::to_iso8601 ( std::chrono::system_clock::time_point tp)
inlinenodiscard

Convert time_point to ISO 8601 string.

Definition at line 107 of file ai_service_connector.cpp.

108 {
109 const auto time_t_val = std::chrono::system_clock::to_time_t(tp);
110 std::tm tm_val{};
111
112#if defined(_MSC_VER)
113 gmtime_s(&tm_val, &time_t_val);
114#else
115 gmtime_r(&time_t_val, &tm_val);
116#endif
117
118 std::ostringstream oss;
119 oss << std::put_time(&tm_val, "%Y-%m-%dT%H:%M:%SZ");
120 return oss.str();
121}