Consolix
Loading...
Searching...
No Matches
test_strip_json_comments.cpp
Go to the documentation of this file.
1#include <iostream>
2#include <fstream>
3#include <string>
4#include <stdexcept>
7
12std::string read_file(const std::string& file_path) {
13 std::ifstream file(file_path);
14 if (!file.is_open()) {
15 throw std::runtime_error("Failed to open file: " + file_path);
16 }
17 return std::string((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
18}
19
24void write_file(const std::string& file_path, const std::string& content) {
25 std::ofstream file(file_path);
26 if (!file.is_open()) {
27 throw std::runtime_error("Failed to write to file: " + file_path);
28 }
29 file << content;
30}
31
32int main() {
33 try {
34 // Пути к файлам
35 const std::string input_file = "test_input.json";
36 const std::string output_file_base = "test_output";
37
38 // Чтение входного файла
39 std::string json_string = read_file(consolix::resolve_exec_path(input_file));
40
41 // Проверка различных вариантов настроек
42 for (bool with_whitespace : {false, true}) {
43 for (bool preserve_newlines : {false, true}) {
44 // Применяем функцию strip_json_comments
45 std::string result = consolix::strip_json_comments(json_string, with_whitespace, preserve_newlines);
46
47 // Формируем имя выходного файла
48 std::string output_file = output_file_base +
49 (with_whitespace ? "_whitespace" : "_no_whitespace") +
50 (preserve_newlines ? "_preserve_newlines" : "_no_newlines") +
51 ".json";
52
53 // Записываем результат в файл
54 write_file(consolix::resolve_exec_path(output_file), result);
55
56 // Отладочный вывод
57 std::cout << "Processed with "
58 << "with_whitespace=" << with_whitespace << ", "
59 << "preserve_newlines=" << preserve_newlines << "\n"
60 << "Result saved to: " << output_file << std::endl;
61 }
62 }
63
64 std::cout << "All tests completed successfully!" << std::endl;
65
66 } catch (const std::exception& e) {
67 std::cerr << "Error: " << e.what() << std::endl;
68 return 1;
69 }
70
71 return 0;
72}
Utilities for working with JSON strings, including removing comments.
*The resulting JSON string may optionally retain whitespace or newlines *where the comments were removed **param json_string The JSON string to process *param with_whitespace If comments are replaced with equivalent whitespace *If comments are removed without leaving whitespace *param preserve_newlines If true and with_whitespace is newline characters *in comments are preserved all characters in the *comments are replaced with whitespace *return A JSON string with comments removed *std::string strip_json_comments(const std::string &json_string, bool with_whitespace=false, bool preserve_newlines=true)
Utilities for working with file and directory paths, including resolving paths relative to the execut...
std::string read_file(const std::string &file_path)
Читает содержимое файла в строку.
void write_file(const std::string &file_path, const std::string &content)
Записывает строку в файл.