-
-
Notifications
You must be signed in to change notification settings - Fork 477
Expand file tree
/
Copy pathjson-writer.hpp
More file actions
121 lines (104 loc) · 2.79 KB
/
json-writer.hpp
File metadata and controls
121 lines (104 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#ifndef OSM2PGSQL_JSON_WRITER_HPP
#define OSM2PGSQL_JSON_WRITER_HPP
/**
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This file is part of osm2pgsql (https://osm2pgsql.org/).
*
* Copyright (C) 2006-2026 by the osm2pgsql developer community.
* For a full list of authors see the git log.
*/
#include "format.hpp"
#include <cassert>
#include <cmath>
#include <iterator>
#include <string>
#include <type_traits>
class json_writer_t
{
public:
void null() { m_buffer.append("null"); }
void boolean(bool value) { m_buffer.append(value ? "true" : "false"); }
template <typename T,
std::enable_if_t<std::is_floating_point_v<T>, bool> = true>
void number(T value)
{
if (std::isfinite(value)) {
m_buffer += fmt::to_string(value);
} else {
null();
}
}
template <typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
void number(T value)
{
m_buffer += fmt::to_string(value);
}
void string(char const *str)
{
m_buffer += '"';
while (auto const c = *str++) {
switch (c) {
case '\b':
m_buffer.append("\\b");
break;
case '\f':
m_buffer.append("\\f");
break;
case '\n':
m_buffer.append("\\n");
break;
case '\r':
m_buffer.append("\\r");
break;
case '\t':
m_buffer.append("\\t");
break;
case '"':
m_buffer.append("\\\"");
break;
case '\\':
m_buffer.append("\\\\");
break;
default:
if (static_cast<unsigned char>(c) <= 0x1fU) {
m_buffer.append(fmt::format(R"(\u{:04x})",
static_cast<unsigned char>(c)));
} else {
m_buffer += c;
}
}
}
m_buffer += '"';
}
void key(char const *key)
{
string(key);
m_buffer += ':';
}
void start_object() { m_buffer += '{'; }
void end_object()
{
assert(!m_buffer.empty());
if (m_buffer.back() == ',') {
m_buffer.back() = '}';
} else {
m_buffer += '}';
}
}
void start_array() { m_buffer += '['; }
void end_array()
{
assert(!m_buffer.empty());
if (m_buffer.back() == ',') {
m_buffer.back() = ']';
} else {
m_buffer += ']';
}
}
void next() { m_buffer += ','; }
std::string const &json() const noexcept { return m_buffer; }
private:
std::string m_buffer;
};
#endif // OSM2PGSQL_JSON_WRITER_HPP