JSON.h 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- JSON.h - JSON values, parsing and serialization -------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===---------------------------------------------------------------------===//
  13. ///
  14. /// \file
  15. /// This file supports working with JSON data.
  16. ///
  17. /// It comprises:
  18. ///
  19. /// - classes which hold dynamically-typed parsed JSON structures
  20. /// These are value types that can be composed, inspected, and modified.
  21. /// See json::Value, and the related types json::Object and json::Array.
  22. ///
  23. /// - functions to parse JSON text into Values, and to serialize Values to text.
  24. /// See parse(), operator<<, and format_provider.
  25. ///
  26. /// - a convention and helpers for mapping between json::Value and user-defined
  27. /// types. See fromJSON(), ObjectMapper, and the class comment on Value.
  28. ///
  29. /// - an output API json::OStream which can emit JSON without materializing
  30. /// all structures as json::Value.
  31. ///
  32. /// Typically, JSON data would be read from an external source, parsed into
  33. /// a Value, and then converted into some native data structure before doing
  34. /// real work on it. (And vice versa when writing).
  35. ///
  36. /// Other serialization mechanisms you may consider:
  37. ///
  38. /// - YAML is also text-based, and more human-readable than JSON. It's a more
  39. /// complex format and data model, and YAML parsers aren't ubiquitous.
  40. /// YAMLParser.h is a streaming parser suitable for parsing large documents
  41. /// (including JSON, as YAML is a superset). It can be awkward to use
  42. /// directly. YAML I/O (YAMLTraits.h) provides data mapping that is more
  43. /// declarative than the toJSON/fromJSON conventions here.
  44. ///
  45. /// - LLVM bitstream is a space- and CPU- efficient binary format. Typically it
  46. /// encodes LLVM IR ("bitcode"), but it can be a container for other data.
  47. /// Low-level reader/writer libraries are in Bitstream/Bitstream*.h
  48. ///
  49. //===---------------------------------------------------------------------===//
  50. #ifndef LLVM_SUPPORT_JSON_H
  51. #define LLVM_SUPPORT_JSON_H
  52. #include "llvm/ADT/DenseMap.h"
  53. #include "llvm/ADT/SmallVector.h"
  54. #include "llvm/ADT/StringRef.h"
  55. #include "llvm/ADT/STLFunctionalExtras.h"
  56. #include "llvm/Support/Error.h"
  57. #include "llvm/Support/FormatVariadic.h"
  58. #include "llvm/Support/raw_ostream.h"
  59. #include <map>
  60. namespace llvm {
  61. namespace json {
  62. // === String encodings ===
  63. //
  64. // JSON strings are character sequences (not byte sequences like std::string).
  65. // We need to know the encoding, and for simplicity only support UTF-8.
  66. //
  67. // - When parsing, invalid UTF-8 is a syntax error like any other
  68. //
  69. // - When creating Values from strings, callers must ensure they are UTF-8.
  70. // with asserts on, invalid UTF-8 will crash the program
  71. // with asserts off, we'll substitute the replacement character (U+FFFD)
  72. // Callers can use json::isUTF8() and json::fixUTF8() for validation.
  73. //
  74. // - When retrieving strings from Values (e.g. asString()), the result will
  75. // always be valid UTF-8.
  76. /// Returns true if \p S is valid UTF-8, which is required for use as JSON.
  77. /// If it returns false, \p Offset is set to a byte offset near the first error.
  78. bool isUTF8(llvm::StringRef S, size_t *ErrOffset = nullptr);
  79. /// Replaces invalid UTF-8 sequences in \p S with the replacement character
  80. /// (U+FFFD). The returned string is valid UTF-8.
  81. /// This is much slower than isUTF8, so test that first.
  82. std::string fixUTF8(llvm::StringRef S);
  83. class Array;
  84. class ObjectKey;
  85. class Value;
  86. template <typename T> Value toJSON(const llvm::Optional<T> &Opt);
  87. /// An Object is a JSON object, which maps strings to heterogenous JSON values.
  88. /// It simulates DenseMap<ObjectKey, Value>. ObjectKey is a maybe-owned string.
  89. class Object {
  90. using Storage = DenseMap<ObjectKey, Value, llvm::DenseMapInfo<StringRef>>;
  91. Storage M;
  92. public:
  93. using key_type = ObjectKey;
  94. using mapped_type = Value;
  95. using value_type = Storage::value_type;
  96. using iterator = Storage::iterator;
  97. using const_iterator = Storage::const_iterator;
  98. Object() = default;
  99. // KV is a trivial key-value struct for list-initialization.
  100. // (using std::pair forces extra copies).
  101. struct KV;
  102. explicit Object(std::initializer_list<KV> Properties);
  103. iterator begin() { return M.begin(); }
  104. const_iterator begin() const { return M.begin(); }
  105. iterator end() { return M.end(); }
  106. const_iterator end() const { return M.end(); }
  107. bool empty() const { return M.empty(); }
  108. size_t size() const { return M.size(); }
  109. void clear() { M.clear(); }
  110. std::pair<iterator, bool> insert(KV E);
  111. template <typename... Ts>
  112. std::pair<iterator, bool> try_emplace(const ObjectKey &K, Ts &&... Args) {
  113. return M.try_emplace(K, std::forward<Ts>(Args)...);
  114. }
  115. template <typename... Ts>
  116. std::pair<iterator, bool> try_emplace(ObjectKey &&K, Ts &&... Args) {
  117. return M.try_emplace(std::move(K), std::forward<Ts>(Args)...);
  118. }
  119. bool erase(StringRef K);
  120. void erase(iterator I) { M.erase(I); }
  121. iterator find(StringRef K) { return M.find_as(K); }
  122. const_iterator find(StringRef K) const { return M.find_as(K); }
  123. // operator[] acts as if Value was default-constructible as null.
  124. Value &operator[](const ObjectKey &K);
  125. Value &operator[](ObjectKey &&K);
  126. // Look up a property, returning nullptr if it doesn't exist.
  127. Value *get(StringRef K);
  128. const Value *get(StringRef K) const;
  129. // Typed accessors return None/nullptr if
  130. // - the property doesn't exist
  131. // - or it has the wrong type
  132. llvm::Optional<std::nullptr_t> getNull(StringRef K) const;
  133. llvm::Optional<bool> getBoolean(StringRef K) const;
  134. llvm::Optional<double> getNumber(StringRef K) const;
  135. llvm::Optional<int64_t> getInteger(StringRef K) const;
  136. llvm::Optional<llvm::StringRef> getString(StringRef K) const;
  137. const json::Object *getObject(StringRef K) const;
  138. json::Object *getObject(StringRef K);
  139. const json::Array *getArray(StringRef K) const;
  140. json::Array *getArray(StringRef K);
  141. };
  142. bool operator==(const Object &LHS, const Object &RHS);
  143. inline bool operator!=(const Object &LHS, const Object &RHS) {
  144. return !(LHS == RHS);
  145. }
  146. /// An Array is a JSON array, which contains heterogeneous JSON values.
  147. /// It simulates std::vector<Value>.
  148. class Array {
  149. std::vector<Value> V;
  150. public:
  151. using value_type = Value;
  152. using iterator = std::vector<Value>::iterator;
  153. using const_iterator = std::vector<Value>::const_iterator;
  154. Array() = default;
  155. explicit Array(std::initializer_list<Value> Elements);
  156. template <typename Collection> explicit Array(const Collection &C) {
  157. for (const auto &V : C)
  158. emplace_back(V);
  159. }
  160. Value &operator[](size_t I);
  161. const Value &operator[](size_t I) const;
  162. Value &front();
  163. const Value &front() const;
  164. Value &back();
  165. const Value &back() const;
  166. Value *data();
  167. const Value *data() const;
  168. iterator begin();
  169. const_iterator begin() const;
  170. iterator end();
  171. const_iterator end() const;
  172. bool empty() const;
  173. size_t size() const;
  174. void reserve(size_t S);
  175. void clear();
  176. void push_back(const Value &E);
  177. void push_back(Value &&E);
  178. template <typename... Args> void emplace_back(Args &&... A);
  179. void pop_back();
  180. // FIXME: insert() takes const_iterator since C++11, old libstdc++ disagrees.
  181. iterator insert(iterator P, const Value &E);
  182. iterator insert(iterator P, Value &&E);
  183. template <typename It> iterator insert(iterator P, It A, It Z);
  184. template <typename... Args> iterator emplace(const_iterator P, Args &&... A);
  185. friend bool operator==(const Array &L, const Array &R);
  186. };
  187. inline bool operator!=(const Array &L, const Array &R) { return !(L == R); }
  188. /// A Value is an JSON value of unknown type.
  189. /// They can be copied, but should generally be moved.
  190. ///
  191. /// === Composing values ===
  192. ///
  193. /// You can implicitly construct Values from:
  194. /// - strings: std::string, SmallString, formatv, StringRef, char*
  195. /// (char*, and StringRef are references, not copies!)
  196. /// - numbers
  197. /// - booleans
  198. /// - null: nullptr
  199. /// - arrays: {"foo", 42.0, false}
  200. /// - serializable things: types with toJSON(const T&)->Value, found by ADL
  201. ///
  202. /// They can also be constructed from object/array helpers:
  203. /// - json::Object is a type like map<ObjectKey, Value>
  204. /// - json::Array is a type like vector<Value>
  205. /// These can be list-initialized, or used to build up collections in a loop.
  206. /// json::ary(Collection) converts all items in a collection to Values.
  207. ///
  208. /// === Inspecting values ===
  209. ///
  210. /// Each Value is one of the JSON kinds:
  211. /// null (nullptr_t)
  212. /// boolean (bool)
  213. /// number (double, int64 or uint64)
  214. /// string (StringRef)
  215. /// array (json::Array)
  216. /// object (json::Object)
  217. ///
  218. /// The kind can be queried directly, or implicitly via the typed accessors:
  219. /// if (Optional<StringRef> S = E.getAsString()
  220. /// assert(E.kind() == Value::String);
  221. ///
  222. /// Array and Object also have typed indexing accessors for easy traversal:
  223. /// Expected<Value> E = parse(R"( {"options": {"font": "sans-serif"}} )");
  224. /// if (Object* O = E->getAsObject())
  225. /// if (Object* Opts = O->getObject("options"))
  226. /// if (Optional<StringRef> Font = Opts->getString("font"))
  227. /// assert(Opts->at("font").kind() == Value::String);
  228. ///
  229. /// === Converting JSON values to C++ types ===
  230. ///
  231. /// The convention is to have a deserializer function findable via ADL:
  232. /// fromJSON(const json::Value&, T&, Path) -> bool
  233. ///
  234. /// The return value indicates overall success, and Path is used for precise
  235. /// error reporting. (The Path::Root passed in at the top level fromJSON call
  236. /// captures any nested error and can render it in context).
  237. /// If conversion fails, fromJSON calls Path::report() and immediately returns.
  238. /// This ensures that the first fatal error survives.
  239. ///
  240. /// Deserializers are provided for:
  241. /// - bool
  242. /// - int and int64_t
  243. /// - double
  244. /// - std::string
  245. /// - vector<T>, where T is deserializable
  246. /// - map<string, T>, where T is deserializable
  247. /// - Optional<T>, where T is deserializable
  248. /// ObjectMapper can help writing fromJSON() functions for object types.
  249. ///
  250. /// For conversion in the other direction, the serializer function is:
  251. /// toJSON(const T&) -> json::Value
  252. /// If this exists, then it also allows constructing Value from T, and can
  253. /// be used to serialize vector<T>, map<string, T>, and Optional<T>.
  254. ///
  255. /// === Serialization ===
  256. ///
  257. /// Values can be serialized to JSON:
  258. /// 1) raw_ostream << Value // Basic formatting.
  259. /// 2) raw_ostream << formatv("{0}", Value) // Basic formatting.
  260. /// 3) raw_ostream << formatv("{0:2}", Value) // Pretty-print with indent 2.
  261. ///
  262. /// And parsed:
  263. /// Expected<Value> E = json::parse("[1, 2, null]");
  264. /// assert(E && E->kind() == Value::Array);
  265. class Value {
  266. public:
  267. enum Kind {
  268. Null,
  269. Boolean,
  270. /// Number values can store both int64s and doubles at full precision,
  271. /// depending on what they were constructed/parsed from.
  272. Number,
  273. String,
  274. Array,
  275. Object,
  276. };
  277. // It would be nice to have Value() be null. But that would make {} null too.
  278. Value(const Value &M) { copyFrom(M); }
  279. Value(Value &&M) { moveFrom(std::move(M)); }
  280. Value(std::initializer_list<Value> Elements);
  281. Value(json::Array &&Elements) : Type(T_Array) {
  282. create<json::Array>(std::move(Elements));
  283. }
  284. template <typename Elt>
  285. Value(const std::vector<Elt> &C) : Value(json::Array(C)) {}
  286. Value(json::Object &&Properties) : Type(T_Object) {
  287. create<json::Object>(std::move(Properties));
  288. }
  289. template <typename Elt>
  290. Value(const std::map<std::string, Elt> &C) : Value(json::Object(C)) {}
  291. // Strings: types with value semantics. Must be valid UTF-8.
  292. Value(std::string V) : Type(T_String) {
  293. if (LLVM_UNLIKELY(!isUTF8(V))) {
  294. assert(false && "Invalid UTF-8 in value used as JSON");
  295. V = fixUTF8(std::move(V));
  296. }
  297. create<std::string>(std::move(V));
  298. }
  299. Value(const llvm::SmallVectorImpl<char> &V)
  300. : Value(std::string(V.begin(), V.end())) {}
  301. Value(const llvm::formatv_object_base &V) : Value(V.str()) {}
  302. // Strings: types with reference semantics. Must be valid UTF-8.
  303. Value(StringRef V) : Type(T_StringRef) {
  304. create<llvm::StringRef>(V);
  305. if (LLVM_UNLIKELY(!isUTF8(V))) {
  306. assert(false && "Invalid UTF-8 in value used as JSON");
  307. *this = Value(fixUTF8(V));
  308. }
  309. }
  310. Value(const char *V) : Value(StringRef(V)) {}
  311. Value(std::nullptr_t) : Type(T_Null) {}
  312. // Boolean (disallow implicit conversions).
  313. // (The last template parameter is a dummy to keep templates distinct.)
  314. template <typename T,
  315. typename = std::enable_if_t<std::is_same<T, bool>::value>,
  316. bool = false>
  317. Value(T B) : Type(T_Boolean) {
  318. create<bool>(B);
  319. }
  320. // Unsigned 64-bit long integers.
  321. template <typename T,
  322. typename = std::enable_if_t<std::is_same<T, uint64_t>::value>,
  323. bool = false, bool = false>
  324. Value(T V) : Type(T_UINT64) {
  325. create<uint64_t>(uint64_t{V});
  326. }
  327. // Integers (except boolean and uint64_t).
  328. // Must be non-narrowing convertible to int64_t.
  329. template <typename T, typename = std::enable_if_t<std::is_integral<T>::value>,
  330. typename = std::enable_if_t<!std::is_same<T, bool>::value>,
  331. typename = std::enable_if_t<!std::is_same<T, uint64_t>::value>>
  332. Value(T I) : Type(T_Integer) {
  333. create<int64_t>(int64_t{I});
  334. }
  335. // Floating point. Must be non-narrowing convertible to double.
  336. template <typename T,
  337. typename = std::enable_if_t<std::is_floating_point<T>::value>,
  338. double * = nullptr>
  339. Value(T D) : Type(T_Double) {
  340. create<double>(double{D});
  341. }
  342. // Serializable types: with a toJSON(const T&)->Value function, found by ADL.
  343. template <typename T,
  344. typename = std::enable_if_t<std::is_same<
  345. Value, decltype(toJSON(*(const T *)nullptr))>::value>,
  346. Value * = nullptr>
  347. Value(const T &V) : Value(toJSON(V)) {}
  348. Value &operator=(const Value &M) {
  349. destroy();
  350. copyFrom(M);
  351. return *this;
  352. }
  353. Value &operator=(Value &&M) {
  354. destroy();
  355. moveFrom(std::move(M));
  356. return *this;
  357. }
  358. ~Value() { destroy(); }
  359. Kind kind() const {
  360. switch (Type) {
  361. case T_Null:
  362. return Null;
  363. case T_Boolean:
  364. return Boolean;
  365. case T_Double:
  366. case T_Integer:
  367. case T_UINT64:
  368. return Number;
  369. case T_String:
  370. case T_StringRef:
  371. return String;
  372. case T_Object:
  373. return Object;
  374. case T_Array:
  375. return Array;
  376. }
  377. llvm_unreachable("Unknown kind");
  378. }
  379. // Typed accessors return None/nullptr if the Value is not of this type.
  380. llvm::Optional<std::nullptr_t> getAsNull() const {
  381. if (LLVM_LIKELY(Type == T_Null))
  382. return nullptr;
  383. return llvm::None;
  384. }
  385. llvm::Optional<bool> getAsBoolean() const {
  386. if (LLVM_LIKELY(Type == T_Boolean))
  387. return as<bool>();
  388. return llvm::None;
  389. }
  390. llvm::Optional<double> getAsNumber() const {
  391. if (LLVM_LIKELY(Type == T_Double))
  392. return as<double>();
  393. if (LLVM_LIKELY(Type == T_Integer))
  394. return as<int64_t>();
  395. if (LLVM_LIKELY(Type == T_UINT64))
  396. return as<uint64_t>();
  397. return llvm::None;
  398. }
  399. // Succeeds if the Value is a Number, and exactly representable as int64_t.
  400. llvm::Optional<int64_t> getAsInteger() const {
  401. if (LLVM_LIKELY(Type == T_Integer))
  402. return as<int64_t>();
  403. if (LLVM_LIKELY(Type == T_Double)) {
  404. double D = as<double>();
  405. if (LLVM_LIKELY(std::modf(D, &D) == 0.0 &&
  406. D >= double(std::numeric_limits<int64_t>::min()) &&
  407. D <= double(std::numeric_limits<int64_t>::max())))
  408. return D;
  409. }
  410. return llvm::None;
  411. }
  412. llvm::Optional<uint64_t> getAsUINT64() const {
  413. if (Type == T_UINT64)
  414. return as<uint64_t>();
  415. else if (Type == T_Integer) {
  416. int64_t N = as<int64_t>();
  417. if (N >= 0)
  418. return as<uint64_t>();
  419. }
  420. return llvm::None;
  421. }
  422. llvm::Optional<llvm::StringRef> getAsString() const {
  423. if (Type == T_String)
  424. return llvm::StringRef(as<std::string>());
  425. if (LLVM_LIKELY(Type == T_StringRef))
  426. return as<llvm::StringRef>();
  427. return llvm::None;
  428. }
  429. const json::Object *getAsObject() const {
  430. return LLVM_LIKELY(Type == T_Object) ? &as<json::Object>() : nullptr;
  431. }
  432. json::Object *getAsObject() {
  433. return LLVM_LIKELY(Type == T_Object) ? &as<json::Object>() : nullptr;
  434. }
  435. const json::Array *getAsArray() const {
  436. return LLVM_LIKELY(Type == T_Array) ? &as<json::Array>() : nullptr;
  437. }
  438. json::Array *getAsArray() {
  439. return LLVM_LIKELY(Type == T_Array) ? &as<json::Array>() : nullptr;
  440. }
  441. private:
  442. void destroy();
  443. void copyFrom(const Value &M);
  444. // We allow moving from *const* Values, by marking all members as mutable!
  445. // This hack is needed to support initializer-list syntax efficiently.
  446. // (std::initializer_list<T> is a container of const T).
  447. void moveFrom(const Value &&M);
  448. friend class Array;
  449. friend class Object;
  450. template <typename T, typename... U> void create(U &&... V) {
  451. new (reinterpret_cast<T *>(&Union)) T(std::forward<U>(V)...);
  452. }
  453. template <typename T> T &as() const {
  454. // Using this two-step static_cast via void * instead of reinterpret_cast
  455. // silences a -Wstrict-aliasing false positive from GCC6 and earlier.
  456. void *Storage = static_cast<void *>(&Union);
  457. return *static_cast<T *>(Storage);
  458. }
  459. friend class OStream;
  460. enum ValueType : char16_t {
  461. T_Null,
  462. T_Boolean,
  463. T_Double,
  464. T_Integer,
  465. T_UINT64,
  466. T_StringRef,
  467. T_String,
  468. T_Object,
  469. T_Array,
  470. };
  471. // All members mutable, see moveFrom().
  472. mutable ValueType Type;
  473. mutable llvm::AlignedCharArrayUnion<bool, double, int64_t, uint64_t,
  474. llvm::StringRef, std::string, json::Array,
  475. json::Object>
  476. Union;
  477. friend bool operator==(const Value &, const Value &);
  478. };
  479. bool operator==(const Value &, const Value &);
  480. inline bool operator!=(const Value &L, const Value &R) { return !(L == R); }
  481. // Array Methods
  482. inline Value &Array::operator[](size_t I) { return V[I]; }
  483. inline const Value &Array::operator[](size_t I) const { return V[I]; }
  484. inline Value &Array::front() { return V.front(); }
  485. inline const Value &Array::front() const { return V.front(); }
  486. inline Value &Array::back() { return V.back(); }
  487. inline const Value &Array::back() const { return V.back(); }
  488. inline Value *Array::data() { return V.data(); }
  489. inline const Value *Array::data() const { return V.data(); }
  490. inline typename Array::iterator Array::begin() { return V.begin(); }
  491. inline typename Array::const_iterator Array::begin() const { return V.begin(); }
  492. inline typename Array::iterator Array::end() { return V.end(); }
  493. inline typename Array::const_iterator Array::end() const { return V.end(); }
  494. inline bool Array::empty() const { return V.empty(); }
  495. inline size_t Array::size() const { return V.size(); }
  496. inline void Array::reserve(size_t S) { V.reserve(S); }
  497. inline void Array::clear() { V.clear(); }
  498. inline void Array::push_back(const Value &E) { V.push_back(E); }
  499. inline void Array::push_back(Value &&E) { V.push_back(std::move(E)); }
  500. template <typename... Args> inline void Array::emplace_back(Args &&...A) {
  501. V.emplace_back(std::forward<Args>(A)...);
  502. }
  503. inline void Array::pop_back() { V.pop_back(); }
  504. inline typename Array::iterator Array::insert(iterator P, const Value &E) {
  505. return V.insert(P, E);
  506. }
  507. inline typename Array::iterator Array::insert(iterator P, Value &&E) {
  508. return V.insert(P, std::move(E));
  509. }
  510. template <typename It>
  511. inline typename Array::iterator Array::insert(iterator P, It A, It Z) {
  512. return V.insert(P, A, Z);
  513. }
  514. template <typename... Args>
  515. inline typename Array::iterator Array::emplace(const_iterator P, Args &&...A) {
  516. return V.emplace(P, std::forward<Args>(A)...);
  517. }
  518. inline bool operator==(const Array &L, const Array &R) { return L.V == R.V; }
  519. /// ObjectKey is a used to capture keys in Object. Like Value but:
  520. /// - only strings are allowed
  521. /// - it's optimized for the string literal case (Owned == nullptr)
  522. /// Like Value, strings must be UTF-8. See isUTF8 documentation for details.
  523. class ObjectKey {
  524. public:
  525. ObjectKey(const char *S) : ObjectKey(StringRef(S)) {}
  526. ObjectKey(std::string S) : Owned(new std::string(std::move(S))) {
  527. if (LLVM_UNLIKELY(!isUTF8(*Owned))) {
  528. assert(false && "Invalid UTF-8 in value used as JSON");
  529. *Owned = fixUTF8(std::move(*Owned));
  530. }
  531. Data = *Owned;
  532. }
  533. ObjectKey(llvm::StringRef S) : Data(S) {
  534. if (LLVM_UNLIKELY(!isUTF8(Data))) {
  535. assert(false && "Invalid UTF-8 in value used as JSON");
  536. *this = ObjectKey(fixUTF8(S));
  537. }
  538. }
  539. ObjectKey(const llvm::SmallVectorImpl<char> &V)
  540. : ObjectKey(std::string(V.begin(), V.end())) {}
  541. ObjectKey(const llvm::formatv_object_base &V) : ObjectKey(V.str()) {}
  542. ObjectKey(const ObjectKey &C) { *this = C; }
  543. ObjectKey(ObjectKey &&C) : ObjectKey(static_cast<const ObjectKey &&>(C)) {}
  544. ObjectKey &operator=(const ObjectKey &C) {
  545. if (C.Owned) {
  546. Owned.reset(new std::string(*C.Owned));
  547. Data = *Owned;
  548. } else {
  549. Data = C.Data;
  550. }
  551. return *this;
  552. }
  553. ObjectKey &operator=(ObjectKey &&) = default;
  554. operator llvm::StringRef() const { return Data; }
  555. std::string str() const { return Data.str(); }
  556. private:
  557. // FIXME: this is unneccesarily large (3 pointers). Pointer + length + owned
  558. // could be 2 pointers at most.
  559. std::unique_ptr<std::string> Owned;
  560. llvm::StringRef Data;
  561. };
  562. inline bool operator==(const ObjectKey &L, const ObjectKey &R) {
  563. return llvm::StringRef(L) == llvm::StringRef(R);
  564. }
  565. inline bool operator!=(const ObjectKey &L, const ObjectKey &R) {
  566. return !(L == R);
  567. }
  568. inline bool operator<(const ObjectKey &L, const ObjectKey &R) {
  569. return StringRef(L) < StringRef(R);
  570. }
  571. struct Object::KV {
  572. ObjectKey K;
  573. Value V;
  574. };
  575. inline Object::Object(std::initializer_list<KV> Properties) {
  576. for (const auto &P : Properties) {
  577. auto R = try_emplace(P.K, nullptr);
  578. if (R.second)
  579. R.first->getSecond().moveFrom(std::move(P.V));
  580. }
  581. }
  582. inline std::pair<Object::iterator, bool> Object::insert(KV E) {
  583. return try_emplace(std::move(E.K), std::move(E.V));
  584. }
  585. inline bool Object::erase(StringRef K) {
  586. return M.erase(ObjectKey(K));
  587. }
  588. /// A "cursor" marking a position within a Value.
  589. /// The Value is a tree, and this is the path from the root to the current node.
  590. /// This is used to associate errors with particular subobjects.
  591. class Path {
  592. public:
  593. class Root;
  594. /// Records that the value at the current path is invalid.
  595. /// Message is e.g. "expected number" and becomes part of the final error.
  596. /// This overwrites any previously written error message in the root.
  597. void report(llvm::StringLiteral Message);
  598. /// The root may be treated as a Path.
  599. Path(Root &R) : Parent(nullptr), Seg(&R) {}
  600. /// Derives a path for an array element: this[Index]
  601. Path index(unsigned Index) const { return Path(this, Segment(Index)); }
  602. /// Derives a path for an object field: this.Field
  603. Path field(StringRef Field) const { return Path(this, Segment(Field)); }
  604. private:
  605. /// One element in a JSON path: an object field (.foo) or array index [27].
  606. /// Exception: the root Path encodes a pointer to the Path::Root.
  607. class Segment {
  608. uintptr_t Pointer;
  609. unsigned Offset;
  610. public:
  611. Segment() = default;
  612. Segment(Root *R) : Pointer(reinterpret_cast<uintptr_t>(R)) {}
  613. Segment(llvm::StringRef Field)
  614. : Pointer(reinterpret_cast<uintptr_t>(Field.data())),
  615. Offset(static_cast<unsigned>(Field.size())) {}
  616. Segment(unsigned Index) : Pointer(0), Offset(Index) {}
  617. bool isField() const { return Pointer != 0; }
  618. StringRef field() const {
  619. return StringRef(reinterpret_cast<const char *>(Pointer), Offset);
  620. }
  621. unsigned index() const { return Offset; }
  622. Root *root() const { return reinterpret_cast<Root *>(Pointer); }
  623. };
  624. const Path *Parent;
  625. Segment Seg;
  626. Path(const Path *Parent, Segment S) : Parent(Parent), Seg(S) {}
  627. };
  628. /// The root is the trivial Path to the root value.
  629. /// It also stores the latest reported error and the path where it occurred.
  630. class Path::Root {
  631. llvm::StringRef Name;
  632. llvm::StringLiteral ErrorMessage;
  633. std::vector<Path::Segment> ErrorPath; // Only valid in error state. Reversed.
  634. friend void Path::report(llvm::StringLiteral Message);
  635. public:
  636. Root(llvm::StringRef Name = "") : Name(Name), ErrorMessage("") {}
  637. // No copy/move allowed as there are incoming pointers.
  638. Root(Root &&) = delete;
  639. Root &operator=(Root &&) = delete;
  640. Root(const Root &) = delete;
  641. Root &operator=(const Root &) = delete;
  642. /// Returns the last error reported, or else a generic error.
  643. Error getError() const;
  644. /// Print the root value with the error shown inline as a comment.
  645. /// Unrelated parts of the value are elided for brevity, e.g.
  646. /// {
  647. /// "id": 42,
  648. /// "name": /* expected string */ null,
  649. /// "properties": { ... }
  650. /// }
  651. void printErrorContext(const Value &, llvm::raw_ostream &) const;
  652. };
  653. // Standard deserializers are provided for primitive types.
  654. // See comments on Value.
  655. inline bool fromJSON(const Value &E, std::string &Out, Path P) {
  656. if (auto S = E.getAsString()) {
  657. Out = std::string(*S);
  658. return true;
  659. }
  660. P.report("expected string");
  661. return false;
  662. }
  663. inline bool fromJSON(const Value &E, int &Out, Path P) {
  664. if (auto S = E.getAsInteger()) {
  665. Out = *S;
  666. return true;
  667. }
  668. P.report("expected integer");
  669. return false;
  670. }
  671. inline bool fromJSON(const Value &E, int64_t &Out, Path P) {
  672. if (auto S = E.getAsInteger()) {
  673. Out = *S;
  674. return true;
  675. }
  676. P.report("expected integer");
  677. return false;
  678. }
  679. inline bool fromJSON(const Value &E, double &Out, Path P) {
  680. if (auto S = E.getAsNumber()) {
  681. Out = *S;
  682. return true;
  683. }
  684. P.report("expected number");
  685. return false;
  686. }
  687. inline bool fromJSON(const Value &E, bool &Out, Path P) {
  688. if (auto S = E.getAsBoolean()) {
  689. Out = *S;
  690. return true;
  691. }
  692. P.report("expected boolean");
  693. return false;
  694. }
  695. inline bool fromJSON(const Value &E, uint64_t &Out, Path P) {
  696. if (auto S = E.getAsUINT64()) {
  697. Out = *S;
  698. return true;
  699. }
  700. P.report("expected uint64_t");
  701. return false;
  702. }
  703. inline bool fromJSON(const Value &E, std::nullptr_t &Out, Path P) {
  704. if (auto S = E.getAsNull()) {
  705. Out = *S;
  706. return true;
  707. }
  708. P.report("expected null");
  709. return false;
  710. }
  711. template <typename T>
  712. bool fromJSON(const Value &E, llvm::Optional<T> &Out, Path P) {
  713. if (E.getAsNull()) {
  714. Out = llvm::None;
  715. return true;
  716. }
  717. T Result;
  718. if (!fromJSON(E, Result, P))
  719. return false;
  720. Out = std::move(Result);
  721. return true;
  722. }
  723. template <typename T>
  724. bool fromJSON(const Value &E, std::vector<T> &Out, Path P) {
  725. if (auto *A = E.getAsArray()) {
  726. Out.clear();
  727. Out.resize(A->size());
  728. for (size_t I = 0; I < A->size(); ++I)
  729. if (!fromJSON((*A)[I], Out[I], P.index(I)))
  730. return false;
  731. return true;
  732. }
  733. P.report("expected array");
  734. return false;
  735. }
  736. template <typename T>
  737. bool fromJSON(const Value &E, std::map<std::string, T> &Out, Path P) {
  738. if (auto *O = E.getAsObject()) {
  739. Out.clear();
  740. for (const auto &KV : *O)
  741. if (!fromJSON(KV.second, Out[std::string(llvm::StringRef(KV.first))],
  742. P.field(KV.first)))
  743. return false;
  744. return true;
  745. }
  746. P.report("expected object");
  747. return false;
  748. }
  749. // Allow serialization of Optional<T> for supported T.
  750. template <typename T> Value toJSON(const llvm::Optional<T> &Opt) {
  751. return Opt ? Value(*Opt) : Value(nullptr);
  752. }
  753. /// Helper for mapping JSON objects onto protocol structs.
  754. ///
  755. /// Example:
  756. /// \code
  757. /// bool fromJSON(const Value &E, MyStruct &R, Path P) {
  758. /// ObjectMapper O(E, P);
  759. /// // When returning false, error details were already reported.
  760. /// return O && O.map("mandatory_field", R.MandatoryField) &&
  761. /// O.mapOptional("optional_field", R.OptionalField);
  762. /// }
  763. /// \endcode
  764. class ObjectMapper {
  765. public:
  766. /// If O is not an object, this mapper is invalid and an error is reported.
  767. ObjectMapper(const Value &E, Path P) : O(E.getAsObject()), P(P) {
  768. if (!O)
  769. P.report("expected object");
  770. }
  771. /// True if the expression is an object.
  772. /// Must be checked before calling map().
  773. operator bool() const { return O; }
  774. /// Maps a property to a field.
  775. /// If the property is missing or invalid, reports an error.
  776. template <typename T> bool map(StringLiteral Prop, T &Out) {
  777. assert(*this && "Must check this is an object before calling map()");
  778. if (const Value *E = O->get(Prop))
  779. return fromJSON(*E, Out, P.field(Prop));
  780. P.field(Prop).report("missing value");
  781. return false;
  782. }
  783. /// Maps a property to a field, if it exists.
  784. /// If the property exists and is invalid, reports an error.
  785. /// (Optional requires special handling, because missing keys are OK).
  786. template <typename T> bool map(StringLiteral Prop, llvm::Optional<T> &Out) {
  787. assert(*this && "Must check this is an object before calling map()");
  788. if (const Value *E = O->get(Prop))
  789. return fromJSON(*E, Out, P.field(Prop));
  790. Out = llvm::None;
  791. return true;
  792. }
  793. /// Maps a property to a field, if it exists.
  794. /// If the property exists and is invalid, reports an error.
  795. /// If the property does not exist, Out is unchanged.
  796. template <typename T> bool mapOptional(StringLiteral Prop, T &Out) {
  797. assert(*this && "Must check this is an object before calling map()");
  798. if (const Value *E = O->get(Prop))
  799. return fromJSON(*E, Out, P.field(Prop));
  800. return true;
  801. }
  802. private:
  803. const Object *O;
  804. Path P;
  805. };
  806. /// Parses the provided JSON source, or returns a ParseError.
  807. /// The returned Value is self-contained and owns its strings (they do not refer
  808. /// to the original source).
  809. llvm::Expected<Value> parse(llvm::StringRef JSON);
  810. class ParseError : public llvm::ErrorInfo<ParseError> {
  811. const char *Msg;
  812. unsigned Line, Column, Offset;
  813. public:
  814. static char ID;
  815. ParseError(const char *Msg, unsigned Line, unsigned Column, unsigned Offset)
  816. : Msg(Msg), Line(Line), Column(Column), Offset(Offset) {}
  817. void log(llvm::raw_ostream &OS) const override {
  818. OS << llvm::formatv("[{0}:{1}, byte={2}]: {3}", Line, Column, Offset, Msg);
  819. }
  820. std::error_code convertToErrorCode() const override {
  821. return llvm::inconvertibleErrorCode();
  822. }
  823. };
  824. /// Version of parse() that converts the parsed value to the type T.
  825. /// RootName describes the root object and is used in error messages.
  826. template <typename T>
  827. Expected<T> parse(const llvm::StringRef &JSON, const char *RootName = "") {
  828. auto V = parse(JSON);
  829. if (!V)
  830. return V.takeError();
  831. Path::Root R(RootName);
  832. T Result;
  833. if (fromJSON(*V, Result, R))
  834. return std::move(Result);
  835. return R.getError();
  836. }
  837. /// json::OStream allows writing well-formed JSON without materializing
  838. /// all structures as json::Value ahead of time.
  839. /// It's faster, lower-level, and less safe than OS << json::Value.
  840. /// It also allows emitting more constructs, such as comments.
  841. ///
  842. /// Only one "top-level" object can be written to a stream.
  843. /// Simplest usage involves passing lambdas (Blocks) to fill in containers:
  844. ///
  845. /// json::OStream J(OS);
  846. /// J.array([&]{
  847. /// for (const Event &E : Events)
  848. /// J.object([&] {
  849. /// J.attribute("timestamp", int64_t(E.Time));
  850. /// J.attributeArray("participants", [&] {
  851. /// for (const Participant &P : E.Participants)
  852. /// J.value(P.toString());
  853. /// });
  854. /// });
  855. /// });
  856. ///
  857. /// This would produce JSON like:
  858. ///
  859. /// [
  860. /// {
  861. /// "timestamp": 19287398741,
  862. /// "participants": [
  863. /// "King Kong",
  864. /// "Miley Cyrus",
  865. /// "Cleopatra"
  866. /// ]
  867. /// },
  868. /// ...
  869. /// ]
  870. ///
  871. /// The lower level begin/end methods (arrayBegin()) are more flexible but
  872. /// care must be taken to pair them correctly:
  873. ///
  874. /// json::OStream J(OS);
  875. // J.arrayBegin();
  876. /// for (const Event &E : Events) {
  877. /// J.objectBegin();
  878. /// J.attribute("timestamp", int64_t(E.Time));
  879. /// J.attributeBegin("participants");
  880. /// for (const Participant &P : E.Participants)
  881. /// J.value(P.toString());
  882. /// J.attributeEnd();
  883. /// J.objectEnd();
  884. /// }
  885. /// J.arrayEnd();
  886. ///
  887. /// If the call sequence isn't valid JSON, asserts will fire in debug mode.
  888. /// This can be mismatched begin()/end() pairs, trying to emit attributes inside
  889. /// an array, and so on.
  890. /// With asserts disabled, this is undefined behavior.
  891. class OStream {
  892. public:
  893. using Block = llvm::function_ref<void()>;
  894. // If IndentSize is nonzero, output is pretty-printed.
  895. explicit OStream(llvm::raw_ostream &OS, unsigned IndentSize = 0)
  896. : OS(OS), IndentSize(IndentSize) {
  897. Stack.emplace_back();
  898. }
  899. ~OStream() {
  900. assert(Stack.size() == 1 && "Unmatched begin()/end()");
  901. assert(Stack.back().Ctx == Singleton);
  902. assert(Stack.back().HasValue && "Did not write top-level value");
  903. }
  904. /// Flushes the underlying ostream. OStream does not buffer internally.
  905. void flush() { OS.flush(); }
  906. // High level functions to output a value.
  907. // Valid at top-level (exactly once), in an attribute value (exactly once),
  908. // or in an array (any number of times).
  909. /// Emit a self-contained value (number, string, vector<string> etc).
  910. void value(const Value &V);
  911. /// Emit an array whose elements are emitted in the provided Block.
  912. void array(Block Contents) {
  913. arrayBegin();
  914. Contents();
  915. arrayEnd();
  916. }
  917. /// Emit an object whose elements are emitted in the provided Block.
  918. void object(Block Contents) {
  919. objectBegin();
  920. Contents();
  921. objectEnd();
  922. }
  923. /// Emit an externally-serialized value.
  924. /// The caller must write exactly one valid JSON value to the provided stream.
  925. /// No validation or formatting of this value occurs.
  926. void rawValue(llvm::function_ref<void(raw_ostream &)> Contents) {
  927. rawValueBegin();
  928. Contents(OS);
  929. rawValueEnd();
  930. }
  931. void rawValue(llvm::StringRef Contents) {
  932. rawValue([&](raw_ostream &OS) { OS << Contents; });
  933. }
  934. /// Emit a JavaScript comment associated with the next printed value.
  935. /// The string must be valid until the next attribute or value is emitted.
  936. /// Comments are not part of standard JSON, and many parsers reject them!
  937. void comment(llvm::StringRef);
  938. // High level functions to output object attributes.
  939. // Valid only within an object (any number of times).
  940. /// Emit an attribute whose value is self-contained (number, vector<int> etc).
  941. void attribute(llvm::StringRef Key, const Value& Contents) {
  942. attributeImpl(Key, [&] { value(Contents); });
  943. }
  944. /// Emit an attribute whose value is an array with elements from the Block.
  945. void attributeArray(llvm::StringRef Key, Block Contents) {
  946. attributeImpl(Key, [&] { array(Contents); });
  947. }
  948. /// Emit an attribute whose value is an object with attributes from the Block.
  949. void attributeObject(llvm::StringRef Key, Block Contents) {
  950. attributeImpl(Key, [&] { object(Contents); });
  951. }
  952. // Low-level begin/end functions to output arrays, objects, and attributes.
  953. // Must be correctly paired. Allowed contexts are as above.
  954. void arrayBegin();
  955. void arrayEnd();
  956. void objectBegin();
  957. void objectEnd();
  958. void attributeBegin(llvm::StringRef Key);
  959. void attributeEnd();
  960. raw_ostream &rawValueBegin();
  961. void rawValueEnd();
  962. private:
  963. void attributeImpl(llvm::StringRef Key, Block Contents) {
  964. attributeBegin(Key);
  965. Contents();
  966. attributeEnd();
  967. }
  968. void valueBegin();
  969. void flushComment();
  970. void newline();
  971. enum Context {
  972. Singleton, // Top level, or object attribute.
  973. Array,
  974. Object,
  975. RawValue, // External code writing a value to OS directly.
  976. };
  977. struct State {
  978. Context Ctx = Singleton;
  979. bool HasValue = false;
  980. };
  981. llvm::SmallVector<State, 16> Stack; // Never empty.
  982. llvm::StringRef PendingComment;
  983. llvm::raw_ostream &OS;
  984. unsigned IndentSize;
  985. unsigned Indent = 0;
  986. };
  987. /// Serializes this Value to JSON, writing it to the provided stream.
  988. /// The formatting is compact (no extra whitespace) and deterministic.
  989. /// For pretty-printing, use the formatv() format_provider below.
  990. inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Value &V) {
  991. OStream(OS).value(V);
  992. return OS;
  993. }
  994. } // namespace json
  995. /// Allow printing json::Value with formatv().
  996. /// The default style is basic/compact formatting, like operator<<.
  997. /// A format string like formatv("{0:2}", Value) pretty-prints with indent 2.
  998. template <> struct format_provider<llvm::json::Value> {
  999. static void format(const llvm::json::Value &, raw_ostream &, StringRef);
  1000. };
  1001. } // namespace llvm
  1002. #endif
  1003. #ifdef __GNUC__
  1004. #pragma GCC diagnostic pop
  1005. #endif