|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +#include "graphqlservice/JSONResponse.h" |
| 5 | + |
| 6 | +#include <tao/json.hpp> |
| 7 | + |
| 8 | +#include <cstdint> |
| 9 | +#include <iostream> |
| 10 | +#include <limits> |
| 11 | +#include <sstream> |
| 12 | +#include <vector> |
| 13 | + |
| 14 | +namespace graphql::response { |
| 15 | + |
| 16 | +class StreamWriter |
| 17 | +{ |
| 18 | +public: |
| 19 | + StreamWriter(std::ostream& stream) |
| 20 | + : _writer { stream } |
| 21 | + { |
| 22 | + } |
| 23 | + |
| 24 | + void start_object() |
| 25 | + { |
| 26 | + _scopeStack.push_back(Scope::Object); |
| 27 | + _writer.begin_object(); |
| 28 | + } |
| 29 | + |
| 30 | + void add_member(const std::string& key) |
| 31 | + { |
| 32 | + _writer.key(key); |
| 33 | + } |
| 34 | + |
| 35 | + void end_object() |
| 36 | + { |
| 37 | + _writer.end_object(); |
| 38 | + _scopeStack.pop_back(); |
| 39 | + end_value(); |
| 40 | + } |
| 41 | + |
| 42 | + void start_array() |
| 43 | + { |
| 44 | + _scopeStack.push_back(Scope::Object); |
| 45 | + _writer.begin_array(); |
| 46 | + } |
| 47 | + |
| 48 | + void end_arrary() |
| 49 | + { |
| 50 | + _writer.end_array(); |
| 51 | + _scopeStack.pop_back(); |
| 52 | + end_value(); |
| 53 | + } |
| 54 | + |
| 55 | + void write_null() |
| 56 | + { |
| 57 | + _writer.null(); |
| 58 | + end_value(); |
| 59 | + } |
| 60 | + |
| 61 | + void write_string(const std::string& value) |
| 62 | + { |
| 63 | + _writer.string(value); |
| 64 | + end_value(); |
| 65 | + } |
| 66 | + |
| 67 | + void write_bool(bool value) |
| 68 | + { |
| 69 | + _writer.boolean(value); |
| 70 | + end_value(); |
| 71 | + } |
| 72 | + |
| 73 | + void write_int(int value) |
| 74 | + { |
| 75 | + _writer.number(static_cast<std::int64_t>(value)); |
| 76 | + end_value(); |
| 77 | + } |
| 78 | + |
| 79 | + void write_float(double value) |
| 80 | + { |
| 81 | + _writer.number(value); |
| 82 | + end_value(); |
| 83 | + } |
| 84 | + |
| 85 | +private: |
| 86 | + enum class Scope |
| 87 | + { |
| 88 | + Array, |
| 89 | + Object, |
| 90 | + }; |
| 91 | + |
| 92 | + void end_value() |
| 93 | + { |
| 94 | + if (_scopeStack.empty()) |
| 95 | + { |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + switch (_scopeStack.back()) |
| 100 | + { |
| 101 | + case Scope::Array: |
| 102 | + _writer.element(); |
| 103 | + break; |
| 104 | + |
| 105 | + case Scope::Object: |
| 106 | + _writer.member(); |
| 107 | + break; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + tao::json::events::to_stream _writer; |
| 112 | + std::vector<Scope> _scopeStack; |
| 113 | +}; |
| 114 | + |
| 115 | +std::string toJSON(Value&& response) |
| 116 | +{ |
| 117 | + std::ostringstream stream; |
| 118 | + Writer writer { std::make_unique<StreamWriter>(stream) }; |
| 119 | + writer.write(std::move(response)); |
| 120 | + return stream.str(); |
| 121 | +} |
| 122 | + |
| 123 | +struct ResponseHandler |
| 124 | +{ |
| 125 | + ResponseHandler() |
| 126 | + { |
| 127 | + // Start with a single null value. |
| 128 | + _responseStack.push_back({}); |
| 129 | + } |
| 130 | + |
| 131 | + Value getResponse() |
| 132 | + { |
| 133 | + auto response = std::move(_responseStack.back()); |
| 134 | + |
| 135 | + _responseStack.pop_back(); |
| 136 | + |
| 137 | + return response; |
| 138 | + } |
| 139 | + |
| 140 | + void null() |
| 141 | + { |
| 142 | + setValue(Value()); |
| 143 | + } |
| 144 | + |
| 145 | + void boolean(bool b) |
| 146 | + { |
| 147 | + setValue(Value(b)); |
| 148 | + } |
| 149 | + |
| 150 | + void number(double d) |
| 151 | + { |
| 152 | + auto value = Value(Type::Float); |
| 153 | + |
| 154 | + value.set<FloatType>(std::move(d)); |
| 155 | + setValue(std::move(value)); |
| 156 | + } |
| 157 | + |
| 158 | + void number(std::int64_t i) |
| 159 | + { |
| 160 | + if (i < std::numeric_limits<std::int32_t>::min() |
| 161 | + || i > std::numeric_limits<std::int32_t>::max()) |
| 162 | + { |
| 163 | + // https://spec.graphql.org/October2021/#sec-Int |
| 164 | + number(static_cast<double>(i)); |
| 165 | + } |
| 166 | + else |
| 167 | + { |
| 168 | + static_assert(sizeof(std::int32_t) == sizeof(IntType), |
| 169 | + "GraphQL only supports 32-bit signed integers"); |
| 170 | + auto value = Value(Type::Int); |
| 171 | + |
| 172 | + value.set<IntType>(static_cast<std::int32_t>(i)); |
| 173 | + setValue(std::move(value)); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + void number(std::uint64_t i) |
| 178 | + { |
| 179 | + if (i > static_cast<std::uint64_t>(std::numeric_limits<std::int64_t>::max())) |
| 180 | + { |
| 181 | + // https://spec.graphql.org/October2021/#sec-Int |
| 182 | + number(static_cast<double>(i)); |
| 183 | + } |
| 184 | + else |
| 185 | + { |
| 186 | + number(static_cast<std::int64_t>(i)); |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + void string(std::string&& str) |
| 191 | + { |
| 192 | + setValue(Value(std::move(str)).from_json()); |
| 193 | + } |
| 194 | + |
| 195 | + void begin_array() |
| 196 | + { |
| 197 | + _responseStack.push_back(Value(Type::List)); |
| 198 | + } |
| 199 | + |
| 200 | + void element() |
| 201 | + { |
| 202 | + } |
| 203 | + |
| 204 | + void end_array() |
| 205 | + { |
| 206 | + setValue(getResponse()); |
| 207 | + } |
| 208 | + |
| 209 | + void begin_object() |
| 210 | + { |
| 211 | + _responseStack.push_back(Value(Type::Map)); |
| 212 | + } |
| 213 | + |
| 214 | + void key(std::string&& str) |
| 215 | + { |
| 216 | + _keyStack.push_back(std::move(str)); |
| 217 | + } |
| 218 | + |
| 219 | + void member() |
| 220 | + { |
| 221 | + } |
| 222 | + |
| 223 | + void end_object() |
| 224 | + { |
| 225 | + setValue(getResponse()); |
| 226 | + } |
| 227 | + |
| 228 | +private: |
| 229 | + void setValue(Value&& value) |
| 230 | + { |
| 231 | + switch (_responseStack.back().type()) |
| 232 | + { |
| 233 | + case Type::Map: |
| 234 | + _responseStack.back().emplace_back(std::move(_keyStack.back()), std::move(value)); |
| 235 | + _keyStack.pop_back(); |
| 236 | + break; |
| 237 | + |
| 238 | + case Type::List: |
| 239 | + _responseStack.back().emplace_back(std::move(value)); |
| 240 | + break; |
| 241 | + |
| 242 | + default: |
| 243 | + _responseStack.back() = std::move(value); |
| 244 | + break; |
| 245 | + } |
| 246 | + } |
| 247 | + |
| 248 | + std::vector<std::string> _keyStack; |
| 249 | + std::vector<Value> _responseStack; |
| 250 | +}; |
| 251 | + |
| 252 | +Value parseJSON(const std::string& json) |
| 253 | +{ |
| 254 | + ResponseHandler handler; |
| 255 | + tao::json::events::from_string(handler, json); |
| 256 | + |
| 257 | + return handler.getResponse(); |
| 258 | +} |
| 259 | + |
| 260 | +} // namespace graphql::response |
0 commit comments