JSON.h 34 KB

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