1 //===--- JSON.h - JSON values, parsing and serialization -------*- C++ -*-===//
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
7 //===---------------------------------------------------------------------===//
10 /// This file supports working with JSON data.
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.
18 /// - functions to parse JSON text into Values, and to serialize Values to text.
19 /// See parse(), operator<<, and format_provider.
21 /// - a convention and helpers for mapping between json::Value and user-defined
22 /// types. See fromJSON(), ObjectMapper, and the class comment on Value.
24 /// - an output API json::OStream which can emit JSON without materializing
25 /// all structures as json::Value.
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).
31 /// Other serialization mechanisms you may consider:
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.
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
44 //===---------------------------------------------------------------------===//
46 #ifndef LLVM_SUPPORT_JSON_H
47 #define LLVM_SUPPORT_JSON_H
49 #include "llvm/ADT/DenseMap.h"
50 #include "llvm/ADT/SmallVector.h"
51 #include "llvm/ADT/StringRef.h"
52 #include "llvm/Support/Error.h"
53 #include "llvm/Support/FormatVariadic.h"
54 #include "llvm/Support/raw_ostream.h"
60 // === String encodings ===
62 // JSON strings are character sequences (not byte sequences like std::string).
63 // We need to know the encoding, and for simplicity only support UTF-8.
65 // - When parsing, invalid UTF-8 is a syntax error like any other
67 // - When creating Values from strings, callers must ensure they are UTF-8.
68 // with asserts on, invalid UTF-8 will crash the program
69 // with asserts off, we'll substitute the replacement character (U+FFFD)
70 // Callers can use json::isUTF8() and json::fixUTF8() for validation.
72 // - When retrieving strings from Values (e.g. asString()), the result will
73 // always be valid UTF-8.
75 /// Returns true if \p S is valid UTF-8, which is required for use as JSON.
76 /// If it returns false, \p Offset is set to a byte offset near the first error.
77 bool isUTF8(llvm::StringRef S
, size_t *ErrOffset
= nullptr);
78 /// Replaces invalid UTF-8 sequences in \p S with the replacement character
79 /// (U+FFFD). The returned string is valid UTF-8.
80 /// This is much slower than isUTF8, so test that first.
81 std::string
fixUTF8(llvm::StringRef S
);
86 template <typename T
> Value
toJSON(const llvm::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.
91 using Storage
= DenseMap
<ObjectKey
, Value
, llvm::DenseMapInfo
<StringRef
>>;
95 using key_type
= ObjectKey
;
96 using mapped_type
= Value
;
97 using value_type
= Storage::value_type
;
98 using iterator
= Storage::iterator
;
99 using const_iterator
= Storage::const_iterator
;
102 // KV is a trivial key-value struct for list-initialization.
103 // (using std::pair forces extra copies).
105 explicit Object(std::initializer_list
<KV
> Properties
);
107 iterator
begin() { return M
.begin(); }
108 const_iterator
begin() const { return M
.begin(); }
109 iterator
end() { return M
.end(); }
110 const_iterator
end() const { return M
.end(); }
112 bool empty() const { return M
.empty(); }
113 size_t size() const { return M
.size(); }
115 void clear() { M
.clear(); }
116 std::pair
<iterator
, bool> insert(KV E
);
117 template <typename
... Ts
>
118 std::pair
<iterator
, bool> try_emplace(const ObjectKey
&K
, Ts
&&... Args
) {
119 return M
.try_emplace(K
, std::forward
<Ts
>(Args
)...);
121 template <typename
... Ts
>
122 std::pair
<iterator
, bool> try_emplace(ObjectKey
&&K
, Ts
&&... Args
) {
123 return M
.try_emplace(std::move(K
), std::forward
<Ts
>(Args
)...);
126 iterator
find(StringRef K
) { return M
.find_as(K
); }
127 const_iterator
find(StringRef K
) const { return M
.find_as(K
); }
128 // operator[] acts as if Value was default-constructible as null.
129 Value
&operator[](const ObjectKey
&K
);
130 Value
&operator[](ObjectKey
&&K
);
131 // Look up a property, returning nullptr if it doesn't exist.
132 Value
*get(StringRef K
);
133 const Value
*get(StringRef K
) const;
134 // Typed accessors return None/nullptr if
135 // - the property doesn't exist
136 // - or it has the wrong type
137 llvm::Optional
<std::nullptr_t
> getNull(StringRef K
) const;
138 llvm::Optional
<bool> getBoolean(StringRef K
) const;
139 llvm::Optional
<double> getNumber(StringRef K
) const;
140 llvm::Optional
<int64_t> getInteger(StringRef K
) const;
141 llvm::Optional
<llvm::StringRef
> getString(StringRef K
) const;
142 const json::Object
*getObject(StringRef K
) const;
143 json::Object
*getObject(StringRef K
);
144 const json::Array
*getArray(StringRef K
) const;
145 json::Array
*getArray(StringRef K
);
147 bool operator==(const Object
&LHS
, const Object
&RHS
);
148 inline bool operator!=(const Object
&LHS
, const Object
&RHS
) {
149 return !(LHS
== RHS
);
152 /// An Array is a JSON array, which contains heterogeneous JSON values.
153 /// It simulates std::vector<Value>.
155 std::vector
<Value
> V
;
158 using value_type
= Value
;
159 using iterator
= std::vector
<Value
>::iterator
;
160 using const_iterator
= std::vector
<Value
>::const_iterator
;
163 explicit Array(std::initializer_list
<Value
> Elements
);
164 template <typename Collection
> explicit Array(const Collection
&C
) {
165 for (const auto &V
: C
)
169 Value
&operator[](size_t I
) { return V
[I
]; }
170 const Value
&operator[](size_t I
) const { return V
[I
]; }
171 Value
&front() { return V
.front(); }
172 const Value
&front() const { return V
.front(); }
173 Value
&back() { return V
.back(); }
174 const Value
&back() const { return V
.back(); }
175 Value
*data() { return V
.data(); }
176 const Value
*data() const { return V
.data(); }
178 iterator
begin() { return V
.begin(); }
179 const_iterator
begin() const { return V
.begin(); }
180 iterator
end() { return V
.end(); }
181 const_iterator
end() const { return V
.end(); }
183 bool empty() const { return V
.empty(); }
184 size_t size() const { return V
.size(); }
185 void reserve(size_t S
) { V
.reserve(S
); }
187 void clear() { V
.clear(); }
188 void push_back(const Value
&E
) { V
.push_back(E
); }
189 void push_back(Value
&&E
) { V
.push_back(std::move(E
)); }
190 template <typename
... Args
> void emplace_back(Args
&&... A
) {
191 V
.emplace_back(std::forward
<Args
>(A
)...);
193 void pop_back() { V
.pop_back(); }
194 // FIXME: insert() takes const_iterator since C++11, old libstdc++ disagrees.
195 iterator
insert(iterator P
, const Value
&E
) { return V
.insert(P
, E
); }
196 iterator
insert(iterator P
, Value
&&E
) {
197 return V
.insert(P
, std::move(E
));
199 template <typename It
> iterator
insert(iterator P
, It A
, It Z
) {
200 return V
.insert(P
, A
, Z
);
202 template <typename
... Args
> iterator
emplace(const_iterator P
, Args
&&... A
) {
203 return V
.emplace(P
, std::forward
<Args
>(A
)...);
206 friend bool operator==(const Array
&L
, const Array
&R
) { return L
.V
== R
.V
; }
208 inline bool operator!=(const Array
&L
, const Array
&R
) { return !(L
== R
); }
210 /// A Value is an JSON value of unknown type.
211 /// They can be copied, but should generally be moved.
213 /// === Composing values ===
215 /// You can implicitly construct Values from:
216 /// - strings: std::string, SmallString, formatv, StringRef, char*
217 /// (char*, and StringRef are references, not copies!)
221 /// - arrays: {"foo", 42.0, false}
222 /// - serializable things: types with toJSON(const T&)->Value, found by ADL
224 /// They can also be constructed from object/array helpers:
225 /// - json::Object is a type like map<ObjectKey, Value>
226 /// - json::Array is a type like vector<Value>
227 /// These can be list-initialized, or used to build up collections in a loop.
228 /// json::ary(Collection) converts all items in a collection to Values.
230 /// === Inspecting values ===
232 /// Each Value is one of the JSON kinds:
235 /// number (double or int64)
236 /// string (StringRef)
237 /// array (json::Array)
238 /// object (json::Object)
240 /// The kind can be queried directly, or implicitly via the typed accessors:
241 /// if (Optional<StringRef> S = E.getAsString()
242 /// assert(E.kind() == Value::String);
244 /// Array and Object also have typed indexing accessors for easy traversal:
245 /// Expected<Value> E = parse(R"( {"options": {"font": "sans-serif"}} )");
246 /// if (Object* O = E->getAsObject())
247 /// if (Object* Opts = O->getObject("options"))
248 /// if (Optional<StringRef> Font = Opts->getString("font"))
249 /// assert(Opts->at("font").kind() == Value::String);
251 /// === Converting JSON values to C++ types ===
253 /// The convention is to have a deserializer function findable via ADL:
254 /// fromJSON(const json::Value&, T&)->bool
255 /// Deserializers are provided for:
257 /// - int and int64_t
260 /// - vector<T>, where T is deserializable
261 /// - map<string, T>, where T is deserializable
262 /// - Optional<T>, where T is deserializable
263 /// ObjectMapper can help writing fromJSON() functions for object types.
265 /// For conversion in the other direction, the serializer function is:
266 /// toJSON(const T&) -> json::Value
267 /// If this exists, then it also allows constructing Value from T, and can
268 /// be used to serialize vector<T>, map<string, T>, and Optional<T>.
270 /// === Serialization ===
272 /// Values can be serialized to JSON:
273 /// 1) raw_ostream << Value // Basic formatting.
274 /// 2) raw_ostream << formatv("{0}", Value) // Basic formatting.
275 /// 3) raw_ostream << formatv("{0:2}", Value) // Pretty-print with indent 2.
278 /// Expected<Value> E = json::parse("[1, 2, null]");
279 /// assert(E && E->kind() == Value::Array);
285 /// Number values can store both int64s and doubles at full precision,
286 /// depending on what they were constructed/parsed from.
293 // It would be nice to have Value() be null. But that would make {} null too.
294 Value(const Value
&M
) { copyFrom(M
); }
295 Value(Value
&&M
) { moveFrom(std::move(M
)); }
296 Value(std::initializer_list
<Value
> Elements
);
297 Value(json::Array
&&Elements
) : Type(T_Array
) {
298 create
<json::Array
>(std::move(Elements
));
300 template <typename Elt
>
301 Value(const std::vector
<Elt
> &C
) : Value(json::Array(C
)) {}
302 Value(json::Object
&&Properties
) : Type(T_Object
) {
303 create
<json::Object
>(std::move(Properties
));
305 template <typename Elt
>
306 Value(const std::map
<std::string
, Elt
> &C
) : Value(json::Object(C
)) {}
307 // Strings: types with value semantics. Must be valid UTF-8.
308 Value(std::string V
) : Type(T_String
) {
309 if (LLVM_UNLIKELY(!isUTF8(V
))) {
310 assert(false && "Invalid UTF-8 in value used as JSON");
311 V
= fixUTF8(std::move(V
));
313 create
<std::string
>(std::move(V
));
315 Value(const llvm::SmallVectorImpl
<char> &V
)
316 : Value(std::string(V
.begin(), V
.end())) {}
317 Value(const llvm::formatv_object_base
&V
) : Value(V
.str()) {}
318 // Strings: types with reference semantics. Must be valid UTF-8.
319 Value(StringRef V
) : Type(T_StringRef
) {
320 create
<llvm::StringRef
>(V
);
321 if (LLVM_UNLIKELY(!isUTF8(V
))) {
322 assert(false && "Invalid UTF-8 in value used as JSON");
323 *this = Value(fixUTF8(V
));
326 Value(const char *V
) : Value(StringRef(V
)) {}
327 Value(std::nullptr_t
) : Type(T_Null
) {}
328 // Boolean (disallow implicit conversions).
329 // (The last template parameter is a dummy to keep templates distinct.)
332 typename
= typename
std::enable_if
<std::is_same
<T
, bool>::value
>::type
,
334 Value(T B
) : Type(T_Boolean
) {
337 // Integers (except boolean). Must be non-narrowing convertible to int64_t.
340 typename
= typename
std::enable_if
<std::is_integral
<T
>::value
>::type
,
341 typename
= typename
std::enable_if
<!std::is_same
<T
, bool>::value
>::type
>
342 Value(T I
) : Type(T_Integer
) {
343 create
<int64_t>(int64_t{I
});
345 // Floating point. Must be non-narrowing convertible to double.
346 template <typename T
,
348 typename
std::enable_if
<std::is_floating_point
<T
>::value
>::type
,
350 Value(T D
) : Type(T_Double
) {
351 create
<double>(double{D
});
353 // Serializable types: with a toJSON(const T&)->Value function, found by ADL.
354 template <typename T
,
355 typename
= typename
std::enable_if
<std::is_same
<
356 Value
, decltype(toJSON(*(const T
*)nullptr))>::value
>,
358 Value(const T
&V
) : Value(toJSON(V
)) {}
360 Value
&operator=(const Value
&M
) {
365 Value
&operator=(Value
&&M
) {
367 moveFrom(std::move(M
));
370 ~Value() { destroy(); }
389 llvm_unreachable("Unknown kind");
392 // Typed accessors return None/nullptr if the Value is not of this type.
393 llvm::Optional
<std::nullptr_t
> getAsNull() const {
394 if (LLVM_LIKELY(Type
== T_Null
))
398 llvm::Optional
<bool> getAsBoolean() const {
399 if (LLVM_LIKELY(Type
== T_Boolean
))
403 llvm::Optional
<double> getAsNumber() const {
404 if (LLVM_LIKELY(Type
== T_Double
))
406 if (LLVM_LIKELY(Type
== T_Integer
))
407 return as
<int64_t>();
410 // Succeeds if the Value is a Number, and exactly representable as int64_t.
411 llvm::Optional
<int64_t> getAsInteger() const {
412 if (LLVM_LIKELY(Type
== T_Integer
))
413 return as
<int64_t>();
414 if (LLVM_LIKELY(Type
== T_Double
)) {
415 double D
= as
<double>();
416 if (LLVM_LIKELY(std::modf(D
, &D
) == 0.0 &&
417 D
>= double(std::numeric_limits
<int64_t>::min()) &&
418 D
<= double(std::numeric_limits
<int64_t>::max())))
423 llvm::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
>();
430 const json::Object
*getAsObject() const {
431 return LLVM_LIKELY(Type
== T_Object
) ? &as
<json::Object
>() : nullptr;
433 json::Object
*getAsObject() {
434 return LLVM_LIKELY(Type
== T_Object
) ? &as
<json::Object
>() : nullptr;
436 const json::Array
*getAsArray() const {
437 return LLVM_LIKELY(Type
== T_Array
) ? &as
<json::Array
>() : nullptr;
439 json::Array
*getAsArray() {
440 return LLVM_LIKELY(Type
== T_Array
) ? &as
<json::Array
>() : nullptr;
445 void copyFrom(const Value
&M
);
446 // We allow moving from *const* Values, by marking all members as mutable!
447 // This hack is needed to support initializer-list syntax efficiently.
448 // (std::initializer_list<T> is a container of const T).
449 void moveFrom(const Value
&&M
);
453 template <typename T
, typename
... U
> void create(U
&&... V
) {
454 new (reinterpret_cast<T
*>(Union
.buffer
)) T(std::forward
<U
>(V
)...);
456 template <typename T
> T
&as() const {
457 // Using this two-step static_cast via void * instead of reinterpret_cast
458 // silences a -Wstrict-aliasing false positive from GCC6 and earlier.
459 void *Storage
= static_cast<void *>(Union
.buffer
);
460 return *static_cast<T
*>(Storage
);
463 friend class OStream
;
465 enum ValueType
: char {
475 // All members mutable, see moveFrom().
476 mutable ValueType Type
;
477 mutable llvm::AlignedCharArrayUnion
<bool, double, int64_t, llvm::StringRef
,
478 std::string
, json::Array
, json::Object
>
480 friend bool operator==(const Value
&, const Value
&);
483 bool operator==(const Value
&, const Value
&);
484 inline bool operator!=(const Value
&L
, const Value
&R
) { return !(L
== R
); }
486 /// ObjectKey is a used to capture keys in Object. Like Value but:
487 /// - only strings are allowed
488 /// - it's optimized for the string literal case (Owned == nullptr)
489 /// Like Value, strings must be UTF-8. See isUTF8 documentation for details.
492 ObjectKey(const char *S
) : ObjectKey(StringRef(S
)) {}
493 ObjectKey(std::string S
) : Owned(new std::string(std::move(S
))) {
494 if (LLVM_UNLIKELY(!isUTF8(*Owned
))) {
495 assert(false && "Invalid UTF-8 in value used as JSON");
496 *Owned
= fixUTF8(std::move(*Owned
));
500 ObjectKey(llvm::StringRef S
) : Data(S
) {
501 if (LLVM_UNLIKELY(!isUTF8(Data
))) {
502 assert(false && "Invalid UTF-8 in value used as JSON");
503 *this = ObjectKey(fixUTF8(S
));
506 ObjectKey(const llvm::SmallVectorImpl
<char> &V
)
507 : ObjectKey(std::string(V
.begin(), V
.end())) {}
508 ObjectKey(const llvm::formatv_object_base
&V
) : ObjectKey(V
.str()) {}
510 ObjectKey(const ObjectKey
&C
) { *this = C
; }
511 ObjectKey(ObjectKey
&&C
) : ObjectKey(static_cast<const ObjectKey
&&>(C
)) {}
512 ObjectKey
&operator=(const ObjectKey
&C
) {
514 Owned
.reset(new std::string(*C
.Owned
));
521 ObjectKey
&operator=(ObjectKey
&&) = default;
523 operator llvm::StringRef() const { return Data
; }
524 std::string
str() const { return Data
.str(); }
527 // FIXME: this is unneccesarily large (3 pointers). Pointer + length + owned
528 // could be 2 pointers at most.
529 std::unique_ptr
<std::string
> Owned
;
530 llvm::StringRef Data
;
533 inline bool operator==(const ObjectKey
&L
, const ObjectKey
&R
) {
534 return llvm::StringRef(L
) == llvm::StringRef(R
);
536 inline bool operator!=(const ObjectKey
&L
, const ObjectKey
&R
) {
539 inline bool operator<(const ObjectKey
&L
, const ObjectKey
&R
) {
540 return StringRef(L
) < StringRef(R
);
548 inline Object::Object(std::initializer_list
<KV
> Properties
) {
549 for (const auto &P
: Properties
) {
550 auto R
= try_emplace(P
.K
, nullptr);
552 R
.first
->getSecond().moveFrom(std::move(P
.V
));
555 inline std::pair
<Object::iterator
, bool> Object::insert(KV E
) {
556 return try_emplace(std::move(E
.K
), std::move(E
.V
));
559 // Standard deserializers are provided for primitive types.
560 // See comments on Value.
561 inline bool fromJSON(const Value
&E
, std::string
&Out
) {
562 if (auto S
= E
.getAsString()) {
568 inline bool fromJSON(const Value
&E
, int &Out
) {
569 if (auto S
= E
.getAsInteger()) {
575 inline bool fromJSON(const Value
&E
, int64_t &Out
) {
576 if (auto S
= E
.getAsInteger()) {
582 inline bool fromJSON(const Value
&E
, double &Out
) {
583 if (auto S
= E
.getAsNumber()) {
589 inline bool fromJSON(const Value
&E
, bool &Out
) {
590 if (auto S
= E
.getAsBoolean()) {
596 template <typename T
> bool fromJSON(const Value
&E
, llvm::Optional
<T
> &Out
) {
602 if (!fromJSON(E
, Result
))
604 Out
= std::move(Result
);
607 template <typename T
> bool fromJSON(const Value
&E
, std::vector
<T
> &Out
) {
608 if (auto *A
= E
.getAsArray()) {
610 Out
.resize(A
->size());
611 for (size_t I
= 0; I
< A
->size(); ++I
)
612 if (!fromJSON((*A
)[I
], Out
[I
]))
618 template <typename T
>
619 bool fromJSON(const Value
&E
, std::map
<std::string
, T
> &Out
) {
620 if (auto *O
= E
.getAsObject()) {
622 for (const auto &KV
: *O
)
623 if (!fromJSON(KV
.second
, Out
[llvm::StringRef(KV
.first
)]))
630 // Allow serialization of Optional<T> for supported T.
631 template <typename T
> Value
toJSON(const llvm::Optional
<T
> &Opt
) {
632 return Opt
? Value(*Opt
) : Value(nullptr);
635 /// Helper for mapping JSON objects onto protocol structs.
639 /// bool fromJSON(const Value &E, MyStruct &R) {
640 /// ObjectMapper O(E);
641 /// if (!O || !O.map("mandatory_field", R.MandatoryField))
643 /// O.map("optional_field", R.OptionalField);
649 ObjectMapper(const Value
&E
) : O(E
.getAsObject()) {}
651 /// True if the expression is an object.
652 /// Must be checked before calling map().
653 operator bool() { return O
; }
655 /// Maps a property to a field, if it exists.
656 template <typename T
> bool map(StringRef Prop
, T
&Out
) {
657 assert(*this && "Must check this is an object before calling map()");
658 if (const Value
*E
= O
->get(Prop
))
659 return fromJSON(*E
, Out
);
663 /// Maps a property to a field, if it exists.
664 /// (Optional requires special handling, because missing keys are OK).
665 template <typename T
> bool map(StringRef Prop
, llvm::Optional
<T
> &Out
) {
666 assert(*this && "Must check this is an object before calling map()");
667 if (const Value
*E
= O
->get(Prop
))
668 return fromJSON(*E
, Out
);
677 /// Parses the provided JSON source, or returns a ParseError.
678 /// The returned Value is self-contained and owns its strings (they do not refer
679 /// to the original source).
680 llvm::Expected
<Value
> parse(llvm::StringRef JSON
);
682 class ParseError
: public llvm::ErrorInfo
<ParseError
> {
684 unsigned Line
, Column
, Offset
;
688 ParseError(const char *Msg
, unsigned Line
, unsigned Column
, unsigned Offset
)
689 : Msg(Msg
), Line(Line
), Column(Column
), Offset(Offset
) {}
690 void log(llvm::raw_ostream
&OS
) const override
{
691 OS
<< llvm::formatv("[{0}:{1}, byte={2}]: {3}", Line
, Column
, Offset
, Msg
);
693 std::error_code
convertToErrorCode() const override
{
694 return llvm::inconvertibleErrorCode();
698 /// json::OStream allows writing well-formed JSON without materializing
699 /// all structures as json::Value ahead of time.
700 /// It's faster, lower-level, and less safe than OS << json::Value.
702 /// Only one "top-level" object can be written to a stream.
703 /// Simplest usage involves passing lambdas (Blocks) to fill in containers:
705 /// json::OStream J(OS);
707 /// for (const Event &E : Events)
709 /// J.attribute("timestamp", int64_t(E.Time));
710 /// J.attributeArray("participants", [&] {
711 /// for (const Participant &P : E.Participants)
712 /// J.string(P.toString());
717 /// This would produce JSON like:
721 /// "timestamp": 19287398741,
722 /// "participants": [
731 /// The lower level begin/end methods (arrayBegin()) are more flexible but
732 /// care must be taken to pair them correctly:
734 /// json::OStream J(OS);
736 /// for (const Event &E : Events) {
738 /// J.attribute("timestamp", int64_t(E.Time));
739 /// J.attributeBegin("participants");
740 /// for (const Participant &P : E.Participants)
741 /// J.value(P.toString());
742 /// J.attributeEnd();
747 /// If the call sequence isn't valid JSON, asserts will fire in debug mode.
748 /// This can be mismatched begin()/end() pairs, trying to emit attributes inside
749 /// an array, and so on.
750 /// With asserts disabled, this is undefined behavior.
753 using Block
= llvm::function_ref
<void()>;
754 // If IndentSize is nonzero, output is pretty-printed.
755 explicit OStream(llvm::raw_ostream
&OS
, unsigned IndentSize
= 0)
756 : OS(OS
), IndentSize(IndentSize
) {
757 Stack
.emplace_back();
760 assert(Stack
.size() == 1 && "Unmatched begin()/end()");
761 assert(Stack
.back().Ctx
== Singleton
);
762 assert(Stack
.back().HasValue
&& "Did not write top-level value");
765 /// Flushes the underlying ostream. OStream does not buffer internally.
766 void flush() { OS
.flush(); }
768 // High level functions to output a value.
769 // Valid at top-level (exactly once), in an attribute value (exactly once),
770 // or in an array (any number of times).
772 /// Emit a self-contained value (number, string, vector<string> etc).
773 void value(const Value
&V
);
774 /// Emit an array whose elements are emitted in the provided Block.
775 void array(Block Contents
) {
780 /// Emit an object whose elements are emitted in the provided Block.
781 void object(Block Contents
) {
787 // High level functions to output object attributes.
788 // Valid only within an object (any number of times).
790 /// Emit an attribute whose value is self-contained (number, vector<int> etc).
791 void attribute(llvm::StringRef Key
, const Value
& Contents
) {
792 attributeImpl(Key
, [&] { value(Contents
); });
794 /// Emit an attribute whose value is an array with elements from the Block.
795 void attributeArray(llvm::StringRef Key
, Block Contents
) {
796 attributeImpl(Key
, [&] { array(Contents
); });
798 /// Emit an attribute whose value is an object with attributes from the Block.
799 void attributeObject(llvm::StringRef Key
, Block Contents
) {
800 attributeImpl(Key
, [&] { object(Contents
); });
803 // Low-level begin/end functions to output arrays, objects, and attributes.
804 // Must be correctly paired. Allowed contexts are as above.
810 void attributeBegin(llvm::StringRef Key
);
814 void attributeImpl(llvm::StringRef Key
, Block Contents
) {
824 Singleton
, // Top level, or object attribute.
829 Context Ctx
= Singleton
;
830 bool HasValue
= false;
832 llvm::SmallVector
<State
, 16> Stack
; // Never empty.
833 llvm::raw_ostream
&OS
;
838 /// Serializes this Value to JSON, writing it to the provided stream.
839 /// The formatting is compact (no extra whitespace) and deterministic.
840 /// For pretty-printing, use the formatv() format_provider below.
841 inline llvm::raw_ostream
&operator<<(llvm::raw_ostream
&OS
, const Value
&V
) {
842 OStream(OS
).value(V
);
847 /// Allow printing json::Value with formatv().
848 /// The default style is basic/compact formatting, like operator<<.
849 /// A format string like formatv("{0:2}", Value) pretty-prints with indent 2.
850 template <> struct format_provider
<llvm::json::Value
> {
851 static void format(const llvm::json::Value
&, raw_ostream
&, StringRef
);