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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
|
//===--- JSONSerialization.cpp - JSON serialization support ---------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/Basic/JSONSerialization.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/Format.h"
using namespace swift::json;
using namespace swift;
unsigned Output::beginArray() {
StateStack.push_back(ArrayFirstValue);
Stream << '[';
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';
indent();
}
return true;
}
void Output::postflightElement(void*) {
if (StateStack.back() == ArrayFirstValue) {
StateStack.pop_back();
StateStack.push_back(ArrayOtherValue);
}
}
void Output::endArray() {
bool HadContent = StateStack.back() != ArrayFirstValue;
StateStack.pop_back();
if (PrettyPrint && HadContent) {
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 << "{";
}
void Output::endObject() {
bool HadContent = StateStack.back() != ObjectFirstKey;
StateStack.pop_back();
if (PrettyPrint && HadContent) {
Stream << '\n';
indent();
}
Stream << "}";
}
bool Output::preflightKey(llvm::StringRef 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';
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) {
llvm::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 << ' ';
}
llvm::StringRef StrRef(Str);
scalarString(StrRef, true);
}
return false;
}
void Output::endBitSetScalar() {
if (PrettyPrint)
Stream << ' ';
Stream << ']';
}
void Output::scalarString(llvm::StringRef &S, bool MustQuote) {
if (MustQuote) {
Stream << '"';
for (unsigned char c : S) {
// According to the JSON standard, the following characters must be
// escaped:
// - Quotation mark (U+0022)
// - Reverse solidus (U+005C)
// - Control characters (U+0000 to U+001F)
// We need to check for these and escape them if present.
//
// Since these are represented by a single byte in UTF8 (and will not be
// present in any multi-byte UTF8 representations), we can just switch on
// the value of the current byte.
//
// Any other bytes present in the string should therefore be emitted
// as-is, without any escaping.
switch (c) {
// First, check for characters for which JSON has custom escape sequences.
case '"':
Stream << '\\' << '"';
break;
case '\\':
Stream << '\\' << '\\';
break;
case '/':
Stream << '\\' << '/';
break;
case '\b':
Stream << '\\' << 'b';
break;
case '\f':
Stream << '\\' << 'f';
break;
case '\n':
Stream << '\\' << 'n';
break;
case '\r':
Stream << '\\' << 'r';
break;
case '\t':
Stream << '\\' << 't';
break;
default:
// Otherwise, check to see if the current byte is a control character.
if (c <= '\x1F') {
// Since we have a control character, we need to escape it using
// JSON's only valid escape sequence: \uxxxx (where x is a hex digit).
// The upper two digits for control characters are always 00.
Stream << "\\u00";
// Convert the current character into hexadecimal digits.
Stream << llvm::hexdigit((c >> 4) & 0xF);
Stream << llvm::hexdigit((c >> 0) & 0xF);
} else {
// This isn't a control character, so we don't need to escape it.
// As a result, emit it directly; if it's part of a multi-byte UTF8
// representation, all bytes will be emitted in this fashion.
Stream << c;
}
break;
}
}
Stream << '"';
}
else
Stream << S;
}
void Output::null() {
Stream << "null";
}
void Output::indent() {
Stream.indent(StateStack.size() * 2);
}
//===----------------------------------------------------------------------===//
// traits for built-in types
//===----------------------------------------------------------------------===//
llvm::StringRef ScalarReferenceTraits<bool>::stringRef(const bool &Val) {
return (Val ? "true" : "false");
}
llvm::StringRef
ScalarReferenceTraits<llvm::StringRef>::stringRef(const llvm::StringRef &Val) {
return Val;
}
llvm::StringRef
ScalarReferenceTraits<std::string>::stringRef(const std::string &Val) {
return Val;
}
void ScalarTraits<uint8_t>::output(const uint8_t &Val, llvm::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,
llvm::raw_ostream &Out) {
Out << Val;
}
void ScalarTraits<uint32_t>::output(const uint32_t &Val,
llvm::raw_ostream &Out) {
Out << Val;
}
#if defined(_MSC_VER)
void ScalarTraits<unsigned long>::output(const unsigned long &Val,
llvm::raw_ostream &Out) {
Out << Val;
}
#endif
void ScalarTraits<uint64_t>::output(const uint64_t &Val,
llvm::raw_ostream &Out) {
Out << Val;
}
void ScalarTraits<int8_t>::output(const int8_t &Val, llvm::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, llvm::raw_ostream &Out) {
Out << Val;
}
void ScalarTraits<int32_t>::output(const int32_t &Val, llvm::raw_ostream &Out) {
Out << Val;
}
void ScalarTraits<int64_t>::output(const int64_t &Val, llvm::raw_ostream &Out) {
Out << Val;
}
void ScalarTraits<double>::output(const double &Val, llvm::raw_ostream &Out) {
Out << llvm::format("%g", Val);
}
void ScalarTraits<float>::output(const float &Val, llvm::raw_ostream &Out) {
Out << llvm::format("%g", Val);
}
|