Alamo
JSON.H
Go to the documentation of this file.
1#ifndef IO_JSON_H
2#define IO_JSON_H
3
4#include <ostream>
5#include <source_location>
6#include <string>
7#include <vector>
8
9namespace IO
10{
11class JSON
12{
13public:
14 static std::string Escape(const std::string &value)
15 {
16 std::string ret;
17 for (char c : value)
18 {
19 switch (c)
20 {
21 case 92: ret += "\\\\"; break;
22 case 34: ret += "\\\""; break;
23 case 8: ret += "\\b"; break;
24 case 12: ret += "\\f"; break;
25 case 10: ret += "\\n"; break;
26 case 13: ret += "\\r"; break;
27 case 9: ret += "\\t"; break;
28 default: ret += c; break;
29 }
30 }
31 return ret;
32 }
33
34 static void Indent(std::ostream &os, int indent)
35 {
36 for (int i = 0; i < indent; i++) os << " ";
37 }
38
39 static void Comma(std::ostream &os, bool &first, int indent)
40 {
41 if (!first) os << ",\n";
42 first = false;
43 Indent(os, indent);
44 }
45
46 static void WriteStringField( std::ostream &os, bool &first, int indent,
47 const std::string &name, const std::string &value)
48 {
49 Comma(os, first, indent);
50 os << "\"" << name << "\": \"" << Escape(value) << "\"";
51 }
52
53 static void WriteBoolField( std::ostream &os, bool &first, int indent,
54 const std::string &name, bool value)
55 {
56 Comma(os, first, indent);
57 os << "\"" << name << "\": " << (value ? "true" : "false");
58 }
59
60 static void WriteIntField( std::ostream &os, bool &first, int indent,
61 const std::string &name, int value)
62 {
63 Comma(os, first, indent);
64 os << "\"" << name << "\": " << value;
65 }
66
67 static void WriteStringArray(std::ostream &os, const std::vector<std::string> &values)
68 {
69 os << "[";
70 for (std::size_t i = 0; i < values.size(); i++)
71 {
72 if (i) os << ", ";
73 os << "\"" << Escape(values[i]) << "\"";
74 }
75 os << "]";
76 }
77
78 static void WriteStringArrayField( std::ostream &os, bool &first, int indent,
79 const std::string &name,
80 const std::vector<std::string> &values)
81 {
82 Comma(os, first, indent);
83 os << "\"" << name << "\": ";
84 WriteStringArray(os, values);
85 }
86
87 static void WriteSourceField( std::ostream &os, bool &first, int indent,
88 const std::source_location &location)
89 {
90 Comma(os, first, indent);
91 os << "\"source\": {";
92 os << "\"file\": \"" << Escape(location.file_name()) << "\", ";
93 os << "\"line\": " << location.line() << ", ";
94 os << "\"function\": \"" << Escape(location.function_name()) << "\"";
95 os << "}";
96 }
97};
98}
99
100#endif
static void WriteStringField(std::ostream &os, bool &first, int indent, const std::string &name, const std::string &value)
Definition JSON.H:46
static void Comma(std::ostream &os, bool &first, int indent)
Definition JSON.H:39
static void WriteBoolField(std::ostream &os, bool &first, int indent, const std::string &name, bool value)
Definition JSON.H:53
static void WriteSourceField(std::ostream &os, bool &first, int indent, const std::source_location &location)
Definition JSON.H:87
static std::string Escape(const std::string &value)
Definition JSON.H:14
static void WriteIntField(std::ostream &os, bool &first, int indent, const std::string &name, int value)
Definition JSON.H:60
static void Indent(std::ostream &os, int indent)
Definition JSON.H:34
static void WriteStringArrayField(std::ostream &os, bool &first, int indent, const std::string &name, const std::vector< std::string > &values)
Definition JSON.H:78
static void WriteStringArray(std::ostream &os, const std::vector< std::string > &values)
Definition JSON.H:67