-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathJSONSerialization.cpp
235 lines (202 loc) · 5.61 KB
/
JSONSerialization.cpp
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
//===--- JSONSerialization.cpp - JSON serialization support ---------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/Basic/JSONSerialization.h"
#include "llvm/Support/Format.h"
using namespace swift::json;
using namespace swift;
unsigned Output::beginArray() {
StateStack.push_back(ArrayFirstValue);
Stream << '[';
if (PrettyPrint) {
Stream << '\n';
}
return 0;
}
bool Output::preflightElement(unsigned, void *&) {
if (StateStack.back() != ArrayFirstValue) {
assert(StateStack.back() == ArrayOtherValue && "We must be in a sequence!");
Stream << ',';
if (PrettyPrint)
Stream << '\n';
}
if (PrettyPrint)
indent();
return true;
}
void Output::postflightElement(void*) {
if (StateStack.back() == ArrayFirstValue) {
StateStack.pop_back();
StateStack.push_back(ArrayOtherValue);
}
}
void Output::endArray() {
StateStack.pop_back();
if (PrettyPrint) {
Stream << '\n';
indent();
}
Stream << ']';
}
bool Output::canElideEmptyArray() {
if (StateStack.size() < 2)
return true;
if (StateStack.back() != ObjectFirstKey)
return true;
State checkedState = StateStack[StateStack.size() - 2];
return (checkedState != ArrayFirstValue && checkedState != ArrayOtherValue);
}
void Output::beginObject() {
StateStack.push_back(ObjectFirstKey);
Stream << "{";
if (PrettyPrint)
Stream << '\n';
}
void Output::endObject() {
StateStack.pop_back();
if (PrettyPrint) {
Stream << '\n';
indent();
}
Stream << "}";
}
bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault,
bool &UseDefault, void *&) {
UseDefault = false;
if (Required || !SameAsDefault) {
if (StateStack.back() != ObjectFirstKey) {
assert(StateStack.back() == ObjectOtherKey && "We must be in an object!");
Stream << ',';
if (PrettyPrint)
Stream << '\n';
}
if (PrettyPrint)
indent();
Stream << '"' << Key << "\":";
if (PrettyPrint)
Stream << ' ';
return true;
}
return false;
}
void Output::postflightKey(void*) {
if (StateStack.back() == ObjectFirstKey) {
StateStack.pop_back();
StateStack.push_back(ObjectOtherKey);
}
}
void Output::beginEnumScalar() {
EnumerationMatchFound = false;
}
bool Output::matchEnumScalar(const char *Str, bool Match) {
if (Match && !EnumerationMatchFound) {
StringRef StrRef(Str);
scalarString(StrRef, true);
EnumerationMatchFound = true;
}
return false;
}
void Output::endEnumScalar() {
if (!EnumerationMatchFound)
llvm_unreachable("bad runtime enum value");
}
bool Output::beginBitSetScalar(bool &DoClear) {
Stream << '[';
if (PrettyPrint)
Stream << ' ';
NeedBitValueComma = false;
DoClear = false;
return true;
}
bool Output::bitSetMatch(const char *Str, bool Matches) {
if (Matches) {
if (NeedBitValueComma) {
Stream << ',';
if (PrettyPrint)
Stream << ' ';
}
StringRef StrRef(Str);
scalarString(StrRef, true);
}
return false;
}
void Output::endBitSetScalar() {
if (PrettyPrint)
Stream << ' ';
Stream << ']';
}
void Output::scalarString(StringRef &S, bool MustQuote) {
if (MustQuote) {
Stream << '"';
Stream.write_escaped(S);
Stream << '"';
}
else
Stream << S;
}
void Output::indent() {
Stream.indent(StateStack.size() * 2);
}
//===----------------------------------------------------------------------===//
// traits for built-in types
//===----------------------------------------------------------------------===//
void ScalarTraits<bool>::output(const bool &Val, raw_ostream &Out) {
Out << (Val ? "true" : "false");
}
void ScalarTraits<StringRef>::output(const StringRef &Val,
raw_ostream &Out) {
Out << Val;
}
void ScalarTraits<std::string>::output(const std::string &Val,
raw_ostream &Out) {
Out << Val;
}
void ScalarTraits<uint8_t>::output(const uint8_t &Val,
raw_ostream &Out) {
// use temp uin32_t because ostream thinks uint8_t is a character
uint32_t Num = Val;
Out << Num;
}
void ScalarTraits<uint16_t>::output(const uint16_t &Val,
raw_ostream &Out) {
Out << Val;
}
void ScalarTraits<uint32_t>::output(const uint32_t &Val,
raw_ostream &Out) {
Out << Val;
}
void ScalarTraits<uint64_t>::output(const uint64_t &Val,
raw_ostream &Out) {
Out << Val;
}
void ScalarTraits<int8_t>::output(const int8_t &Val, raw_ostream &Out) {
// use temp in32_t because ostream thinks int8_t is a character
int32_t Num = Val;
Out << Num;
}
void ScalarTraits<int16_t>::output(const int16_t &Val,
raw_ostream &Out) {
Out << Val;
}
void ScalarTraits<int32_t>::output(const int32_t &Val,
raw_ostream &Out) {
Out << Val;
}
void ScalarTraits<int64_t>::output(const int64_t &Val,
raw_ostream &Out) {
Out << Val;
}
void ScalarTraits<double>::output(const double &Val, raw_ostream &Out) {
Out << llvm::format("%g", Val);
}
void ScalarTraits<float>::output(const float &Val, raw_ostream &Out) {
Out << llvm::format("%g", Val);
}