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 <cmath>
  60. #include <map>
  61. namespace llvm {
  62. namespace json {
  63. // === String encodings ===
  64. //
  65. // JSON strings are character sequences (not byte sequences like std::string).
  66. // We need to know the encoding, and for simplicity only support UTF-8.
  67. //
  68. // - When parsing, invalid UTF-8 is a syntax error like any other
  69. //
  70. // - When creating Values from strings, callers must ensure they are UTF-8.
  71. // with asserts on, invalid UTF-8 will crash the program
  72. // with asserts off, we'll substitute the replacement character (U+FFFD)
  73. // Callers can use json::isUTF8() and json::fixUTF8() for validation.
  74. //
  75. // - When retrieving strings from Values (e.g. asString()), the result will
  76. // always be valid UTF-8.
  77. /// Returns true if \p S is valid UTF-8, which is required for use as JSON.
  78. /// If it returns false, \p Offset is set to a byte offset near the first error.
  79. bool isUTF8(llvm::StringRef S, size_t *ErrOffset = nullptr);
  80. /// Replaces invalid UTF-8 sequences in \p S with the replacement character
  81. /// (U+FFFD). The returned string is valid UTF-8.
  82. /// This is much slower than isUTF8, so test that first.
  83. std::string fixUTF8(llvm::StringRef S);
  84. class Array;
  85. class ObjectKey;
  86. class Value;
  87. template <typename T> Value toJSON(const std::optional<T> &Opt);
  88. /// An Object is a JSON object, which maps strings to heterogenous JSON values.
  89. /// It simulates DenseMap<ObjectKey, Value>. ObjectKey is a maybe-owned string.
  90. class Object {
  91. using Storage = DenseMap<ObjectKey, Value, llvm::DenseMapInfo<StringRef>>;
  92. Storage M;
  93. public:
  94. using key_type = ObjectKey;
  95. using mapped_type = Value;
  96. using value_type = Storage::value_type;
  97. using iterator = Storage::iterator;
  98. using const_iterator = Storage::const_iterator;
  99. Object() = default;
  100. // KV is a trivial key-value struct for list-initialization.
  101. // (using std::pair forces extra copies).
  102. struct KV;
  103. explicit Object(std::initializer_list<KV> Properties);
  104. iterator begin() { return M.begin(); }
  105. const_iterator begin() const { return M.begin(); }
  106. iterator end() { return M.end(); }
  107. const_iterator end() const { return M.end(); }
  108. bool empty() const { return M.empty(); }
  109. size_t size() const { return M.size(); }
  110. void clear() { M.clear(); }
  111. std::pair<iterator, bool> insert(KV E);
  112. template <typename... Ts>
  113. std::pair<iterator, bool> try_emplace(const ObjectKey &K, Ts &&... Args) {
  114. return M.try_emplace(K, std::forward<Ts>(Args)...);
  115. }
  116. template <typename... Ts>
  117. std::pair<iterator, bool> try_emplace(ObjectKey &&K, Ts &&... Args) {
  118. return M.try_emplace(std::move(K), std::forward<Ts>(Args)...);
  119. }
  120. bool erase(StringRef K);
  121. void erase(iterator I) { M.erase(I); }
  122. iterator find(StringRef K) { return M.find_as(K); }
  123. const_iterator find(StringRef K) const { return M.find_as(K); }
  124. // operator[] acts as if Value was default-constructible as null.
  125. Value &operator[](const ObjectKey &K);
  126. Value &operator[](ObjectKey &&K);
  127. // Look up a property, returning nullptr if it doesn't exist.
  128. Value *get(StringRef K);
  129. const Value *get(StringRef K) const;
  130. // Typed accessors return std::nullopt/nullptr if
  131. // - the property doesn't exist
  132. // - or it has the wrong type
  133. std::optional<std::nullptr_t> getNull(StringRef K) const;
  134. std::optional<bool> getBoolean(StringRef K) const;
  135. std::optional<double> getNumber(StringRef K) const;
  136. std::optional<int64_t> getInteger(StringRef K) const;
  137. std::optional<llvm::StringRef> getString(StringRef K) const;
  138. const json::Object *getObject(StringRef K) const;
  139. json::Object *getObject(StringRef K);
  140. const json::Array *getArray(StringRef K) const;
  141. json::Array *getArray(StringRef K);
  142. };
  143. bool operator==(const Object &LHS, const Object &RHS);
  144. inline bool operator!=(const Object &LHS, const Object &RHS) {
  145. return !(LHS == RHS);
  146. }
  147. /// An Array is a JSON array, which contains heterogeneous JSON values.
  148. /// It simulates std::vector<Value>.
  149. class Array {
  150. std::vector<Value> V;
  151. public:
  152. using value_type = Value;
  153. using iterator = std::vector<Value>::iterator;
  154. using const_iterator = std::vector<Value>::const_iterator;
  155. Array() = default;
  156. explicit Array(std::initializer_list<Value> Elements);
  157. template <typename Collection> explicit Array(const Collection &C) {
  158. for (const auto &V : C)
  159. emplace_back(V);
  160. }
  161. Value &operator[](size_t I);
  162. const Value &operator[](size_t I) const;
  163. Value &front();
  164. const Value &front() const;
  165. Value &back();
  166. const Value &back() const;
  167. Value *data();
  168. const Value *data() const;
  169. iterator begin();
  170. const_iterator begin() const;
  171. iterator end();
  172. const_iterator end() const;
  173. bool empty() const;
  174. size_t size() const;
  175. void reserve(size_t S);
  176. void clear();
  177. void push_back(const Value &E);
  178. void push_back(Value &&E);
  179. template <typename... Args> void emplace_back(Args &&...A);
  180. void pop_back();
  181. iterator insert(const_iterator P, const Value &E);
  182. iterator insert(const_iterator P, Value &&E);
  183. template <typename It> iterator insert(const_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 (std::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 (std::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. /// - std::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 std::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 std::nullopt/nullptr if the Value is not of this
  380. // type.
  381. std::optional<std::nullptr_t> getAsNull() const {
  382. if (LLVM_LIKELY(Type == T_Null))
  383. return nullptr;
  384. return std::nullopt;
  385. }
  386. std::optional<bool> getAsBoolean() const {
  387. if (LLVM_LIKELY(Type == T_Boolean))
  388. return as<bool>();
  389. return std::nullopt;
  390. }
  391. std::optional<double> getAsNumber() const {
  392. if (LLVM_LIKELY(Type == T_Double))
  393. return as<double>();
  394. if (LLVM_LIKELY(Type == T_Integer))
  395. return as<int64_t>();
  396. if (LLVM_LIKELY(Type == T_UINT64))
  397. return as<uint64_t>();
  398. return std::nullopt;
  399. }
  400. // Succeeds if the Value is a Number, and exactly representable as int64_t.
  401. std::optional<int64_t> getAsInteger() const {
  402. if (LLVM_LIKELY(Type == T_Integer))
  403. return as<int64_t>();
  404. if (LLVM_LIKELY(Type == T_Double)) {
  405. double D = as<double>();
  406. if (LLVM_LIKELY(std::modf(D, &D) == 0.0 &&
  407. D >= double(std::numeric_limits<int64_t>::min()) &&
  408. D <= double(std::numeric_limits<int64_t>::max())))
  409. return D;
  410. }
  411. return std::nullopt;
  412. }
  413. std::optional<uint64_t> getAsUINT64() const {
  414. if (Type == T_UINT64)
  415. return as<uint64_t>();
  416. else if (Type == T_Integer) {
  417. int64_t N = as<int64_t>();
  418. if (N >= 0)
  419. return as<uint64_t>();
  420. }
  421. return std::nullopt;
  422. }
  423. std::optional<llvm::StringRef> getAsString() const {
  424. if (Type == T_String)
  425. return llvm::StringRef(as<std::string>());
  426. if (LLVM_LIKELY(Type == T_StringRef))
  427. return as<llvm::StringRef>();
  428. return std::nullopt;
  429. }
  430. const json::Object *getAsObject() const {
  431. return LLVM_LIKELY(Type == T_Object) ? &as<json::Object>() : nullptr;
  432. }
  433. json::Object *getAsObject() {
  434. return LLVM_LIKELY(Type == T_Object) ? &as<json::Object>() : nullptr;
  435. }
  436. const json::Array *getAsArray() const {
  437. return LLVM_LIKELY(Type == T_Array) ? &as<json::Array>() : nullptr;
  438. }
  439. json::Array *getAsArray() {
  440. return LLVM_LIKELY(Type == T_Array) ? &as<json::Array>() : nullptr;
  441. }
  442. private:
  443. void destroy();
  444. void copyFrom(const Value &M);
  445. // We allow moving from *const* Values, by marking all members as mutable!
  446. // This hack is needed to support initializer-list syntax efficiently.
  447. // (std::initializer_list<T> is a container of const T).
  448. void moveFrom(const Value &&M);
  449. friend class Array;
  450. friend class Object;
  451. template <typename T, typename... U> void create(U &&... V) {
  452. new (reinterpret_cast<T *>(&Union)) T(std::forward<U>(V)...);
  453. }
  454. template <typename T> T &as() const {
  455. // Using this two-step static_cast via void * instead of reinterpret_cast
  456. // silences a -Wstrict-aliasing false positive from GCC6 and earlier.
  457. void *Storage = static_cast<void *>(&Union);
  458. return *static_cast<T *>(Storage);
  459. }
  460. friend class OStream;
  461. enum ValueType : char16_t {
  462. T_Null,
  463. T_Boolean,
  464. T_Double,
  465. T_Integer,
  466. T_UINT64,
  467. T_StringRef,
  468. T_String,
  469. T_Object,
  470. T_Array,
  471. };
  472. // All members mutable, see moveFrom().
  473. mutable ValueType Type;
  474. mutable llvm::AlignedCharArrayUnion<bool, double, int64_t, uint64_t,
  475. llvm::StringRef, std::string, json::Array,
  476. json::Object>
  477. Union;
  478. friend bool operator==(const Value &, const Value &);
  479. };
  480. bool operator==(const Value &, const Value &);
  481. inline bool operator!=(const Value &L, const Value &R) { return !(L == R); }
  482. // Array Methods
  483. inline Value &Array::operator[](size_t I) { return V[I]; }
  484. inline const Value &Array::operator[](size_t I) const { return V[I]; }
  485. inline Value &Array::front() { return V.front(); }
  486. inline const Value &Array::front() const { return V.front(); }
  487. inline Value &Array::back() { return V.back(); }
  488. inline const Value &Array::back() const { return V.back(); }
  489. inline Value *Array::data() { return V.data(); }
  490. inline const Value *Array::data() const { return V.data(); }
  491. inline typename Array::iterator Array::begin() { return V.begin(); }
  492. inline typename Array::const_iterator Array::begin() const { return V.begin(); }
  493. inline typename Array::iterator Array::end() { return V.end(); }
  494. inline typename Array::const_iterator Array::end() const { return V.end(); }
  495. inline bool Array::empty() const { return V.empty(); }
  496. inline size_t Array::size() const { return V.size(); }
  497. inline void Array::reserve(size_t S) { V.reserve(S); }
  498. inline void Array::clear() { V.clear(); }
  499. inline void Array::push_back(const Value &E) { V.push_back(E); }
  500. inline void Array::push_back(Value &&E) { V.push_back(std::move(E)); }
  501. template <typename... Args> inline void Array::emplace_back(Args &&...A) {
  502. V.emplace_back(std::forward<Args>(A)...);
  503. }
  504. inline void Array::pop_back() { V.pop_back(); }
  505. inline typename Array::iterator Array::insert(const_iterator P, const Value &E) {
  506. return V.insert(P, E);
  507. }
  508. inline typename Array::iterator Array::insert(const_iterator P, Value &&E) {
  509. return V.insert(P, std::move(E));
  510. }
  511. template <typename It>
  512. inline typename Array::iterator Array::insert(const_iterator P, It A, It Z) {
  513. return V.insert(P, A, Z);
  514. }
  515. template <typename... Args>
  516. inline typename Array::iterator Array::emplace(const_iterator P, Args &&...A) {
  517. return V.emplace(P, std::forward<Args>(A)...);
  518. }
  519. inline bool operator==(const Array &L, const Array &R) { return L.V == R.V; }
  520. /// ObjectKey is a used to capture keys in Object. Like Value but:
  521. /// - only strings are allowed
  522. /// - it's optimized for the string literal case (Owned == nullptr)
  523. /// Like Value, strings must be UTF-8. See isUTF8 documentation for details.
  524. class ObjectKey {
  525. public:
  526. ObjectKey(const char *S) : ObjectKey(StringRef(S)) {}
  527. ObjectKey(std::string S) : Owned(new std::string(std::move(S))) {
  528. if (LLVM_UNLIKELY(!isUTF8(*Owned))) {
  529. assert(false && "Invalid UTF-8 in value used as JSON");
  530. *Owned = fixUTF8(std::move(*Owned));
  531. }
  532. Data = *Owned;
  533. }
  534. ObjectKey(llvm::StringRef S) : Data(S) {
  535. if (LLVM_UNLIKELY(!isUTF8(Data))) {
  536. assert(false && "Invalid UTF-8 in value used as JSON");
  537. *this = ObjectKey(fixUTF8(S));
  538. }
  539. }
  540. ObjectKey(const llvm::SmallVectorImpl<char> &V)
  541. : ObjectKey(std::string(V.begin(), V.end())) {}
  542. ObjectKey(const llvm::formatv_object_base &V) : ObjectKey(V.str()) {}
  543. ObjectKey(const ObjectKey &C) { *this = C; }
  544. ObjectKey(ObjectKey &&C) : ObjectKey(static_cast<const ObjectKey &&>(C)) {}
  545. ObjectKey &operator=(const ObjectKey &C) {
  546. if (C.Owned) {
  547. Owned.reset(new std::string(*C.Owned));
  548. Data = *Owned;
  549. } else {
  550. Data = C.Data;
  551. }
  552. return *this;
  553. }
  554. ObjectKey &operator=(ObjectKey &&) = default;
  555. operator llvm::StringRef() const { return Data; }
  556. std::string str() const { return Data.str(); }
  557. private:
  558. // FIXME: this is unneccesarily large (3 pointers). Pointer + length + owned
  559. // could be 2 pointers at most.
  560. std::unique_ptr<std::string> Owned;
  561. llvm::StringRef Data;
  562. };
  563. inline bool operator==(const ObjectKey &L, const ObjectKey &R) {
  564. return llvm::StringRef(L) == llvm::StringRef(R);
  565. }
  566. inline bool operator!=(const ObjectKey &L, const ObjectKey &R) {
  567. return !(L == R);
  568. }
  569. inline bool operator<(const ObjectKey &L, const ObjectKey &R) {
  570. return StringRef(L) < StringRef(R);
  571. }
  572. struct Object::KV {
  573. ObjectKey K;
  574. Value V;
  575. };
  576. inline Object::Object(std::initializer_list<KV> Properties) {
  577. for (const auto &P : Properties) {
  578. auto R = try_emplace(P.K, nullptr);
  579. if (R.second)
  580. R.first->getSecond().moveFrom(std::move(P.V));
  581. }
  582. }
  583. inline std::pair<Object::iterator, bool> Object::insert(KV E) {
  584. return try_emplace(std::move(E.K), std::move(E.V));
  585. }
  586. inline bool Object::erase(StringRef K) {
  587. return M.erase(ObjectKey(K));
  588. }
  589. /// A "cursor" marking a position within a Value.
  590. /// The Value is a tree, and this is the path from the root to the current node.
  591. /// This is used to associate errors with particular subobjects.
  592. class Path {
  593. public:
  594. class Root;
  595. /// Records that the value at the current path is invalid.
  596. /// Message is e.g. "expected number" and becomes part of the final error.
  597. /// This overwrites any previously written error message in the root.
  598. void report(llvm::StringLiteral Message);
  599. /// The root may be treated as a Path.
  600. Path(Root &R) : Parent(nullptr), Seg(&R) {}
  601. /// Derives a path for an array element: this[Index]
  602. Path index(unsigned Index) const { return Path(this, Segment(Index)); }
  603. /// Derives a path for an object field: this.Field
  604. Path field(StringRef Field) const { return Path(this, Segment(Field)); }
  605. private:
  606. /// One element in a JSON path: an object field (.foo) or array index [27].
  607. /// Exception: the root Path encodes a pointer to the Path::Root.
  608. class Segment {
  609. uintptr_t Pointer;
  610. unsigned Offset;
  611. public:
  612. Segment() = default;
  613. Segment(Root *R) : Pointer(reinterpret_cast<uintptr_t>(R)) {}
  614. Segment(llvm::StringRef Field)
  615. : Pointer(reinterpret_cast<uintptr_t>(Field.data())),
  616. Offset(static_cast<unsigned>(Field.size())) {}
  617. Segment(unsigned Index) : Pointer(0), Offset(Index) {}
  618. bool isField() const { return Pointer != 0; }
  619. StringRef field() const {
  620. return StringRef(reinterpret_cast<const char *>(Pointer), Offset);
  621. }
  622. unsigned index() const { return Offset; }
  623. Root *root() const { return reinterpret_cast<Root *>(Pointer); }
  624. };
  625. const Path *Parent;
  626. Segment Seg;
  627. Path(const Path *Parent, Segment S) : Parent(Parent), Seg(S) {}
  628. };
  629. /// The root is the trivial Path to the root value.
  630. /// It also stores the latest reported error and the path where it occurred.
  631. class Path::Root {
  632. llvm::StringRef Name;
  633. llvm::StringLiteral ErrorMessage;
  634. std::vector<Path::Segment> ErrorPath; // Only valid in error state. Reversed.
  635. friend void Path::report(llvm::StringLiteral Message);
  636. public:
  637. Root(llvm::StringRef Name = "") : Name(Name), ErrorMessage("") {}
  638. // No copy/move allowed as there are incoming pointers.
  639. Root(Root &&) = delete;
  640. Root &operator=(Root &&) = delete;
  641. Root(const Root &) = delete;
  642. Root &operator=(const Root &) = delete;
  643. /// Returns the last error reported, or else a generic error.
  644. Error getError() const;
  645. /// Print the root value with the error shown inline as a comment.
  646. /// Unrelated parts of the value are elided for brevity, e.g.
  647. /// {
  648. /// "id": 42,
  649. /// "name": /* expected string */ null,
  650. /// "properties": { ... }
  651. /// }
  652. void printErrorContext(const Value &, llvm::raw_ostream &) const;
  653. };
  654. // Standard deserializers are provided for primitive types.
  655. // See comments on Value.
  656. inline bool fromJSON(const Value &E, std::string &Out, Path P) {
  657. if (auto S = E.getAsString()) {
  658. Out = std::string(*S);
  659. return true;
  660. }
  661. P.report("expected string");
  662. return false;
  663. }
  664. inline bool fromJSON(const Value &E, int &Out, Path P) {
  665. if (auto S = E.getAsInteger()) {
  666. Out = *S;
  667. return true;
  668. }
  669. P.report("expected integer");
  670. return false;
  671. }
  672. inline bool fromJSON(const Value &E, int64_t &Out, Path P) {
  673. if (auto S = E.getAsInteger()) {
  674. Out = *S;
  675. return true;
  676. }
  677. P.report("expected integer");
  678. return false;
  679. }
  680. inline bool fromJSON(const Value &E, double &Out, Path P) {
  681. if (auto S = E.getAsNumber()) {
  682. Out = *S;
  683. return true;
  684. }
  685. P.report("expected number");
  686. return false;
  687. }
  688. inline bool fromJSON(const Value &E, bool &Out, Path P) {
  689. if (auto S = E.getAsBoolean()) {
  690. Out = *S;
  691. return true;
  692. }
  693. P.report("expected boolean");
  694. return false;
  695. }
  696. inline bool fromJSON(const Value &E, uint64_t &Out, Path P) {
  697. if (auto S = E.getAsUINT64()) {
  698. Out = *S;
  699. return true;
  700. }
  701. P.report("expected uint64_t");
  702. return false;
  703. }
  704. inline bool fromJSON(const Value &E, std::nullptr_t &Out, Path P) {
  705. if (auto S = E.getAsNull()) {
  706. Out = *S;
  707. return true;
  708. }
  709. P.report("expected null");
  710. return false;
  711. }
  712. template <typename T>
  713. bool fromJSON(const Value &E, std::optional<T> &Out, Path P) {
  714. if (E.getAsNull()) {
  715. Out = std::nullopt;
  716. return true;
  717. }
  718. T Result;
  719. if (!fromJSON(E, Result, P))
  720. return false;
  721. Out = std::move(Result);
  722. return true;
  723. }
  724. template <typename T>
  725. bool fromJSON(const Value &E, std::vector<T> &Out, Path P) {
  726. if (auto *A = E.getAsArray()) {
  727. Out.clear();
  728. Out.resize(A->size());
  729. for (size_t I = 0; I < A->size(); ++I)
  730. if (!fromJSON((*A)[I], Out[I], P.index(I)))
  731. return false;
  732. return true;
  733. }
  734. P.report("expected array");
  735. return false;
  736. }
  737. template <typename T>
  738. bool fromJSON(const Value &E, std::map<std::string, T> &Out, Path P) {
  739. if (auto *O = E.getAsObject()) {
  740. Out.clear();
  741. for (const auto &KV : *O)
  742. if (!fromJSON(KV.second, Out[std::string(llvm::StringRef(KV.first))],
  743. P.field(KV.first)))
  744. return false;
  745. return true;
  746. }
  747. P.report("expected object");
  748. return false;
  749. }
  750. // Allow serialization of std::optional<T> for supported T.
  751. template <typename T> Value toJSON(const std::optional<T> &Opt) {
  752. return Opt ? Value(*Opt) : Value(nullptr);
  753. }
  754. /// Helper for mapping JSON objects onto protocol structs.
  755. ///
  756. /// Example:
  757. /// \code
  758. /// bool fromJSON(const Value &E, MyStruct &R, Path P) {
  759. /// ObjectMapper O(E, P);
  760. /// // When returning false, error details were already reported.
  761. /// return O && O.map("mandatory_field", R.MandatoryField) &&
  762. /// O.mapOptional("optional_field", R.OptionalField);
  763. /// }
  764. /// \endcode
  765. class ObjectMapper {
  766. public:
  767. /// If O is not an object, this mapper is invalid and an error is reported.
  768. ObjectMapper(const Value &E, Path P) : O(E.getAsObject()), P(P) {
  769. if (!O)
  770. P.report("expected object");
  771. }
  772. /// True if the expression is an object.
  773. /// Must be checked before calling map().
  774. operator bool() const { return O; }
  775. /// Maps a property to a field.
  776. /// If the property is missing or invalid, reports an error.
  777. template <typename T> bool map(StringLiteral Prop, T &Out) {
  778. assert(*this && "Must check this is an object before calling map()");
  779. if (const Value *E = O->get(Prop))
  780. return fromJSON(*E, Out, P.field(Prop));
  781. P.field(Prop).report("missing value");
  782. return false;
  783. }
  784. /// Maps a property to a field, if it exists.
  785. /// If the property exists and is invalid, reports an error.
  786. /// (Optional requires special handling, because missing keys are OK).
  787. template <typename T> bool map(StringLiteral Prop, std::optional<T> &Out) {
  788. assert(*this && "Must check this is an object before calling map()");
  789. if (const Value *E = O->get(Prop))
  790. return fromJSON(*E, Out, P.field(Prop));
  791. Out = std::nullopt;
  792. return true;
  793. }
  794. /// Maps a property to a field, if it exists.
  795. /// If the property exists and is invalid, reports an error.
  796. /// If the property does not exist, Out is unchanged.
  797. template <typename T> bool mapOptional(StringLiteral Prop, T &Out) {
  798. assert(*this && "Must check this is an object before calling map()");
  799. if (const Value *E = O->get(Prop))
  800. return fromJSON(*E, Out, P.field(Prop));
  801. return true;
  802. }
  803. private:
  804. const Object *O;
  805. Path P;
  806. };
  807. /// Parses the provided JSON source, or returns a ParseError.
  808. /// The returned Value is self-contained and owns its strings (they do not refer
  809. /// to the original source).
  810. llvm::Expected<Value> parse(llvm::StringRef JSON);
  811. class ParseError : public llvm::ErrorInfo<ParseError> {
  812. const char *Msg;
  813. unsigned Line, Column, Offset;
  814. public:
  815. static char ID;
  816. ParseError(const char *Msg, unsigned Line, unsigned Column, unsigned Offset)
  817. : Msg(Msg), Line(Line), Column(Column), Offset(Offset) {}
  818. void log(llvm::raw_ostream &OS) const override {
  819. OS << llvm::formatv("[{0}:{1}, byte={2}]: {3}", Line, Column, Offset, Msg);
  820. }
  821. std::error_code convertToErrorCode() const override {
  822. return llvm::inconvertibleErrorCode();
  823. }
  824. };
  825. /// Version of parse() that converts the parsed value to the type T.
  826. /// RootName describes the root object and is used in error messages.
  827. template <typename T>
  828. Expected<T> parse(const llvm::StringRef &JSON, const char *RootName = "") {
  829. auto V = parse(JSON);
  830. if (!V)
  831. return V.takeError();
  832. Path::Root R(RootName);
  833. T Result;
  834. if (fromJSON(*V, Result, R))
  835. return std::move(Result);
  836. return R.getError();
  837. }
  838. /// json::OStream allows writing well-formed JSON without materializing
  839. /// all structures as json::Value ahead of time.
  840. /// It's faster, lower-level, and less safe than OS << json::Value.
  841. /// It also allows emitting more constructs, such as comments.
  842. ///
  843. /// Only one "top-level" object can be written to a stream.
  844. /// Simplest usage involves passing lambdas (Blocks) to fill in containers:
  845. ///
  846. /// json::OStream J(OS);
  847. /// J.array([&]{
  848. /// for (const Event &E : Events)
  849. /// J.object([&] {
  850. /// J.attribute("timestamp", int64_t(E.Time));
  851. /// J.attributeArray("participants", [&] {
  852. /// for (const Participant &P : E.Participants)
  853. /// J.value(P.toString());
  854. /// });
  855. /// });
  856. /// });
  857. ///
  858. /// This would produce JSON like:
  859. ///
  860. /// [
  861. /// {
  862. /// "timestamp": 19287398741,
  863. /// "participants": [
  864. /// "King Kong",
  865. /// "Miley Cyrus",
  866. /// "Cleopatra"
  867. /// ]
  868. /// },
  869. /// ...
  870. /// ]
  871. ///
  872. /// The lower level begin/end methods (arrayBegin()) are more flexible but
  873. /// care must be taken to pair them correctly:
  874. ///
  875. /// json::OStream J(OS);
  876. // J.arrayBegin();
  877. /// for (const Event &E : Events) {
  878. /// J.objectBegin();
  879. /// J.attribute("timestamp", int64_t(E.Time));
  880. /// J.attributeBegin("participants");
  881. /// for (const Participant &P : E.Participants)
  882. /// J.value(P.toString());
  883. /// J.attributeEnd();
  884. /// J.objectEnd();
  885. /// }
  886. /// J.arrayEnd();
  887. ///
  888. /// If the call sequence isn't valid JSON, asserts will fire in debug mode.
  889. /// This can be mismatched begin()/end() pairs, trying to emit attributes inside
  890. /// an array, and so on.
  891. /// With asserts disabled, this is undefined behavior.
  892. class OStream {
  893. public:
  894. using Block = llvm::function_ref<void()>;
  895. // If IndentSize is nonzero, output is pretty-printed.
  896. explicit OStream(llvm::raw_ostream &OS, unsigned IndentSize = 0)
  897. : OS(OS), IndentSize(IndentSize) {
  898. Stack.emplace_back();
  899. }
  900. ~OStream() {
  901. assert(Stack.size() == 1 && "Unmatched begin()/end()");
  902. assert(Stack.back().Ctx == Singleton);
  903. assert(Stack.back().HasValue && "Did not write top-level value");
  904. }
  905. /// Flushes the underlying ostream. OStream does not buffer internally.
  906. void flush() { OS.flush(); }
  907. // High level functions to output a value.
  908. // Valid at top-level (exactly once), in an attribute value (exactly once),
  909. // or in an array (any number of times).
  910. /// Emit a self-contained value (number, string, vector<string> etc).
  911. void value(const Value &V);
  912. /// Emit an array whose elements are emitted in the provided Block.
  913. void array(Block Contents) {
  914. arrayBegin();
  915. Contents();
  916. arrayEnd();
  917. }
  918. /// Emit an object whose elements are emitted in the provided Block.
  919. void object(Block Contents) {
  920. objectBegin();
  921. Contents();
  922. objectEnd();
  923. }
  924. /// Emit an externally-serialized value.
  925. /// The caller must write exactly one valid JSON value to the provided stream.
  926. /// No validation or formatting of this value occurs.
  927. void rawValue(llvm::function_ref<void(raw_ostream &)> Contents) {
  928. rawValueBegin();
  929. Contents(OS);
  930. rawValueEnd();
  931. }
  932. void rawValue(llvm::StringRef Contents) {
  933. rawValue([&](raw_ostream &OS) { OS << Contents; });
  934. }
  935. /// Emit a JavaScript comment associated with the next printed value.
  936. /// The string must be valid until the next attribute or value is emitted.
  937. /// Comments are not part of standard JSON, and many parsers reject them!
  938. void comment(llvm::StringRef);
  939. // High level functions to output object attributes.
  940. // Valid only within an object (any number of times).
  941. /// Emit an attribute whose value is self-contained (number, vector<int> etc).
  942. void attribute(llvm::StringRef Key, const Value& Contents) {
  943. attributeImpl(Key, [&] { value(Contents); });
  944. }
  945. /// Emit an attribute whose value is an array with elements from the Block.
  946. void attributeArray(llvm::StringRef Key, Block Contents) {
  947. attributeImpl(Key, [&] { array(Contents); });
  948. }
  949. /// Emit an attribute whose value is an object with attributes from the Block.
  950. void attributeObject(llvm::StringRef Key, Block Contents) {
  951. attributeImpl(Key, [&] { object(Contents); });
  952. }
  953. // Low-level begin/end functions to output arrays, objects, and attributes.
  954. // Must be correctly paired. Allowed contexts are as above.
  955. void arrayBegin();
  956. void arrayEnd();
  957. void objectBegin();
  958. void objectEnd();
  959. void attributeBegin(llvm::StringRef Key);
  960. void attributeEnd();
  961. raw_ostream &rawValueBegin();
  962. void rawValueEnd();
  963. private:
  964. void attributeImpl(llvm::StringRef Key, Block Contents) {
  965. attributeBegin(Key);
  966. Contents();
  967. attributeEnd();
  968. }
  969. void valueBegin();
  970. void flushComment();
  971. void newline();
  972. enum Context {
  973. Singleton, // Top level, or object attribute.
  974. Array,
  975. Object,
  976. RawValue, // External code writing a value to OS directly.
  977. };
  978. struct State {
  979. Context Ctx = Singleton;
  980. bool HasValue = false;
  981. };
  982. llvm::SmallVector<State, 16> Stack; // Never empty.
  983. llvm::StringRef PendingComment;
  984. llvm::raw_ostream &OS;
  985. unsigned IndentSize;
  986. unsigned Indent = 0;
  987. };
  988. /// Serializes this Value to JSON, writing it to the provided stream.
  989. /// The formatting is compact (no extra whitespace) and deterministic.
  990. /// For pretty-printing, use the formatv() format_provider below.
  991. inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Value &V) {
  992. OStream(OS).value(V);
  993. return OS;
  994. }
  995. } // namespace json
  996. /// Allow printing json::Value with formatv().
  997. /// The default style is basic/compact formatting, like operator<<.
  998. /// A format string like formatv("{0:2}", Value) pretty-prints with indent 2.
  999. template <> struct format_provider<llvm::json::Value> {
  1000. static void format(const llvm::json::Value &, raw_ostream &, StringRef);
  1001. };
  1002. } // namespace llvm
  1003. #endif
  1004. #ifdef __GNUC__
  1005. #pragma GCC diagnostic pop
  1006. #endif